I have an array that I want to retrieve the values from and insert them into excel using Jxl, one value into one row eg:
1| blah blah blah
2| blah blah blah
3| blah blah blah
I can retrieve the values from the array as is shown in the code below, but inserting them into rows is proving difficult for me....the best I have managed through various attempts is inserting the right number of rows but all of the same value (the last value stored in the array).
private static void InsertStrings() throws RowsExceededException, WriteException {
try {
System.out.println("Starting Write to Excel");
WritableWorkbook workbook = Workbook.createWorkbook(new File("C:\\Users\\Jason\\Documents\\Development\\Seaport Crawler\\CrawlerResults.xls"));
WritableSheet sheet = workbook.createSheet("Results", 0);
Label label = new Label(0, 0, "Notices to Mariners");
sheet.addCell(label);
for ( int indx = 0; indx < arr_Heading.size(); indx++)
{
/********HEADING and TITLE ARRAY*********/
String heading;
int rowValue = indx;
heading = arr_Heading.get(rowValue);
//Editing the Descriptions
if (heading.contains("-")) {
// Split it
String string = heading;
String[] parts = string.split("-",2);
String part1 = parts[0]; // Heading
String part2 = parts[1]; // Title
//String part1s = part1.replaceAll("\\s+","");//Replace Spaces with none
// String part2s = part2.replaceAll("\\s+","");
System.out.println("row Value........" + rowValue + " Heading... " + part1 + " Title... " + part2);
//Insert Strings to Excel Workbook
Label ntmHeading = new Label(2, indx, part1);
sheet.addCell(ntmHeading);
}else{
//something
}
// All sheets and cells added. Now write out the workbook
workbook.write();
workbook.close();
}
} catch (IOException e) {
System.out.println("WRITE TO EXCEL FAILED");
e.printStackTrace();
}
Thanks
Write the following statements outside the for loop:
workbook.write();
workbook.close();
Do some adjustments for try catch block/throws and it works.