Search code examples
javaexcelapache-poidocumentation

I can't print the info in doc file in a sequence when I read the inf from the Excel sheet


The output format should be like this in the doc:

Document Output

This is the input format of the excel sheet:

Excel Output

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.


Solution

  • You create a new XWPFDocument for each cell found in the Excel sheet. That cannot lead to the result you wanted. To get your wanted result, only one XWPFDocument is needed which then gets filled with paragraphs according the contents of the found Excel cells.

    And for the Excel part, you should use the org.apache.poi.ss.usermodel.* rather than HSSF since this is more general and also able reading XSSF (*.xlsx) then.

    And you should not rely on getStringCellValue since not allExcelcell contents must always be text. Instead you should use DataFormatter to get the cell contents from the Excel cells.

    Complete example:

    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    
    import org.apache.poi.ss.usermodel.*;
    import org.apache.poi.xwpf.usermodel.*;
    
    class ReadExcelWriteWord {
    
     public static void main(String[] args) throws Exception {
    
      DataFormatter formatter = new DataFormatter();
    
      Workbook workbook = WorkbookFactory.create(new FileInputStream("convertion java.xls"));
      FileOutputStream out = new FileOutputStream("output.docx");
      XWPFDocument document = new XWPFDocument();
    
      Sheet sheet = workbook.getSheetAt(0);
      Row row = null;
      Cell cell = null;
      XWPFParagraph paragraph = null;
      XWPFRun run = null;
    
      String[] header = new String[4];
      row = sheet.getRow(0);
      if (row != null) {
       for (int k = 0; k < 4; k++) {
        cell = row.getCell(k);
        header[k] = formatter.formatCellValue(cell);
       }
      }
    
      String result = "";
      String doc = "";
      for (int j = 1; j < 5; j++) {
       row = sheet.getRow(j);
       if (row != null) {
        for (int i = 0; i < 4; i++) {
         cell = row.getCell(i);
         result = formatter.formatCellValue(cell);
         doc = header[i] + " = " + result;
         System.out.println(doc);
         paragraph = document.createParagraph();
         run = paragraph.createRun();
         run.setText(doc);
        }
       }
       paragraph = document.createParagraph();
       System.out.println();
      }
    
      workbook.close();
      document.write(out);
      out.close();
      document.close();
     }
    }