本文将对如何在Java程序中操作Word表格作进一步介绍。操作要点包括

  • 如何在Word中创建嵌套表格、
  • 对已有表格添加行或者列
  • 复制已有表格中的指定行或者列
  • 对跨页的表格可设置是否禁止跨页断行

创建表格,包括添加数据、插入表格、合并单元格、设置表格样式、单元格居中、单元格背景色,单元格字体样式等设置,可参考这篇文章里的内容。


使用工具:Free Spire.Doc for Java (免费版)

Jar文件可通过官网下载jar文件包,下载后,解压文件,将lib文件夹下的Spire.Doc.jar导入Java程序;也可以在maven项目中通过maven仓库安装导入


【添加Word嵌套表格】

import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.TextRange; public class NestedTable {
public static void main(String[]args){
//加载测试文档
Document doc = new Document("sample.docx"); //获取指定表格中的单元格,并设置行高、列宽
Section sec = doc.getSections().get(0);
Table table = sec.getTables().get(0);
table.getRows().get(0).setHeight(120f);
table.getRows().get(0).getCells().get(0).setWidth(380); //添加嵌套表格到指定单元格
Table nestedtable = table.get(0,0).addTable(true);
nestedtable.getTableFormat().setHorizontalAlignment(RowAlignment.Center);//设置嵌套表格在单元格中的对齐方式
nestedtable.resetCells(4,4);//指定嵌套表格行数、列数
nestedtable.autoFit(AutoFitBehaviorType.Auto_Fit_To_Contents);//设置嵌套表格内容自适应方法
//声明表格数组内容
String[][] data ={
new String[]{"编号","产区","最新型号","生产日期",},
new String[]{"1","A","V2.2.0","2019-06-21"},
new String[]{"2","B","V2.6.1","2019-06-18"},
new String[]{"3","C","V2.6.2","2019-06-14"},
}; //填充数组内容到嵌套表格
for (int i = 0; i < data.length; i++) {
TableRow dataRow = nestedtable.getRows().get(i);
dataRow.getCells().get(i).setWidth(160);
dataRow.setHeight(25);
dataRow.setHeightType(TableRowHeightType.Exactly);
for (int j = 0; j < data[i].length; j++) {
dataRow.getCells().get(j).getCellFormat().setVerticalAlignment(VerticalAlignment.Middle);
TextRange range = dataRow.getCells().get(j).addParagraph().appendText(data[i][j]);
range.getCharacterFormat().setFontName("楷体");
range.getCharacterFormat().setFontSize(11f);
range.getOwnerParagraph().getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
}
} //保存文档
doc.saveToFile("nesedtable1.docx",FileFormat.Docx_2010);
}
}

嵌套表格效果:

【在Word表格中添加行或者列】

     1. 添加行

import com.spire.doc.*;

public class AddRow {
public static void main(String[] args){
//加载测试文档
Document doc = new Document();
doc.loadFromFile("sample.docx"); //获取表格
Section section = doc.getSections().get(0);
Table table = section.getTables().get(0); table.addRow();//默认在表格最下方插入一行
//table.getRows().insert(2,table.addRow());//在表格中第3行插入一行
//table.addRow(4);//默认在表格最下方添加4个单元格
//table.addRow(true,2);//带格式在最后一行添加2个单元格
//table.addRow(false,2);//不带格式在最后一行添加2个单元格 //保存文档
doc.saveToFile("addrow.docx",FileFormat.Docx_2013);
doc.dispose();
}
}

表格行添加效果:

     2. 添加列

