表格作为一种可视化交流模式及组织整理数据的手段,在各种场合及文档中应用广泛。常见的表格可包含文字、图片等元素,我们操作表格时可以插入图片、写入文字及格式化表格样式等。下面,将通过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. 某面试公司出的面试题---用JS比较两个版本号高低

    一天中午某个公司给我反馈的面试题,说,比较两个文件的版本号,然后我给发过去了,说我的代码不符合他的要求,o(╯□╰)o了var compareVersion = compareVersion||fun ...

  2. android基础知识杂记

    Activity中获取视图组件对象:public View findViewById(@IdRes int id) 该方法以组件的资源ID为参数,返回一个视图对象View,需要强转成具体的视图类对象. ...

  3. Install ADDS on Windows Server 2012 R2 with PowerShell

    Install ADDS on Windows Server 2012 R2 with PowerShell Posted by ethernuno on 20/04/2014 In this tut ...

  4. 【Partition List】cpp

    题目: Given a linked list and a value x, partition it such that all nodes less than x come before node ...

  5. C 语言 习题 1-10

    练习 1-10 编写一个将输入复制到输出的程序,并将其中的制表符替换为\t,把回退符替换为\b,把反斜杠替按为\\.这样可以将制表符和回退符以可见的方式显示出来. #include<stdio. ...

  6. Pycharm注册码最新版本2019激活码activation code + 最实用的激活方法(亲测有效)

    同时适用于jetbrains全系列可用例:IDEA.WebStorm.phpstor 由于想趁着这个寒假多学习下python,所以这些实用小技巧分享给大家,拿走不谢~ 这里为大家提供了两种最实用的激活 ...

  7. jquery左右滑动菜单

    <div class="mini-container" style="position:relative;height:100%;"> <di ...

  8. Java学习2

    final在修饰类时,并不限制用户修改对象包含的变量值,只是限制了对象的主转移,只能针对某一个对象进行操作,中途不可更改对象. 重写父类的方法 重写(Override)和重载(Overload)都是针 ...

  9. sqlserver2008透明书库加密

    /*Title:TDE加密Author:浪客Environment:Windows Server 2008 Enterprise + SQL Server 2008 EnterpriseDescrip ...

  10. sqlserver创建存储过程返回table

    --创建存储过程test create procedure [dbo].[test] ( @I_MTR NVARCHAR (MAX), @I_TYPE NVARCHAR (MAX), @I_FAC N ...