java实现 poi XWPFDocument 生成word(附段落、表格)

XWPFDocument 生成word,效果图如下:

在这里插入图片描述
详细代码如下:

public static void main(String[] args) throws Exception{
        {
            XWPFDocument doc = new XWPFDocument();
            //创建段落================标题
            XWPFParagraph paragraph = doc.createParagraph();
            //设置对齐
            paragraph.setAlignment(ParagraphAlignment.CENTER);
            //相同属性得区域
            XWPFRun run = paragraph.createRun();
            run.setColor("000000");
            run.setText("java生成word文档");
            run.setFontFamily("黑体");
            run.setFontSize(29);
            run.setBold(true);
            //换行
            run.addBreak();
            //表格标题
            paragraph = doc.createParagraph();
            paragraph.setAlignment(ParagraphAlignment.LEFT);
            run = paragraph.createRun();
            run.setFontSize(18);
            run.setText("一、表格标题");
            run.setFontFamily("黑体");
            run.addBreak();
            //3*4 表格
            XWPFTable table = doc.createTable(3, 4);
            //表格属性
            CTTblPr tablePr = table.getCTTbl().addNewTblPr();
            //表格宽度
            CTJc cTJc = tablePr.addNewJc();
            //居中
            cTJc.setVal(STJc.CENTER);
            //列宽自动分割
            CTTblWidth tableWidth = tablePr.addNewTblW();
            //设置表格宽度
            tableWidth.setType(STTblWidth.DXA);
            tableWidth.setW(BigInteger.valueOf(9072));
            //========================================第一行===================================
            //设置单元格宽度
            XWPFTableRow carRow1 = table.getRow(0);
            //设置单元格高度
            carRow1.setHeight(768);
            //设置单元格宽度
            carRow1.getCell(0).getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(2268));
            //以创建段落的方式给单元格赋值
            List<XWPFParagraph> paragraphs = carRow1.getCell(0).getParagraphs();
            paragraphs.get(0).setAlignment(ParagraphAlignment.LEFT);
            paragraphs.get(0).setVerticalAlignment(TextAlignment.CENTER);
            XWPFRun run1 = paragraphs.get(0).createRun();
            run1.setText("语言");
            run1.setFontSize(10);
            run1.setFontFamily("微软雅黑");
            run1.setColor("000000");
            run1.setBold(true);
            carRow1.getCell(0).setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);

            carRow1.getCell(1).getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(2268));
            paragraphs = carRow1.getCell(1).getParagraphs();
            paragraphs.get(0).setAlignment(ParagraphAlignment.CENTER);
            paragraphs.get(0).setVerticalAlignment(TextAlignment.CENTER);
            run1 = paragraphs.get(0).createRun();
            run1.setText("java");
            run1.setFontSize(10);
            run1.setFontFamily("微软雅黑");
            run1.setColor("000000");
            carRow1.getCell(1).setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);

            carRow1.getCell(2).getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(2268));
            paragraphs = carRow1.getCell(2).getParagraphs();
            paragraphs.get(0).setAlignment(ParagraphAlignment.LEFT);
            paragraphs.get(0).setVerticalAlignment(TextAlignment.CENTER);
            run1 = paragraphs.get(0).createRun();
            run1.setText("作者");
            run1.setFontSize(10);
            run1.setFontFamily("微软雅黑");
            run1.setColor("000000");
            run1.setBold(true);
            carRow1.getCell(2).setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);

            carRow1.getCell(3).getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(2268));
            paragraphs = carRow1.getCell(3).getParagraphs();
            paragraphs.get(0).setAlignment(ParagraphAlignment.CENTER);
            paragraphs.get(0).setVerticalAlignment(TextAlignment.CENTER);
            run1 = paragraphs.get(0).createRun();
            run1.setText("音乐土豆");
            run1.setFontSize(10);
            run1.setFontFamily("微软雅黑");
            run1.setColor("000000");
            carRow1.getCell(3).setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
            XWPFTableRow carRow2 = table.getRow(1);
            carRow2.setHeight(768);
            carRow2.getCell(0).getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(2268));
            paragraphs = carRow2.getCell(0).getParagraphs();
            paragraphs.get(0).setAlignment(ParagraphAlignment.LEFT);
            paragraphs.get(0).setVerticalAlignment(TextAlignment.CENTER);
            run1 = paragraphs.get(0).createRun();
            run1.setText("");
            carRow2.getCell(1).getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(2268));
            paragraphs = carRow2.getCell(1).getParagraphs();
            paragraphs.get(0).setAlignment(ParagraphAlignment.LEFT);
            paragraphs.get(0).setVerticalAlignment(TextAlignment.CENTER);
            run1 = paragraphs.get(0).createRun();
            run1.setText("");
            carRow2.getCell(2).getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(2268));
            paragraphs = carRow2.getCell(2).getParagraphs();
            paragraphs.get(0).setAlignment(ParagraphAlignment.LEFT);
            paragraphs.get(0).setVerticalAlignment(TextAlignment.CENTER);
            run1 = paragraphs.get(0).createRun();
            run1.setText("key");
            run1.setFontSize(10);
            run1.setFontFamily("微软雅黑");
            run1.setColor("000000");
            run1.setBold(true);
            carRow2.getCell(2).setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);

            carRow2.getCell(3).getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(2268));
            paragraphs = carRow2.getCell(3).getParagraphs();
            paragraphs.get(0).setAlignment(ParagraphAlignment.CENTER);
            paragraphs.get(0).setVerticalAlignment(TextAlignment.CENTER);
            run1 = paragraphs.get(0).createRun();
            run1.setText("value");
            run1.setFontSize(10);
            run1.setFontFamily("微软雅黑");
            run1.setColor("000000");
            carRow1.getCell(3).setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
            XWPFTableRow carRow3 = table.getRow(2);
            carRow3.setHeight(768);
            carRow3.getCell(0).getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(2268));
            paragraphs = carRow3.getCell(0).getParagraphs();
            paragraphs.get(0).setAlignment(ParagraphAlignment.LEFT);
            paragraphs.get(0).setVerticalAlignment(TextAlignment.CENTER);
            run1 = paragraphs.get(0).createRun();
            run1.setText("创作时间");
            run1.setFontSize(10);
            run1.setFontFamily("微软雅黑");
            run1.setColor("000000");
            run1.setBold(true);
            carRow3.getCell(0).setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);

            carRow3.getCell(1).getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(2268));
            paragraphs = carRow3.getCell(1).getParagraphs();
            paragraphs.get(0).setAlignment(ParagraphAlignment.LEFT);
            paragraphs.get(0).setVerticalAlignment(TextAlignment.CENTER);
            run1 = paragraphs.get(0).createRun();
            run1.setText("");

            carRow3.getCell(2).getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(2268));
            paragraphs = carRow3.getCell(2).getParagraphs();
            paragraphs.get(0).setAlignment(ParagraphAlignment.LEFT);
            paragraphs.get(0).setVerticalAlignment(TextAlignment.CENTER);
            run1 = paragraphs.get(0).createRun();
            run1.setText("2021年12月16日");
            run1.setFontSize(10);
            run1.setFontFamily("微软雅黑");
            run1.setColor("000000");
            run1.setBold(true);
            carRow3.getCell(2).setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);

            carRow3.getCell(3).getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(2268));
            paragraphs = carRow3.getCell(3).getParagraphs();
            paragraphs.get(0).setAlignment(ParagraphAlignment.LEFT);
            paragraphs.get(0).setVerticalAlignment(TextAlignment.CENTER);
            run1 = paragraphs.get(0).createRun();
            run1.setText("");
            //合并列
            horizontal(table,2,0,1);
            horizontal(table,2,2,3);
            //合并行
            vertically(table,0,0,1);
            //输出到D盘
            FileOutputStream os = new FileOutputStream("D:\\测试.docx");
            doc.write(os);
            os.close();
        }

