poi+java在word指定位置生成简单表格

本文介绍如何利用Java的Apache POI库在Word文档的指定位置生成简单表格,包括配置pom.xml,创建工具类,以及调用方法实现模板和展示效果。
摘要由CSDN通过智能技术生成

poi+java在word指定位置生成简单表格

pom.xml

    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi</artifactId>
        <version>3.17</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
        <version>3.17</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml-schemas -->
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml-schemas</artifactId>
        <version>3.17</version>
    </dependency>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-excelant</artifactId>
        <version>3.17</version>
    </dependency>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-scratchpad</artifactId>
        <version>3.17</version>
    </dependency>

工具类

package com.example.demo2.utils;

import org.apache.poi.POIXMLDocument;
import org.apache.poi.xwpf.usermodel.*;
import org.apache.xmlbeans.XmlCursor;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;

import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.math.BigInteger;
import java.util.List;

public class InsertExcelByKeyInWord {
   
    //创建一个表格插入到key标记的位置
    public <T>void exportBg(List<String> head,List<T> data,String srcPath,String targetPath,String key) {
   
        XWPFDocument doc = null;
        try {
   
            doc = new XWPFDocument(POIXMLDocument.openPackage(srcPath));
            List<XWPFParagraph> paragraphList = doc.getParagraphs();
            
  • 4
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用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. 编写代码来创建表格并添加内容: ```java import org.apache.poi.xwpf.usermodel.*; import java.io.FileOutputStream; import java.io.IOException; public class WordTableGenerator { public static void main(String[] args) { XWPFDocument document = new XWPFDocument(); // 创建一个表格 XWPFTable table = document.createTable(); // 添加表头 XWPFTableRow headerRow = table.getRow(0); headerRow.getCell(0).setText("姓名"); headerRow.addNewTableCell().setText("年龄"); headerRow.addNewTableCell().setText("性别"); // 添加数据行 XWPFTableRow dataRow1 = table.createRow(); dataRow1.getCell(0).setText("张三"); dataRow1.getCell(1).setText("25"); dataRow1.getCell(2).setText("男"); XWPFTableRow dataRow2 = table.createRow(); dataRow2.getCell(0).setText("李四"); dataRow2.getCell(1).setText("30"); dataRow2.getCell(2).setText("女"); // 保存文档 try { FileOutputStream out = new FileOutputStream("example.docx"); document.write(out); out.close(); System.out.println("生成Word文档成功!"); } catch (IOException e) { e.printStackTrace(); } } } ``` 上述代码创建了一个包含表头和两行数据的表格,并将其保存为名为`example.docx`的Word文档。你可以根据需要添加更多的表格行和单元格,并设置样式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值