表格作为一种可视化交流模式及组织整理数据的手段,在各种场合及文档中应用广泛。常见的表格可包含文字、图片等元素,我们操作表格时可以插入图片、写入文字及格式化表格样式等。下面,将通过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创建表格的更多相关文章

  1. Java读取word中表格

    因为要新建一个站,公司要把word表格的部分行列存到数据库中.之前用java操作过excel,本来打算用java从word表格中读取数据,再存到数据库中,结果因为权限不够,无法访问公司要写的那个数据库 ...

  2. Java 操作Word表格——创建嵌套表格、添加/复制表格行或列、设置表格是否禁止跨页断行

    本文将对如何在Java程序中操作Word表格作进一步介绍.操作要点包括 如何在Word中创建嵌套表格. 对已有表格添加行或者列 复制已有表格中的指定行或者列 对跨页的表格可设置是否禁止跨页断行 创建表 ...

  3. 如何通过Java应用程序创建Word表格

    表格,又称为表,既是一种可视化交流模式,又是一种组织整理数据的手段.人们在通讯交流.科学研究以及数据分析活动当中广泛采用着形形色色的表格.那么如何通过Java应用程序创建Word表格呢?别担心,本文将 ...

  4. Java 操作Word书签(二):添加文本、图片、表格到书签内容

    在Java操作Word书签(一)中介绍了给Word中的特定段落或文字添加书签.读取及删除已有书签的方法,本文将继续介绍Java 操作Word书签的方法,即如何给已有的书签添加内容,包括添加文本.图片. ...

  5. Java 操作Word书签(三):用文本、图片、表格替换书签

    本篇文章将继续介绍通过Java来操作Word书签的方法,即替换Word中已有书签,包括用新的文本.图片.表格等替换原有书签处的内容. 使用工具:Free Spire.Doc for Java (免费版 ...

  6. Java 操作Word表格

    本文将对如何在Java程序中操作Word表格作进一步介绍.操作要点包括 如何在Word中创建嵌套表格. 对已有表格添加行或者列 复制已有表格中的指定行或者列 对跨页的表格可设置是否禁止跨页断行 创建表 ...

  7. [转载]JAVA获取word表格中数据的方案

    上一个项目的开发中需要实现从word中读取表格数据的功能,在JAVA社区搜索了很多资料,终于找到了两个相对最佳的方案,因为也得到了不少网友们的帮助,所以不敢独自享用,在此做一个分享. 两个方案分别是: ...

  8. [原创]JAVA获取word表格中数据的方案

    上一个项目的开发中需要实现从word中读取表格数据的功能,在JAVA社区搜索了很多资料,终于找到了两个相对最佳的方案,因为也得到了不少网友们的帮助,所以不敢独自享用,在此做一个分享. 两个方案分别是: ...

  9. Java 实现word 中写入文字图片的解决方案

    JAVA生成WORD文件的方法目前有以下两种方式: 一种是jacob 但是局限于windows平台 往往许多JAVA程序运行于其他操作系统 在此不讨论该方案; 一种是poi但是他的excel处理很程序 ...

随机推荐

  1. Apache的安装与下载

    PHP的运行必然少不了服务器的支持,何为服务器?通俗讲就是在一台计算机上,安装个服务器软件,这台计算机便可以称之为服务器,服务器软件和计算机本身的操作系统是两码事,计算机自身的操作系统可以为linux ...

  2. HDU 2177 取(2堆)石子游戏

    取(2堆)石子游戏 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  3. Python虚拟机类机制之从class对象到instance对象(五)

    从class对象到instance对象 现在,我们来看看如何通过class对象,创建instance对象 demo1.py class A(object): name = "Python&q ...

  4. 9、JS对象 知识总结

    1.对象 <!DOCTYPE html> <html> <body> <script> <!-- 新建对象 --> person=new O ...

  5. 安恒杯月赛 babypass getshell不用英文字母和数字

    BABYBYPASS 先贴代码: ①限制字符长度35个 ②不能使用英文字母和数字和 _ $ 最后提示有个getFlag()函数,从这个函数入手. 我们的第一思路是直接eval执行getFlag函数,但 ...

  6. python 学习分享-实战篇简单的ftp

    import socket import os import time import pickle Basedb = os.path.dirname(os.path.dirname(os.path.a ...

  7. [译]tar打包时忽略某些文件夹内容

    使用tar的 --exclude的选项 $ tar --exclude='./folder' --exclude='./upload/folder2' -zcvf /backup/filename.t ...

  8. Oracle连接查询小结

    表TESTA,TESTB,TESTC,各有A, B两列 A B 001 10A 002 20A A B 001 10B 003 30B A B 001 10C 004 40C 连接分为两种:内连接与外 ...

  9. 【bzoj1266】[AHOI2006]上学路线route 最短路+最小割

    题目描述 可可和卡卡家住合肥市的东郊,每天上学他们都要转车多次才能到达市区西端的学校.直到有一天他们两人参加了学校的信息学奥林匹克竞赛小组才发现每天上学的乘车路线不一定是最优的. 可可:“很可能我们在 ...

  10. 编程风格——代码中特殊的注释技术——TODO、FIXME和XXX的用处

    代码中特殊的注释技术——TODO.FIXME和XXX的用处 前言:今天在阅读Qt Creator的源代码时,发现一些注释中有FIXME英文单词,用英文词典居然查不到其意义!实际上,在阅读一些开源代码时 ...