java poi生成word 并插入 表格

spring boot 项目

java:

public static void main(String[] args) {
    // 文档生成方法
    XWPFDocument doc = new XWPFDocument();
    XWPFParagraph p1 = doc.createParagraph(); // 创建段落
    XWPFRun r1 = p1.createRun(); // 创建段落文本
    r1.setText("目录"); // 设置文本
    FileOutputStream out = null; // 创建输出流
    try {
        // 向word文档中添加内容
        XWPFParagraph p3 = doc.createParagraph(); // 创建段落
        XWPFRun r3 = p3.createRun(); // 创建段落文本
        r3.addTab();// tab
        r3.addBreak();// 换行
        r3.setBold(true);
        XWPFParagraph p2 = doc.createParagraph(); // 创建段落
        XWPFRun r2 = p2.createRun(); // 创建段落文本
        // 设置文本
        r2.setText("表名");
        r2.setFontSize(14);
        r2.setBold(true);
        XWPFTable table1 = doc.createTable(8, 10);
        table1.setWidthType(TableWidthType.AUTO);

        // 获取到刚刚插入的行
        XWPFTableRow row1 = table1.getRow(0);
        // 设置单元格内容
        row1.getCell(0).setText("字段名");
        row1.getCell(1).setText("字段说明");
        row1.getCell(2).setText("数据类型");
        row1.getCell(3).setText("长度");
        row1.getCell(4).setText("索引");
        row1.getCell(5).setText("是否为空");
        row1.getCell(6).setText("主键");
        row1.getCell(7).setText("外键");
        row1.getCell(8).setText("缺省值");
        row1.getCell(9).setText("备注");

        doc.setTable(0, table1);
        String filePath = "F:\\simple.docx";
        out = new FileOutputStream(new File(filePath));
        doc.write(out);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {

        if (out != null) {
            try {
                out.close();
            } catch (IOException e) {

                e.printStackTrace();
            }
        }

    }

}

 

pom.xml:

 

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <!--<dependency>-->
        <!--<groupId>org.apache.poi</groupId>-->
        <!--<artifactId>poi-ooxml</artifactId>-->
        <!--<version>3.14</version>-->
    <!--</dependency>-->

    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-excelant</artifactId>
        <version>3.14</version>
    </dependency>

    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-examples</artifactId>
        <version>4.0.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml-schemas</artifactId>
        <version>3.10-FINAL</version>
    </dependency>

    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
        <version>4.0.0</version>
    </dependency>

   <!--Apache poi  在word中的表格中插入表格,图片等操作  开始-->
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi</artifactId>
        <version>4.0.0</version>
    </dependency>

    <dependency>
        <groupId>org.apache.xmlbeans</groupId>
        <artifactId>xmlbeans</artifactId>
        <version>3.1.0</version>
    </dependency>

    <dependency>
        <groupId>com.deepoove</groupId>
        <artifactId>poi-tl</artifactId>
        <version>1.6.0-beta1</version>
    </dependency>
    <!--结束-->


    <!--<dependency>-->
        <!--<groupId>org.apache.xmlbeans</groupId>-->
        <!--<artifactId>xmlbeans</artifactId>-->
        <!--<version>2.6.0</version>-->
    <!--</dependency>-->
    <dependency>
        <groupId>org.junit.platform</groupId>
        <artifactId>junit-platform-commons</artifactId>
        <version>RELEASE</version>
        <scope>compile</scope>
    </dependency>

    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
        <version>3.4</version>
    </dependency>

    <dependency>
        <groupId>joda-time</groupId>
        <artifactId>joda-time</artifactId>
        <version>2.9.4</version>
    </dependency>

    <dependency>
        <groupId>jacob</groupId>
        <artifactId>jacob</artifactId>
        <scope>system</scope>
        <systemPath>${basedir}/lib/jacob.jar</systemPath>
    </dependency>

    <dependency>
        <groupId>ooxml-schemas</groupId>
        <artifactId>ooxml-schemas</artifactId>
        <scope>system</scope>
        <systemPath>${basedir}/lib/ooxml-schemas-1.1.jar</systemPath>
    </dependency>

    <dependency>
        <groupId>spire.doc.free-2.7.3</groupId>
        <artifactId>spire.doc.free-2.7.3</artifactId>
        <scope>system</scope>
        <systemPath>${basedir}/lib/spire.doc.free-2.7.3.jar</systemPath>
    </dependency>

</dependencies>

 

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Java中,可以使用Apache POI库来生成Word文档并插入表格和图片。下面是一个简单的示例代码: 首先,需要引入Apache 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> ``` 然后,可以使用以下代码生成一个Word文件并插入表格和图片: ```java import org.apache.poi.util.IOUtils; import org.apache.poi.xwpf.usermodel.*; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; public class WordGenerator { public static void main(String[] args) { try { // 创建一个新的Word文档 XWPFDocument document = new XWPFDocument(); // 创建一个表格 XWPFTable table = document.createTable(3, 3); // 往表格中添加内容 table.getRow(0).getCell(0).setText("姓名"); table.getRow(0).getCell(1).setText("性别"); table.getRow(0).getCell(2).setText("年龄"); table.getRow(1).getCell(0).setText("张三"); table.getRow(1).getCell(1).setText("男"); table.getRow(1).getCell(2).setText("20"); table.getRow(2).getCell(0).setText("李四"); table.getRow(2).getCell(1).setText("女"); table.getRow(2).getCell(2).setText("22"); // 插入一张图片 InputStream imageStream = new FileInputStream("path/to/image.jpg"); XWPFParagraph paragraph = document.createParagraph(); XWPFRun run = paragraph.createRun(); run.addPicture(imageStream, XWPFDocument.PICTURE_TYPE_JPEG, "image.jpg", Units.toEMU(200), Units.toEMU(200)); imageStream.close(); // 保存Word文档 FileOutputStream out = new FileOutputStream("path/to/output.docx"); document.write(out); out.close(); System.out.println("Word文档生成成功!"); } catch (Exception e) { e.printStackTrace(); } } } ``` 需要注意的是,上述代码中的图片路径和输出路径需要根据实际情况修改。另外,还需要根据实际需求来调整表格的行数、列数以及单元格内容。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值