1

I try to create table in .doc file. I didn't getting much help to create table in doc using poi. I try below code but it didn't work. It's didn't create table like below screenshot.

enter image description here

private static void insertTable() throws FileNotFoundException, IOException {
        HWPFDocument doc = new HWPFDocument(new FileInputStream("/home/ikadmin/Downloads/in.doc"));
        int rows = 3, columns = 3;
        Range range = doc.getRange();
        Table table = range.insertTableBefore((short) columns, rows);
        for (int rowIdx = 0; rowIdx < table.numRows(); rowIdx++) {
            TableRow row = table.getRow(rowIdx);
            for (int colIdx = 0; colIdx < row.numCells(); colIdx++) {
                TableCell cell = row.getCell(colIdx);

                Paragraph par = cell.getParagraph(0);

                par.insertBefore("" + (rowIdx * row.numCells() + colIdx));
            }
        }
        String newFilePath = "/home/ikadmin/Downloads/out.doc";
        OutputStream out = new FileOutputStream(newFilePath);
        doc.write(out);
        out.flush();
        out.close();
    }

currently I use POI 3.11 version.

Your help will be highly appreciated

Divyesh Kanzariya
  • 3,629
  • 3
  • 43
  • 44

0 Answers0