qq_45866015 2023-08-24 15:14 采纳率: 97.6%
浏览 4
已结题

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

  • 写回答

2条回答 默认 最新

  • 家有娇妻张兔兔 Java领域优质创作者 2023-08-24 15:49
    关注

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

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 9月1日
  • 已采纳回答 8月24日
  • 创建了问题 8月24日

悬赏问题

  • ¥15 有偿求苍穹外卖环境配置
  • ¥15 代码在keil5里变成了这样怎么办啊,文件图像也变了,
  • ¥20 Ue4.26打包win64bit报错,如何解决?(语言-c++)
  • ¥15 clousx6整点报时指令怎么写
  • ¥30 远程帮我安装软件及库文件
  • ¥15 关于#自动化#的问题:如何通过电脑控制多相机同步拍照或摄影(相机或者摄影模组数量大于60),并将所有采集的照片或视频以一定编码规则存放至规定电脑文件夹内
  • ¥20 深信服vpn-2050这台设备如何配置才能成功联网?
  • ¥15 Arduino的wifi连接,如何关闭低功耗模式?
  • ¥15 Android studio 无法定位adb是什么问题?
  • ¥15 C#连接不上服务器,