I am writing a program which writes data to an excel sheet sort of a table format. My code gives me all the details as I want.
But in order to get the table format I have to follow these steps in excel sheet
(select a column -> data -> text to columns -> select the option 'delimited' -> click button 'next' -> select the option 'comma'-> click button 'next'-> click button 'finish').
I want my code to automatically generate the format I want without me doing the above shown steps in the excel sheet. Can anyone help me in this? Thanks in advance. Below shown is my code.
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); `