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

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

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


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

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


【添加Word嵌套表格】

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

嵌套表格效果:

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

     1. 添加行

  1. import com.spire.doc.*;
  2.  
  3. public class AddRow {
  4. public static void main(String[] args){
  5. //加载测试文档
  6. Document doc = new Document();
  7. doc.loadFromFile("sample.docx");
  8.  
  9. //获取表格
  10. Section section = doc.getSections().get(0);
  11. Table table = section.getTables().get(0);
  12.  
  13. table.addRow();//默认在表格最下方插入一行
  14. //table.getRows().insert(2,table.addRow());//在表格中第3行插入一行
  15. //table.addRow(4);//默认在表格最下方添加4个单元格
  16. //table.addRow(true,2);//带格式在最后一行添加2个单元格
  17. //table.addRow(false,2);//不带格式在最后一行添加2个单元格
  18.  
  19. //保存文档
  20. doc.saveToFile("addrow.docx",FileFormat.Docx_2013);
  21. doc.dispose();
  22. }
  23. }

表格行添加效果:

     2. 添加列

  1. import com.spire.doc.*;
  2. import com.spire.doc.documents.BorderStyle;
  3.  
  4. import java.awt.*;
  5.  
  6. public class AddColumn {
  7. public static void main(String[] args){
  8. //加载测试文档
  9. Document doc = new Document();
  10. doc.loadFromFile("sample.docx");
  11.  
  12. //获取表格
  13. Section section = doc.getSections().get(0);
  14. Table table = section.getTables().get(0);
  15.  
  16. //获取表格单元格宽度及类型
  17. float width = table.get(0,0).getWidth();
  18. CellWidthType type = table.get(0,0).getCellWidthType();
  19. //遍历表格每一行
  20. for (int i = 0; i < table.getRows().getCount(); i++) {
  21. TableRow row = table.getRows().get(i);//获取表格每一行
  22. Color color = row.getCells().get(0).getCellFormat().getBackColor();//获取表格单元格背景色
  23. //基于表格每行,在最后添加一个单元格,并设置单元格格式
  24. TableCell cell = row.addCell(true);//默认在最后一列添加单元格
  25. cell.setWidth(width);
  26. cell.setCellWidthType(type);
  27. cell.getCellFormat().getBorders().setBorderType(BorderStyle.Single);
  28. cell.getCellFormat().setBackColor(color);
  29. //如需在指定位置插入列,基于以上代码并增加下面一行代码即可
  30. //row.getCells().insert(2,cell);//插入一列作为第三列
  31. }
  32.  
  33. //保存文档
  34. doc.saveToFile("addcolumn.docx", FileFormat.Docx_2013);
  35. doc.dispose();
  36. }
  37. }

表格列添加效果:

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

1. 复制行

  1. import com.spire.doc.*;
  2.  
  3. public class CopyRow {
  4. public static void main(String[] args) {
  5. //加载测试文档
  6. Document doc = new Document();
  7. doc.loadFromFile("test.docx");
  8.  
  9. //获取表格
  10. Section section = doc.getSections().get(0);
  11. Table table =section.getTables().get(0);
  12.  
  13. //复制第三行,并将复制后的行插入到表格作为第五行
  14. TableRow row = table.getRows().get(2).deepClone();
  15. table.getRows().insert(4,row);
  16.  
  17. //保存文档
  18. doc.saveToFile("CopyRow.docx",FileFormat.Docx_2013);
  19. doc.dispose();
  20. }
  21. }

表格行复制效果:

      2. 复制列

  1. import com.spire.doc.*;
  2.  
  3. public class CopyColumn {
  4. public static void main(String[] args) {
  5. //加载测试文档
  6. Document doc = new Document();
  7. doc.loadFromFile("test.docx");
  8.  
  9. //获取表格
  10. Section section = doc.getSections().get(0);
  11. Table table =section.getTables().get(0);
  12.  
  13. //遍历表格每行
  14. for (int i = 0; i < table.getRows().getCount(); i++) {
  15. //复制表格中每行的最后一个单元格,复制
  16. TableRow row = table.getRows().get(i);
  17. TableCell cell = (TableCell) row.getCells().getLastItem().deepClone();
  18. //row.getCells().add(cell);//默认在每行最后添加复制后的单元格
  19. row.getCells().insert(2,cell);//在指定位置插入复制后的单元格
  20. }
  21.  
  22. //保存文档
  23. doc.saveToFile("CopyColumn1.docx",FileFormat.Docx_2013);
  24. doc.dispose();
  25. }
  26. }

