1

我尝试在.doc文件中创建表。doc在使用 poi创建表时我没有得到太多帮助。我尝试下面的代码,但没有奏效。它没有像下面的屏幕截图那样创建表格。

在此处输入图像描述

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();
    }

目前我使用 POI 3.11 版本。

您的帮助将不胜感激

4

0 回答 0