XWPFTable 合并单元格

  • 4
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论
要使用Apache POI生成Word文档,你可以按照以下步骤进行操作: 1. 导入所需的POI库: ```xml <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>4.1.2</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.1.2</version> </dependency> ``` 2. 编写代码来创建并编辑Word文档: ```java import org.apache.poi.xwpf.usermodel.*; import java.io.FileOutputStream; import java.io.IOException; public class WordDocumentGenerator { public static void main(String[] args) { // 创建一个新的Word文档 XWPFDocument document = new XWPFDocument(); // 创建段落 XWPFParagraph paragraph = document.createParagraph(); // 创建文本运行 XWPFRun run = paragraph.createRun(); // 设置文本内容 run.setText("这是一个生成Word文档。"); // 添加换行符 paragraph.createRun().addBreak(); // 添加一个带有样式的文本 XWPFRun styledRun = paragraph.createRun(); styledRun.setText("这是带有样式的文本。"); styledRun.setBold(true); styledRun.setFontSize(14); // 保存文档 try { FileOutputStream out = new FileOutputStream("example.docx"); document.write(out); out.close(); System.out.println("生成Word文档成功!"); } catch (IOException e) { e.printStackTrace(); } } } ``` 上述代码创建了一个简单的Word文档,其中包含两个段落。第一个段落包含文本"这是一个生成Word文档。",第二个段落包含带有样式的文本"这是带有样式的文本。"。最后,将文档保存为名为`example.docx`的文件。你可以根据需要添加更多的段落表格、图片以及其他格式设置。记得根据实际情况进行异常处理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

音乐土豆

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值