Java poi 插入doc表格,左侧多一列的问题,


    public static void main(String[] args) {
        Map<String, List<List<List<String>>>> r2 = ExcelToConsole.getData(); // Replace with your actual data

        String outputFilePath = "src/main/resources/output.docx"; // Replace with your desired output file path

        try (FileOutputStream outputStream = new FileOutputStream(outputFilePath)) {
            XWPFDocument document = new XWPFDocument();

            // Iterate through the map and write table for each entry
            for (Map.Entry<String, List<List<List<String>>>> entry : r2.entrySet()) {
                String key = entry.getKey();
                List<List<List<String>>> value = entry.getValue();

                // Create a paragraph with the key (file name)
                XWPFParagraph paragraph = document.createParagraph();
                XWPFRun run = paragraph.createRun();
                run.setText("File Name: " + key);

                // Iterate through the value (list of tables) and write each table
                for (List<List<String>> tableData : value) {
                    XWPFTable table = document.createTable();
                    writeDataToTable(table, tableData);
                }
            }

            // Write the document content to the output file
            document.write(outputStream);
            System.out.println("Tables written to Word document successfully.");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void writeDataToTable(XWPFTable table, List<List<String>> data) {
        for (List<String> row : data) {
            XWPFTableRow tableRow = table.createRow();
            for (String cellValue : row) {
                XWPFTableCell cell = tableRow.createCell();
                setCellTextStyle(cell, cellValue); // Apply custom style to cell text
            }
        }
    }

    public static void setCellTextStyle(XWPFTableCell cell, String text) {
        XWPFParagraph paragraph = cell.addParagraph();
        XWPFRun run = paragraph.createRun();
        run.setText(text);
        run.setFontSize(12); // Set font size to 12 points (小四)
        run.setColor("FF0000"); // Set font color to red
        run.setFontFamily("宋体"); // Set font family
    }

帮看下有啥问题,为什么插入的时候,左面多了一列。

img

没有指定行和列吧
你的数据和默认的行高列宽不匹配导致的
在创建表的时候根据你的数据指定下行和列再试试

img

【以下回答由 GPT 生成】

问题分析: 目前的代码逻辑是根据传入的数据生成表格,并将表格写入到doc文件中,但在每次创建新的表格时并未指定表格的列数。因此,每创建一个新的表格,会默认添加一列。所以在代码中需要指定表格的列数,并确保每个表格都有相同的列数。

解决方案: 1. 首先,在writeDataToTable方法中添加一个参数,用于指定表格的列数。修改方法如下:

public static void writeDataToTable(XWPFTable table, List<List<String>> data, int numCols) {
   // ...
}
  1. 修改main方法中的调用writeDataToTable的部分,将表格列数作为参数传递进去。修改的部分如下:
for (List<List<String>> tableData : value) {
    XWPFTable table = document.createTable(tableData.size(), numCols); // 指定表格的行数和列数
    writeDataToTable(table, tableData);
}
  1. 运行代码,生成的doc文件中的表格左侧将不再多出一列。

完整代码如下:

public static void main(String[] args) {
    Map<String, List<List<List<String>>>> r2 = ExcelToConsole.getData(); // Replace with your actual data

    String outputFilePath = "src/main/resources/output.docx"; // Replace with your desired output file path
    int numCols = 0; // 表格列数

    try (FileOutputStream outputStream = new FileOutputStream(outputFilePath)) {
        XWPFDocument document = new XWPFDocument();

        // Iterate through the map and write table for each entry
        for (Map.Entry<String, List<List<List<String>>>> entry : r2.entrySet()) {
            String key = entry.getKey();
            List<List<List<String>>> value = entry.getValue();

            // Create a paragraph with the key (file name)
            XWPFParagraph paragraph = document.createParagraph();
            XWPFRun run = paragraph.createRun();
            run.setText("File Name: " + key);

            // Iterate through the value (list of tables) and write each table
            for (List<List<String>> tableData : value) {
                if (numCols == 0) { // 第一次创建表格时获取列数
                    numCols = tableData.get(0).size();
                }
                XWPFTable table = document.createTable(tableData.size(), numCols); // Specify the number of rows and columns
                writeDataToTable(table, tableData, numCols);
            }
        }

        // Write the document content to the output file
        document.write(outputStream);
        System.out.println("Tables written to Word document successfully.");
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public static void writeDataToTable(XWPFTable table, List<List<String>> data, int numCols) {
    for (List<String> row : data) {
        XWPFTableRow tableRow = table.createRow();
        for (String cellValue : row) {
            XWPFTableCell cell = tableRow.createCell();
            setCellTextStyle(cell, cellValue); // Apply custom style to cell text
        }
    }
}

这样,生成的doc文件中的表格将不再有多余的一列。



【相关推荐】



如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^