如何使用Java在Word中插入表格
序言
各位好啊,我是会编程的蜗牛,作为java开发者,有时候需要操作word或者excel。这里面比较常用的框架是POI,我之前用过POI来读写excel,也有用过alibaba easy excel来读写excel,还都挺好用的。
不过对于word的操作就比较少了,基本的文本操作用poi应该也是可以实现的。但对于复杂的操作,像在word里面插入表格等,就有点力不从心了。
今天介绍一款github上开源的框架,Free Spire.Doc,它对于word的操作还是很方便,没有像POI那样需要写一大堆代码,非常简洁高效。
本文来源:infoQ,作者:Geek_249eec,整理:会编程的蜗牛
当我们在编辑 Word 文档时,如果遇到大量数据需要体现,可以选择直接在 Word 文档中创建表格。将数据应用于表格内,不仅能够简化文档语言,而且也可以使数据内容更加清晰、直观。下面我就将使用Free Spire.Doc for Java演示如何在 Java 中创建 Word 表格。
安装 Spire.Doc.Jar
方法一:
如果您使用的是 maven,可以通过添加以下代码到项目的 pom.xml 文件中,将 JAR 文件导入到应用程序中。
<repositories> <repository> <id>com.e-iceblue</id> <url>https://repo.e-iceblue.cn/repository/maven-public/</url> </repository> </repositories> <dependencies> <dependency> <groupId>e-iceblue</groupId> <artifactId>spire.doc.free</artifactId> <version>5.2.0</version> </dependency> </dependencies>
方法二:
如果您没有使用 maven,则可以从https://www.e-iceblue.cn/Downloads/Free-Spire-Doc-JAVA.html下载 Free Spire.Doc for Java,找到 lib 文件夹下的 Spire.Doc.jar 并进行解压。
然后在 IDEA 中创建一个新项目,依次点击“文件”(File),“项目结构”(Project Structure),“组件”(Modules),“依赖项”(Dependencies),再点击右方绿色“+”下的第一个选项“JAR 文件或路径”(JARs or Directories),找到解压后的 Spire.Doc.jar 文件,点击确认,将其导入到项目中。
在 Word 中创建表格:
具体操作步骤:
- 创建一个 Document 对象,并向其添加一个节。
- 将标题行和其他行的数据分别存储在一维字符串数组和二维字符串数组中。
- 使用 Section.addTable() 方法将表格添加到节。
- 将数据插入标题行,并设置行格式,包括行高、背景颜色和文本对齐方式。
- 将数据插入其余行,并对这些行应用格式。
- 使用 Document.saveToFile() 方法保存文件。
相关代码:
import com.spire.doc.*; import com.spire.doc.documents.*; import com.spire.doc.fields.TextRange; import java.awt.*; public class CreateTable { public static void main(String[] args) { //创建一个Document对象 Document document = new Document(); //添加一个节 Section section = document.addSection(); //定义表格数据 String[] header = { "学号", "姓名", "性别", "班级", "成绩" }; String[][] data = { new String[]{"0105", "李雷", "男", "1", "88"}, new String[]{"0721", "赵文", "女", "7", "92"}, new String[]{"1131", "陈华", "女", "11", "91"}, new String[]{"0418", "宋野", "男", "4", "95"}, new String[]{"0513", "韩梅", "女", "5", "94"}, }; //添加表格 Table table = section.addTable(true); table.resetCells(data.length + 1, header.length); //将第一行设置为表格标题 TableRow row = table.getRows().get(0); row.isHeader(true); row.setHeight(20); row.setHeightType(TableRowHeightType.Exactly); row.getRowFormat().setBackColor(Color.gray); for (int i = 0; i < header.length; i++) { row.getCells().get(i).getCellFormat().setVerticalAlignment(VerticalAlignment.Middle); Paragraph p = row.getCells().get(i).addParagraph(); p.getFormat().setHorizontalAlignment(HorizontalAlignment.Center); TextRange txtRange = p.appendText(header[i]); txtRange.getCharacterFormat().setBold(true); } //将数据添加到其余行 for (int r = 0; r < data.length; r++) { TableRow dataRow = table.getRows().get(r + 1); dataRow.setHeight(25); dataRow.setHeightType(TableRowHeightType.Exactly); dataRow.getRowFormat().setBackColor(Color.white); for (int c = 0; c < data[r].length; c++) { dataRow.getCells().get(c).getCellFormat().setVerticalAlignment(VerticalAlignment.Middle); dataRow.getCells().get(c).addParagraph().appendText(data[r][c]); } } //设置单元格的背景颜色 for (int j = 1; j < table.getRows().getCount(); j++) { if (j % 2 == 0) { TableRow row2 = table.getRows().get(j); for (int f = 0; f < row2.getCells().getCount(); f++) { row2.getCells().get(f).getCellFormat().setBackColor(new Color(173, 216, 230)); } } } //保存结果文件 document.saveToFile("result.docx", FileFormat.Docx_2013); } }
效果图展示:

总结
使用Free Spire.Doc for Java,可以大大简化我们操作word,是一款非常值得推荐的开源框架,我之前就用这个框架解决了一个数据填充的问题,这个用POI写了半天也没搞定,用这个框架几分钟就搞定了,推荐。
推荐阅读 点击标题可跳转

我整理的干货,回复【JAVA核心】获取《JAVA核心面试知识整理》
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· 上周热点回顾(2.17-2.23)
· 如何使用 Uni-app 实现视频聊天(源码,支持安卓、iOS)
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)