import com.spire.doc.*;
import com.spire.doc.documents.BorderStyle; import java.awt.*; public class AddColumn {
public static void main(String[] args){
//加载测试文档
Document doc = new Document();
doc.loadFromFile("sample.docx"); //获取表格
Section section = doc.getSections().get(0);
Table table = section.getTables().get(0); //获取表格单元格宽度及类型
float width = table.get(0,0).getWidth();
CellWidthType type = table.get(0,0).getCellWidthType();
//遍历表格每一行
for (int i = 0; i < table.getRows().getCount(); i++) {
TableRow row = table.getRows().get(i);//获取表格每一行
Color color = row.getCells().get(0).getCellFormat().getBackColor();//获取表格单元格背景色
//基于表格每行,在最后添加一个单元格,并设置单元格格式
TableCell cell = row.addCell(true);//默认在最后一列添加单元格
cell.setWidth(width);
cell.setCellWidthType(type);
cell.getCellFormat().getBorders().setBorderType(BorderStyle.Single);
cell.getCellFormat().setBackColor(color);
//如需在指定位置插入列,基于以上代码并增加下面一行代码即可
//row.getCells().insert(2,cell);//插入一列作为第三列
} //保存文档
doc.saveToFile("addcolumn.docx", FileFormat.Docx_2013);
doc.dispose();
}
}

表格列添加效果:

【复制Word表格中的行或者列】

1. 复制行

import com.spire.doc.*;

public class CopyRow {
public static void main(String[] args) {
//加载测试文档
Document doc = new Document();
doc.loadFromFile("test.docx"); //获取表格
Section section = doc.getSections().get(0);
Table table =section.getTables().get(0); //复制第三行,并将复制后的行插入到表格作为第五行
TableRow row = table.getRows().get(2).deepClone();
table.getRows().insert(4,row); //保存文档
doc.saveToFile("CopyRow.docx",FileFormat.Docx_2013);
doc.dispose();
}
}

表格行复制效果:

      2. 复制列

import com.spire.doc.*;

public class CopyColumn {
public static void main(String[] args) {
//加载测试文档
Document doc = new Document();
doc.loadFromFile("test.docx"); //获取表格
Section section = doc.getSections().get(0);
Table table =section.getTables().get(0); //遍历表格每行
for (int i = 0; i < table.getRows().getCount(); i++) {
//复制表格中每行的最后一个单元格,复制
TableRow row = table.getRows().get(i);
TableCell cell = (TableCell) row.getCells().getLastItem().deepClone();
//row.getCells().add(cell);//默认在每行最后添加复制后的单元格
row.getCells().insert(2,cell);//在指定位置插入复制后的单元格
} //保存文档
doc.saveToFile("CopyColumn1.docx",FileFormat.Docx_2013);
doc.dispose();
}
}

表格列复制效果:

【设置Word表格是否禁止跨页断行】

这里通过两种方式来设置防止表格跨页出现断行的效果,供参考。

  1. 设置属性禁止跨页断行

import com.spire.doc.*;

public class PreventPagebreak {
public static void main(String[]args){
//加载测试文档
Document doc= new Document("test.docx"); //获取表格
Table table = doc.getSections().get(0).getTables().get(0); //设置表格是否分页断行
table.getTableFormat().isBreakAcrossPages(false); //保存文档
doc.saveToFile("result.docx",FileFormat.Docx_2013);
}
}

    2. 保持表格内容在同一页面

import com.spire.doc.*;
import com.spire.doc.documents.Paragraph; public class PreventPagebreak {
public static void main(String[]args){
//加载测试文档
Document doc= new Document("test.docx"); //获取表格
Table table = doc.getSections().get(0).getTables().get(0); //遍历表格单元格
for (int i = 0;i< table.getRows().getCount();i++) {
TableRow rows = table.getRows().get(i);
for (int j = 0; j< rows.getCells().getCount(); j++){
for (int z= 0; z < rows.getCells().get(j).getParagraphs().getCount();z++){
Paragraph p = rows.getCells().get(j).getParagraphs().get(z);
p.getFormat().setKeepFollow(true);//设置表格内容在同一页显示
}
}
} //保存文档
doc.saveToFile("result1.docx",FileFormat.Docx_2013);
}
}

(本文完)

转载请注明出处!!