表格列复制效果:

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

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

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

  1. import com.spire.doc.*;
  2.  
  3. public class PreventPagebreak {
  4. public static void main(String[]args){
  5. //加载测试文档
  6. Document doc= new Document("test.docx");
  7.  
  8. //获取表格
  9. Table table = doc.getSections().get(0).getTables().get(0);
  10.  
  11. //设置表格是否分页断行
  12. table.getTableFormat().isBreakAcrossPages(false);
  13.  
  14. //保存文档
  15. doc.saveToFile("result.docx",FileFormat.Docx_2013);
  16. }
  17. }

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

  1. import com.spire.doc.*;
  2. import com.spire.doc.documents.Paragraph;
  3.  
  4. public class PreventPagebreak {
  5. public static void main(String[]args){
  6. //加载测试文档
  7. Document doc= new Document("test.docx");
  8.  
  9. //获取表格
  10. Table table = doc.getSections().get(0).getTables().get(0);
  11.  
  12. //遍历表格单元格
  13. for (int i = 0;i< table.getRows().getCount();i++) {
  14. TableRow rows = table.getRows().get(i);
  15. for (int j = 0; j< rows.getCells().getCount(); j++){
  16. for (int z= 0; z < rows.getCells().get(j).getParagraphs().getCount();z++){
  17. Paragraph p = rows.getCells().get(j).getParagraphs().get(z);
  18. p.getFormat().setKeepFollow(true);//设置表格内容在同一页显示
  19. }
  20. }
  21. }
  22.  
  23. //保存文档
  24. doc.saveToFile("result1.docx",FileFormat.Docx_2013);
  25. }
  26. }

(本文完)

转载请注明出处!!

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. 使用Quarkus在Openshift上构建微服务的快速指南

    在我的博客上,您有机会阅读了许多关于使用Spring Boot或Micronaut之类框架构建微服务的文章.这里将介绍另一个非常有趣的框架专门用于微服务体系结构,它越来越受到大家的关注– Quarku ...

  2. Jmeter 逻辑控制器 之 事务控制器

    前面我在做性能测试的时候,由于我们的系统是需要登录的,登录成功后,系统默认加载其订单数据,因此在用户看来这是一个操作.所以为了模拟这个操作,我需要访问两个接口,并且把这两个接口的响应时间算在一起,那么 ...

  3. hive学习笔记之-数据类型

    数据类型 Hive基本的数据类型: Hive集合数据类型: 另外还有一个复合数据类型,可以综合上面的数据类型组合到一起. ·          union: UNIONTYPE<data_typ ...

  4. mapper 传多个参数

    Mybatis的Mapper接口的参数,一般是一个对象,但如果不是对象,并且有多个参数的时候呢?我们第一个的想法是把参数封装成一个java.util.Map类型,然后在方法的注释上面写上map的key ...

  5. STL中关于全排列next_permutation以及prev_permutation的用法

    这两个函数都包含在algorithm库中.STL提供了两个用来计算排列组合关系的算法,分别是next_permutation和prev_permutation. 一.函数原型 首先我们来看看这两个函数 ...

  6. 使用broker进行Datagurd主备切换报ORA-12514异常

    在使用Datagurd broker进行Datagurd主备切换时报ORA-12514监听异常, 详细信息如下: DGMGRL> switchover to xiaohe; Performing ...

  7. Vue学习之不同组件之间的消息传递

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. 【linux】记录一个yum update和upgrade的区别

    yum update 更新软件包和系统软件.系统内核 yum upgrade只更新软件包,不更新系统软件和系统内核 查看版本号 [root@localhost ~]# uname -r 3.10.0- ...

  9. FPGA 开发详细流程你了解吗?

    FPGA 的详细开发流程就是利用 EDA 开发工具对 FPGA 芯片进行开发的过程. FPGA 的详细开发流程如下所示,主要包括电路设计.设计输入.综合(优化).布局布线(实现与优化).编程配置五大步 ...

  10. 小型APP系统开发与应用项目实训

    实训项目 :             小型APP系统开发与应用项目实训                           项目成品名称:          果乐多商城               项 ...