gpt4 book ai didi

java - 使用 Java 将数据写入 Excel 工作表

转载 作者:行者123 更新时间:2023-12-01 11:37:19 25 4
gpt4 key购买 nike

我正在编写一个程序,它将数据写入表格格式的 Excel 工作表中。我的代码为我提供了我想要的所有详细信息。

但是为了获得表格格式,我必须在 Excel 工作表中执行这些步骤

(选择列 -> 数据 -> 文本到列 -> 选择“分隔”选项 -> 单击“下一步”按钮 -> 选择“逗号”选项 -> 单击“下一步”按钮 ->单击“完成”按钮)。

我希望我的代码能够自动生成我想要的格式,而无需我在 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 - the Java API for Microsoft Documents 。Apache POI 项目是 Excel 文件格式(xls 和 xlsx)的纯 Java 实现。您可以通过 POI 直接写入/读取 native Excel 文件。请参阅examples

关于java - 使用 Java 将数据写入 Excel 工作表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29838838/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com