Java 操作Word表格——创建嵌套表格、添加/复制表格行或列、设置表格是否禁止跨页断行的更多相关文章

  1. Java 操作Word表格

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

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

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

  3. Java操作ElasticSearch之创建客户端连接

    Java操作ElasticSearch之创建客户端连接 3 发布时间:『 2017-09-11 17:02』  博客类别:elasticsearch  阅读(3157) Java操作ElasticSe ...

  4. java操作文件的创建、删除、遍历

    java操作文件的创建.删除.遍历: package test; import java.io.File; import java.io.IOException; import java.util.A ...

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

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

  6. [转载]java操作word(一)

    一. 需求背景 在做项目的过程中,经常会遇到要把数据库数据导出到Word文件中的需求,因为很多情况下,我们需要将数据导出到WORD中进行打印.此需求可以通过用程序填充数据到word模板中来实现.所谓模 ...

  7. [原创]java操作word(一)

    一. 需求背景 在做项目的过程中,经常会遇到要把数据库数据导出到Word文件中的需求,因为很多情况下,我们需要将数据导出到WORD中进行打印.此需求可以通过用程序填充数据到word模板中来实现.所谓模 ...

  8. Java操作word文档使用JACOB和POI操作word,Excel,PPT需要的jar包

    可参考文档: http://wibiline.iteye.com/blog/1725492 下载jar包 http://download.csdn.net/download/javashixiaofe ...

  9. java操作Word总结

    import com.jacob.activeX.ActiveXComponent; import com.jacob.com.Dispatch; import com.jacob.com.Varia ...

随机推荐

  1. Optional和Stream的map与flatMap

    Optional的map和flatMap Optional存在map和flatMap方法.map源码如下 public<U> Optional<U> map(Function& ...

  2. spring boot启动加载项CommandLineRunner

    spring boot启动加载项CommandLineRunner 在使用SpringBoot构建项目时,我们通常有一些预先数据的加载.那么SpringBoot提供了一个简单的方式来实现–Comman ...

  3. Spinner列表选择框

    Spinner首先它是一个弹出式的列表选择框,由于间接继承了ViewGroup,所以它可以当做一个容器使用; 如果我们可以明确下拉列表中的列表项, 则可以不需要编写代码, 只需要为spinner指定a ...

  4. Spring Boot 面试题总结

    1.什么是spring boot 答案:springboot是用来简化spring应用的初始搭建和开发过程,使用特定的配置文件来配置,例如application.properties,简化来maven ...

  5. Sticks(剪枝+BFS)

    Problem Description George took sticks of the same length and cut them randomly until all parts beca ...

  6. Linux环境基于CentOS7 搭建部署Docker容器

    1.Docker容器概述 区分Docker容器技术和VM虚拟机技术: evernotecid://394EFE90-9CE0-4D65-A8CD-DFEC0DC8061E/appyinxiangcom ...

  7. 字节输出流OutputStream

    1.OutputStream是输出字节流的超类. import java.io.File; import java.io.FileOutputStream; import java.io.IOExce ...

  8. mysql ER图

        ER 图 ER图也被称为实体-联系图,提供了表示实体类型.属性和联系的方法,下图就是典型的一张ER图. ER图主要由四个成分构成: 1 实体 实体是客观世界中存在的各种事物,或者某个抽象事件, ...

  9. 基于python-django框架的支付宝支付案例

    目录 @ 一. 开发前的准备 1. 必须了解的知识 SDK:软件开发工具包,可以为开发者提供快速开发的工具 沙箱环境:也就是测试环境 支付宝支付金额的精度:小数点后两位(面试) 支付宝用的什么加密方式 ...

  10. 如何使用rsync备份

    已知3台服务器主机名分别为web01.backup .nfs主机信息见下表: 角色 外网IP(NAT) 内网IP(LAN) 主机名 WEB eth0:10.0.0.7 eth1:172.16.1.7 ...