The output format should be like this in the doc:
This is the input format of the excel sheet:
I'm trying to read an Excel sheet and trying to print the information in a systematical order in the Word document. I can read from the Excel sheet but couldn't print it in the doc file. What should I do?
It's showing the last line of the Excel file i.e, its only over-writing the doc file but not appending it. Is there a way to append this file?
public static void main(String[] args) throws Exception {
File src = new File("convertion java.xls");
FileInputStream fis = new FileInputStream(src);
HSSFWorkbook wb = new HSSFWorkbook(fis);
HSSFSheet sheet1 = wb.getSheetAt(0);
String[] header = new String[4];
for (int k = 0; k < 4; k++) {
header[k] = sheet1.getRow(0).getCell(k).getStringCellValue();
}
FileOutputStream out = new FileOutputStream(new File("output.docx"));
for (int j = 1; j < 5; j++) {
for (int i = 0; i < 4; i++) {
result = sheet1.getRow(j).getCell(i).getStringCellValue();
doc = header[i] + " = " + result;
System.out.println(doc);
XWPFDocument document = new XWPFDocument();
XWPFParagraph paragraph = document.createParagraph();
XWPFRun paragraphOneRunOne = paragraph.createRun();
paragraphOneRunOne.setText(doc);
document.write(out);
}
System.out.println();
}
wb.close();
out.close();
}}
I expect the output to append the doc file but not to over-write it.