当前位置: 首页 > 知识库问答 >
问题:

使用Java将数据写入excel表单

荣晨朗
2023-03-14

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

但是为了得到表格格式,我必须在excel sheet中遵循以下步骤

我想让我的代码自动生成我想要的格式,而不需要我在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);

`

共有1个答案

蓝飞
2023-03-14

请参阅Apache POI-Microsoft文档的Java API。Apache POI项目的是Excel文件格式(xls和xlsx)的纯Java实现。通过POI可以直接写入/读取本机Excel文件。请参阅示例。

 类似资料:
  • 我有保存在表中的数据(我已经从一个文件中收集了它),我想用java把它写在Excel xls文件中 我只共享主类,因为它显示了我如何将数据保存在表中

  • 问题内容: 是否可以使用PHP fwrite()将内容附加到.xls文件? 当我使用fwrite()尝试此操作时,生成的文件在Excel 2007中导致错误消息。 我可以使用特定的分隔符来完成此操作吗? 没有第三方库,有可能吗? 问题答案: 您可以使用PhpSpreadsheet库读取现有的Excel文件,向其中添加新的行/列,然后将其写回为真实的Excel文件。 免责声明:我是该库的作者之一。

  • 问题描述 (Problem Description) 如何使用Java将数据写入Excel工作表。 解决方案 (Solution) 以下是使用Java将数据写入Excel工作表的程序。 import java.io.File; import java.io.FileOutputStream; import java.util.Map; import java.util.Set; import ja

  • 我正在尝试使用java selenium web驱动程序apache poi从excel读取和写入数据。但我的代码是从excel表中读取数据,但没有将数据写入excel表。我已经包含了poi-4.0.1中的所有jar文件,这是我的代码 }

  • 本文向大家介绍Python将列表数据写入文件(txt, csv,excel),包括了Python将列表数据写入文件(txt, csv,excel)的使用技巧和注意事项,需要的朋友参考一下 写入txt文件 写入csv文件 写入excel文件 以上所述是小编给大家介绍的Python将列表数据写入文件(txt, csv,excel)详解整合,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回

  • 我的excel工作表包含5行2列。我想在excel中再添加一列。但当我使用WorkbookFactory时,它显示错误。我导入了poi-3.8。jar和poi-ooxml-3.5-beta5。罐子它在线程“main”java中给出了错误异常。错误:未解决的编译问题:无法解决WorkbookFactory。请帮我做什么。