Java 在Word创建表格
表格作为一种可视化交流模式及组织整理数据的手段,在各种场合及文档中应用广泛。常见的表格可包含文字、图片等元素,我们操作表格时可以插入图片、写入文字及格式化表格样式等。下面,将通过Java编程在Word文档中创建表格并实现格式化操作,包括设置字体、字号、字体颜色、字体粗细等,设置单元格对齐方式、单元格背景色、单元格合并、设置表格边框样式、插入图片等。
使用工具:Free Spire.Doc for Java 2.0.0 (免费版)
Jar文件导入
方法1:首先通过官网获取jar包。下载控件包并解压。
导入步骤:在程序中新建一个directory目录,并命名(本示例中命名为lib);将控件包lib文件夹下的Spire.Doc.jar文件(如下图1)复制到程序中新建的目录下。复制jar文件后,鼠标右键点击jar文件,选择”Add as Library”。完成导入(如下图2)。
图1:

图2:

方法2:通过maven导入。参考导入方法。
Java代码示例(供参考)
Step 1: 创建文档
Document doc = new Document();
Section sec = doc.addSection();
Step 2:声明数组内容
//声明数组内容
String[] header = {"班级","姓名","性别", "学号", "专业成绩"};
String[][] data =
{
new String[]{"一班","王丽", "女", "Y1256486", "138"},
new String[]{"一班","洪菲菲", "女", "Y5425875", "134"},
new String[]{"二班","刘洋", "男", "B1546258", "141"},
new String[]{"三班","冯刚", "男", "B1542367", "136"},
new String[]{"三班","刘思源", "男", "Z1263547", "133"},
};
Step 3:添加表格并写入数据
//添加表格
Table table = sec.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.ORANGE);
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 range1 = p.appendText(header[i]);
range1.getCharacterFormat().setFontName("Arial");
range1.getCharacterFormat().setFontSize(12f);
range1.getCharacterFormat().setBold(true);
range1.getCharacterFormat().setTextColor(Color.white);
}
//写入剩余组内容到表格,并格式化数据
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);
TextRange range2 = dataRow.getCells().get(c).addParagraph().appendText(data[r][c]);
range2.getOwnerParagraph().getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
range2.getCharacterFormat().setFontName("Arial");
range2.getCharacterFormat().setFontSize(10f);
}
}
Step 4:合并单元格
table.applyVerticalMerge(0,1,2);
table.applyVerticalMerge(0,4,5);
Step 5:插入图片到单元格
DocPicture dp = table.getRows().get(1).getCells().get(0).addParagraph().appendPicture("1.png");
dp.getOwnerParagraph().getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
Step 6:设置单元格背景色
for (int j = 1; j < table.getRows().getCount(); j++) {
if (j % 2 == 0) {
TableRow row2 = table.getRows().get(j);
for (int f = 1; f < row2.getCells().getCount(); f++) {
row2.getCells().get(f).getCellFormat().setBackColor(new Color(144,238,144));
}
}
}
Step 7:设置表格边框样式
table.getTableFormat().getBorders().setBorderType(BorderStyle.Thick_Thin_Large_Gap);
Step 8: 保存文档
doc.saveToFile("CreateTable.docx", FileFormat.Docx_2013);
表格创建效果:

全部代码:
import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.DocPicture;
import com.spire.doc.fields.TextRange;
import java.awt.*; public class CreateTable {
public static void main(String[] args){
//创建Document对象
Document doc = new Document();
Section sec = doc.addSection(); //声明数组内容
String[] header = {"班级","姓名","性别", "学号", "专业成绩"};
String[][] data =
{
new String[]{"一班","王丽", "女", "Y1256486", "138"},
new String[]{"一班","洪菲菲", "女", "Y5425875", "134"},
new String[]{"二班","刘洋", "男", "B1546258", "141"},
new String[]{"三班","冯刚", "男", "B1542367", "136"},
new String[]{"三班","刘思源", "男", "Z1263547", "133"},
}; //添加表格
Table table = sec.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.ORANGE);
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 range1 = p.appendText(header[i]);
range1.getCharacterFormat().setFontName("Arial");
range1.getCharacterFormat().setFontSize(12f);
range1.getCharacterFormat().setBold(true);
range1.getCharacterFormat().setTextColor(Color.white);
} //写入剩余组内容到表格,并格式化数据
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);
TextRange range2 = dataRow.getCells().get(c).addParagraph().appendText(data[r][c]);
range2.getOwnerParagraph().getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
range2.getCharacterFormat().setFontName("Arial");
range2.getCharacterFormat().setFontSize(10f);
}
} //纵向合并指定单元格
table.applyVerticalMerge(0,1,2);
table.applyVerticalMerge(0,4,5); //插入图片到指定单元格
DocPicture dp = table.getRows().get(1).getCells().get(0).addParagraph().appendPicture("1.png");
dp.getOwnerParagraph().getFormat().setHorizontalAlignment(HorizontalAlignment.Center); //设置单元格背景颜色
for (int j = 1; j < table.getRows().getCount(); j++) {
if (j % 2 == 0) {
TableRow row2 = table.getRows().get(j);
for (int f = 1; f < row2.getCells().getCount(); f++) {
row2.getCells().get(f).getCellFormat().setBackColor(new Color(144,238,144));
}
}
} //设置表格边框样式
table.getTableFormat().getBorders().setBorderType(BorderStyle.Thick_Thin_Large_Gap); //保存文档
doc.saveToFile("CreateTable.docx", FileFormat.Docx_2013);
} }
(本文完)
转载请注明出处!
Java 在Word创建表格的更多相关文章
- Java读取word中表格
因为要新建一个站,公司要把word表格的部分行列存到数据库中.之前用java操作过excel,本来打算用java从word表格中读取数据,再存到数据库中,结果因为权限不够,无法访问公司要写的那个数据库 ...
- Java 操作Word表格——创建嵌套表格、添加/复制表格行或列、设置表格是否禁止跨页断行
本文将对如何在Java程序中操作Word表格作进一步介绍.操作要点包括 如何在Word中创建嵌套表格. 对已有表格添加行或者列 复制已有表格中的指定行或者列 对跨页的表格可设置是否禁止跨页断行 创建表 ...
- 如何通过Java应用程序创建Word表格
表格,又称为表,既是一种可视化交流模式,又是一种组织整理数据的手段.人们在通讯交流.科学研究以及数据分析活动当中广泛采用着形形色色的表格.那么如何通过Java应用程序创建Word表格呢?别担心,本文将 ...
- Java 操作Word书签(二):添加文本、图片、表格到书签内容
在Java操作Word书签(一)中介绍了给Word中的特定段落或文字添加书签.读取及删除已有书签的方法,本文将继续介绍Java 操作Word书签的方法,即如何给已有的书签添加内容,包括添加文本.图片. ...
- Java 操作Word书签(三):用文本、图片、表格替换书签
本篇文章将继续介绍通过Java来操作Word书签的方法,即替换Word中已有书签,包括用新的文本.图片.表格等替换原有书签处的内容. 使用工具:Free Spire.Doc for Java (免费版 ...
- Java 操作Word表格
本文将对如何在Java程序中操作Word表格作进一步介绍.操作要点包括 如何在Word中创建嵌套表格. 对已有表格添加行或者列 复制已有表格中的指定行或者列 对跨页的表格可设置是否禁止跨页断行 创建表 ...
- [转载]JAVA获取word表格中数据的方案
上一个项目的开发中需要实现从word中读取表格数据的功能,在JAVA社区搜索了很多资料,终于找到了两个相对最佳的方案,因为也得到了不少网友们的帮助,所以不敢独自享用,在此做一个分享. 两个方案分别是: ...
- [原创]JAVA获取word表格中数据的方案
上一个项目的开发中需要实现从word中读取表格数据的功能,在JAVA社区搜索了很多资料,终于找到了两个相对最佳的方案,因为也得到了不少网友们的帮助,所以不敢独自享用,在此做一个分享. 两个方案分别是: ...
- Java 实现word 中写入文字图片的解决方案
JAVA生成WORD文件的方法目前有以下两种方式: 一种是jacob 但是局限于windows平台 往往许多JAVA程序运行于其他操作系统 在此不讨论该方案; 一种是poi但是他的excel处理很程序 ...
随机推荐
- WPF控件开发(2) 自动完成(AutoComplete)-1
自动完成功能使用范围很广,多以TextBox或ComboBox的形式出现,在输入的同时给予候选词,候选词一般有两种方式获取. 一种类似Baidu,Google,Bing之类的搜索引擎所用的直接给予前十 ...
- uReplicator实现分析
MirrorMakerWorker分析 是整个同步机制的主入口,主要组织的逻辑有: 配置数据的传入与处理,ConsumerConfig对象的构建 度量对象的准备,定时上报的度量数据收集线程的定义与启动 ...
- python学习之dictionary函数的用法
编写下面这段代码运行出现了报错.#!/usr/bin/env python2.7#-*-coding:utf-8 -*- d=['T']a=raw_input('请输入a的值')if a in d : ...
- WINDOWS批量替换不同文件夹下的相同文件
今天帮媳妇解决的问题,记录一下,也许以后有用 例子: N个文件夹下有同一个文件(common.php),但是,现在对common.php文件进行了大量修改. 现在想用最新的common.php替换掉所 ...
- jmter+ANT+jekins之配置文件简单优化(build.xml)
<?xml version="1.0" encoding="utf-8"?> <project name="ant-jmeter-t ...
- python - 接口自动化测试 - MysqlUtil - 数据库操作封装
# -*- coding:utf-8 -*- ''' @project: ApiAutoTest @author: Jimmy @file: mysql_util.py @ide: PyCharm C ...
- Visual C++网络五子棋游戏源代码
说明:网络对战版的五子棋,VC++游戏源码,带音乐,可设置网络最终网络下棋,通过源代码你将了解到设置菜单状态.服务器端口申请.客户机申请连接.发送数据.游戏编写.监听和使用套接字.主菜单对象定义等基础 ...
- c# 钩子程序
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.R ...
- 【bzoj2427】[HAOI2010]软件安装 Tarjan+树形背包dp
题目描述 现在我们的手头有N个软件,对于一个软件i,它要占用Wi的磁盘空间,它的价值为Vi.我们希望从中选择一些软件安装到一台磁盘容量为M计算机上,使得这些软件的价值尽可能大(即Vi的和最大).但是现 ...
- HDU——2089 不要62
不要62 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...