127 apache poi3.11 写 word 中内嵌 表格换行的输出

前言

最近一个 地方的需求

需要再 单元格中 输出带换行回车的内容

然后 找了一下 相关 demo, 基本上是 使用 \r\n 或者 <br> 或者 <w:br/> 来进行处理

但是 测试了一通 之后, 发现 都不行

来自于网上的 demo [不可用]

复制代码
/**
 * Test18WordBreakDemo
 *
 * @author Jerry.X.He <970655147@qq.com>
 * @version 1.0
 * @date 2024-05-14 09:07
 */
public class Test18WordBreakDemo {

    public static void main(String[] args) throws Exception {
        XWPFDocument document = new XWPFDocument();
        XWPFTable table = document.createTable();
        XWPFTableRow row = table.getRow(0);
        XWPFTableCell cell = row.createCell();

        String contentWithBreak = "First line<br>Second line";
        // 替换HTML的<br>为对应的Unicode换行符也可以,但推荐使用POI对HTML的支持
        // String contentWithBreak = "First line\nSecond line";

        cell.getParagraphs().get(0).setAlignment(ParagraphAlignment.LEFT);
        cell.getParagraphs().get(0).setSpacingBefore(200);
        cell.getParagraphs().get(0).setSpacingAfter(200);
        cell.setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
        CTShd ctshd = cell.getCTTc().addNewTcPr().addNewShd();
        ctshd.setColor("auto");
        ctshd.setFill("99CCFF");
        cell.getParagraphs().get(0).setStyle("TableContents");

        cell.setText(contentWithBreak);
        cell.getParagraphs().get(0).setStyle("YourCustomStyle");

        FileOutputStream out = new FileOutputStream("Test18WordBreakDemo.docx");
        document.write(out);
        out.close();
    }

}

<br/> 输出结果如下

或者 \r\n 输出如下, 但是 不是期望的效果

正确实现效果的 demo

后面看了一下 正确的结构, 仿写 代码如下

复制代码
/**
 * Test18WordBreakDemo
 *
 * @author Jerry.X.He <970655147@qq.com>
 * @version 1.0
 * @date 2024-05-14 09:07
 */
public class Test18WordBreakDemo {

    public static void main(String[] args) throws Exception {
        // 创建Word文档对象
        XWPFDocument document = new XWPFDocument();

        // 创建表格
        XWPFTable table = document.createTable();

        // 添加行
        XWPFTableRow row = table.getRow(0);

        // 添加单元格
        XWPFTableCell cell = row.createCell();

        setTextInCell(cell, Arrays.asList("第一行", "第二行", "第三行"));

        // 保存文档
        FileOutputStream out = new FileOutputStream("./WordTableExample.docx");
        document.write(out);
        out.close();
    }

    public static void setTextInCell(XWPFTableCell cell, List<String> textList) {
        for (String text : textList) {
            XWPFParagraph paragraph = cell.addParagraph();
            paragraph.setAlignment(ParagraphAlignment.CENTER);
            XWPFRun run = paragraph.createRun();
            run.setText(text);
        }
    }

}

输出结果如下

相关推荐
513495928 小时前
在Vue.js项目中使用docx和file-saver实现Word文档导出
前端·vue.js·word
Access开发易登软件10 小时前
数据处理中的两大基石:何时选择Excel,何时考虑Access
数据库·信息可视化·excel·vba·access
开开心心就好11 小时前
PDF密码移除工具,免费解除打印编辑复制权限
java·网络·windows·websocket·pdf·电脑·excel
缺点内向13 小时前
C# 高效统计 Word 文档字数:告别手动,拥抱自动化
c#·自动化·word
weixin_4624462314 小时前
Python 实战:将 HTML 表格一键导出为 Excel(xlsx)
linux·python·excel·pandas
wtsolutions14 小时前
Sheet-to-Doc Supports JSON and JSONL: New Way to Batch Generate Word Documents
json·word·batch
Data-Miner14 小时前
Excel-Agent永久买断,一款结合AI Agent的excel数据处理智能体
人工智能·excel
weixin_462446231 天前
Python 使用 openpyxl 从 URL 读取 Excel 并获取 Sheet 及单元格样式信息
python·excel·openpyxl
wtsolutions1 天前
MCP Server Integration - JSON to Excel for AI and Automation
json·excel
开开心心就好1 天前
音频编辑工具,多端支持基础剪辑易操作
java·网络·windows·java-ee·电脑·maven·excel