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

时间:2015-04-24 04:39:44

标签: java excel

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

但是为了获得表格格式,我必须在excel表格中按照以下步骤

(选择一列 - >数据 - >文字到列 - >选择选项'分隔' - >点击按钮' next' - >选择选项'逗号' - >点击按钮'下一步' - >点击按钮'完成')。

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

`

2 个答案:

答案 0 :(得分:1)

Apache POI - the Java API for Microsoft Documents。 Apache POI Project是Excel文件格式(xls和xlsx)的纯Java实现。您可以通过POI直接写入/读取本机Excel的文件。请参阅examples

答案 1 :(得分:0)

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