使用Java将数据写入Excel表单

我正在写一个程序,它将数据写入表格格式的excel表格。 我的代码给了我所有的细节,我想要的。

但为了获得表格格式,我必须在Excel表格中遵循这些步骤

(select一列 – >数据 – >文本到列 – >select“分隔”选项 – >点击button'下一步' – >select'逗号' – >点击button'下一步' – >点击button'完成')。 。

我希望我的代码自动生成我想要的格式,而无需在Excel工作表中执行上述步骤。 任何人都可以帮助我吗? 提前致谢。 下面显示的是我的代码。

public class GenrateCSV { private static JFileChooser fileChooser; private ComparisonController comparisonController; private int referenceId; private void generateXlsFile(String sFileName, ComparisonController comparisonController) { try { this.referenceId = comparisonController.getReferenceId(); FileWriter writer = new FileWriter(sFileName); //Main headings writer.append(","); writer.append(","); writer.append(","); writer.append('\n'); writer.append('\n'); writer.append(","); writer.append(","); writer.append("Case name"); writer.append(","); writer.append(","); writer.append(','); writer.append("Folder 01"); writer.append(","); writer.append(','); writer.append(','); writer.append(','); writer.append(','); writer.append("Folder 02"); writer.append(","); writer.append(','); writer.append(','); writer.append(','); writer.append(","); writer.append("Compared results"); writer.append('\n'); //folder 01- sub headings writer.append(","); writer.append(","); writer.append(","); writer.append(","); writer.append("Min. Jacobian"); writer.append(","); writer.append("Average Jacobian"); writer.append(","); writer.append("Max. Jacobian"); writer.append(','); writer.append("Range"); writer.append(","); writer.append(','); //folder 02 - sub headings writer.append("Min. Jacobian"); writer.append(","); writer.append("Average Jacobian"); writer.append(","); writer.append("Max. Jacobian"); writer.append(','); writer.append("Range"); writer.append(","); writer.append(','); //comapred results - sub headings writer.append("Percentage change of min. values"); writer.append(","); writer.append("Percentage change of Average"); writer.append(","); writer.append("Percentage change of min. values"); writer.append(","); writer.append("Percentage change of Ranges"); writer.append('\n'); //empty line as for the 2nd line in all the columns writer.append('\n'); //Data for folder 1. Case: turb_rad_A70 1 //case name writer.append(","); writer.append(","); String string = comparisonController.getQalFile1().getFileDetails().getParent();; string = string.replace("\\", ","); String[] splittedStr = string.split(","); writer.append(splittedStr[splittedStr.length - 1]); //Min. Jacobian writer.append(","); writer.append(","); if (referenceId == 0) { writer.append(String.valueOf(comparisonController.getQalFile1().getMinimumElement().getJacobianRatio())); } //Avg.Jacobian writer.append(","); if (referenceId == 0) { writer.append(String.valueOf(comparisonController.getQalFile1().getAvgElement().getJacobianRatio())); } //Max. Jacobian writer.append(","); if (referenceId == 0) { writer.append(String.valueOf(comparisonController.getQalFile1().getMaximumElement().getJacobianRatio())); } //Range writer.append(","); if (referenceId == 0) { writer.append(String.valueOf(comparisonController.getQalFile1().getMaximumElement().getJacobianRatio() - comparisonController.getQalFile1().getMinimumElement().getJacobianRatio())); } //Data for folder 2. Case: turb_rad_A70 1 //Min. Jacobian writer.append(","); writer.append(","); if (referenceId == 0) { writer.append(String.valueOf(comparisonController.getQalFile2().getMinimumElement().getJacobianRatio())); } //Avg.Jacobian writer.append(","); if (referenceId == 0) { writer.append(String.valueOf(comparisonController.getQalFile2().getAvgElement().getJacobianRatio())); } //Max. Jacobian writer.append(","); if (referenceId == 0) { writer.append(String.valueOf(comparisonController.getQalFile2().getMaximumElement().getJacobianRatio())); } //Range writer.append(","); if (referenceId == 0) { writer.append(String.valueOf(comparisonController.getQalFile2().getMaximumElement().getJacobianRatio() - comparisonController.getQalFile2().getMinimumElement().getJacobianRatio())); } //Data for compared reults. Case: turb_rad_A70 1 //Percentage change of min.values ((Folder 01 - Folder 02)/|Folder 01|*100) writer.append(","); writer.append(","); if (referenceId == 0) { writer.append(String.valueOf(((comparisonController.getQalFile1().getMinimumElement().getJacobianRatio() - comparisonController.getQalFile2().getMinimumElement().getJacobianRatio()) / comparisonController.getQalFile1().getMinimumElement().getJacobianRatio()) * 100)); } //Percentage change of Average. ((Folder 01 - Folder 02)/|Folder 01|*100) writer.append(","); if (referenceId == 0) { writer.append(String.valueOf(((comparisonController.getQalFile1().getAvgElement().getJacobianRatio() - comparisonController.getQalFile2().getAvgElement().getJacobianRatio()) / comparisonController.getQalFile1().getAvgElement().getJacobianRatio()) * 100)); } //Percentage change of max.values ((Folder 01 - Folder 02)/|Folder 01|*100) writer.append(","); if (referenceId == 0) { writer.append(String.valueOf(((comparisonController.getQalFile1().getMaximumElement().getJacobianRatio() - comparisonController.getQalFile2().getMaximumElement().getJacobianRatio()) / comparisonController.getQalFile1().getMaximumElement().getJacobianRatio()) * 100)); } //Percentage change of Range. ((Folder 01 - Folder 02)/|Folder 01|*100) writer.append(","); if (referenceId == 0) { double file1range = comparisonController.getQalFile1().getMaximumElement().getJacobianRatio() - comparisonController.getQalFile1().getMinimumElement().getJacobianRatio(); double file2range = comparisonController.getQalFile2().getMaximumElement().getJacobianRatio() - comparisonController.getQalFile2().getMinimumElement().getJacobianRatio(); writer.append(String.valueOf(((file1range - file2range) / file1range) * 100)); // writer.append(String.valueOf(((comparisonController.getQalFile1().getMaximumElement().getJacobianRatio() - comparisonController.getQalFile1().getMinimumElement().getJacobianRatio()) - ((comparisonController.getQalFile2().getMaximumElement().getJacobianRatio() - comparisonController.getQalFile2().getMinimumElement().getJacobianRatio()) - (comparisonController.getQalFile1().getMaximumElement().getJacobianRatio() - comparisonController.getQalFile1().getMinimumElement().getJacobianRatio()))) / (comparisonController.getQalFile1().getMaximumElement().getJacobianRatio() - comparisonController.getQalFile1().getMinimumElement().getJacobianRatio()) * 100)); } System.out.println(writer.toString()); System.out.println("1-max" + comparisonController.getQalFile1().getMaximumElement().getJacobianRatio()); System.out.println("1-min" + comparisonController.getQalFile1().getMinimumElement().getJacobianRatio()); System.out.println(); System.out.println("2-max" + comparisonController.getQalFile1().getMaximumElement().getJacobianRatio()); System.out.println("2-min" + comparisonController.getQalFile1().getMinimumElement().getJacobianRatio()); System.out.println(); System.out.println("1-range" + (comparisonController.getQalFile1().getMaximumElement().getJacobianRatio() - comparisonController.getQalFile1().getMinimumElement().getJacobianRatio())); System.out.println("2-range" + (comparisonController.getQalFile2().getMaximumElement().getJacobianRatio() - comparisonController.getQalFile2().getMinimumElement().getJacobianRatio())); System.out.println(); double file1range = comparisonController.getQalFile1().getMaximumElement().getJacobianRatio() - comparisonController.getQalFile1().getMinimumElement().getJacobianRatio(); double file2range = comparisonController.getQalFile2().getMaximumElement().getJacobianRatio() - comparisonController.getQalFile2().getMinimumElement().getJacobianRatio(); System.out.println(((file1range - file2range) / file1range) * 100); 

`

请参阅Apache POI – Microsoft文档的Java API 。 Apache POI Project是Excel文件格式(xls和xlsx)的纯Java实现。 您可以通过POI直接编写/读取本机Excel的文件。 请看例子 。

Jasper Reports为您提供了一种编写excel文件的简单方法