本文将对如何在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. Aspose.Java实现word转pdf,添加水印等操作

    Aspose.Java实现word转pdf,添加水印等操作 一. word转pdf 二. 文档插入水印 Aspose是一款商用版控件,支持各类文档操作,这里主要介绍如何在Springboot项目中使用 ...

  2. java操作office和pdf文件java读取word,excel和pdf文档内容

    在平常应用程序中,对office和pdf文档进行读取数据是比较常见的功能,尤其在很多web应用程序中.所以今天我们就简单来看一下Java对word.excel.pdf文件的读取.本篇博客只是讲解简单应 ...

  3. java 操作 word 表格和样式,java读取word表格中的表格 java如何读取word中的excel表格数据...

    Java 利用poi 可以直接读取word中的表格保持样式生1.读取word 2003及word 2007需要的jar包 读取 2003 版本(.doc)的word文件相对来说比较简单,只需要 poi ...

  4. 【使用分享】一文掌握Aspose.Words for Java,实现Word文档的生成与操作

    Aspose.Words for Java是一个功能强大的Java Word文档处理组件,支持文档的生成.修改.转换.渲染等功能.本文将为您全面介绍Aspose.Words的主要功能与用法. 一.文档 ...

  5. Java读取word中表格

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

  6. 【Java 编程】文件操作,文件内容的读写—数据流

    一.认识文件 1.什么是文件 平时说的文件一般都是指存储在硬盘上的普通文件 形如 txt, jpg, mp4, rar 等这些文件都可以认为是普通文件,它们都是在硬盘上存储的 在计算机中,文件可能是一 ...

  7. java设置界面边框,技术员教你解决Java 添加Word页面边框

    电脑现已成为我们工作.生活和娱乐必不可少的工具了,在使用电脑的过程中,可能会遇到Java 添加Word页面边框的问题,如果我们遇到了Java 添加Word页面边框的情况,该怎么处理怎么才能解决Java ...

  8. java导出word的几种方式

    目前来看,java导出word大致有6种解决方案: 1:Jacob是Java-COM Bridge的缩写,它在Java与微软的COM组件之间构建一座桥梁.使用Jacob自带的DLL动态链接库,并通过J ...

  9. java实现word、pdf文件下载功能

    在SpringMVC的开发过程中,有时需要实现文档的下载功能.文档的下载功能涉及到了java IO流操作的基础知识,下面本文详细介绍java如何实现后台文档下载功能. 首先根据文档在项目中的存储路径建 ...

最新文章

  1. android蓝牙开启后会尝试自动连接,以编程方式配对后,Android会自动连接蓝牙设备...
  2. 15拆分成3个不同的自然数_15个小时搜救破拆,他磨破3双手套营救出4个生还者...
  3. system.gc会立即执行垃圾回收吗_JVM基础到实战03-垃圾回收概念
  4. ext js如何动态更改xtype_K8S ConfigMap 用于动态应用程序的实践
  5. 用c语言简单办法做一个字典_幼儿园手工,用废纸筒做一个简单的小蝴蝶,有教程...
  6. 海明距离mysql查询_海量数据,海明距离高效检索(smlar) - 阿里云RDS PosgreSQL最佳实践-阿里云开发者社区...
  7. APP搜索如何又快又准?
  8. 计算机标准化管理制度,计算机中心标准化管理.doc
  9. 每日英语:Foreign Tourists Skip Beijing
  10. neo4j的查询语法Cypher+python演示
  11. 设置linux系统的线程数量,Linux_查看系统cpu个数、核心数、线程数
  12. 原神 - 米游社 每日签到
  13. 【医疗图像分割】Deep neural networks for the detection and segmentation of the retinal fluid in OCT images.
  14. 使用Python爬虫自动爬取沪港通每日持股数据
  15. 2019年度受欢迎的开源软件
  16. 三八节买什么数码好物?三八女神节实用不吃灰的数码好物推荐
  17. 计算机应用基础excel2007 6.2使用函数和公式 教案,计算机应用基础教案62使用函数和公式.docx...
  18. 计算机基础知识在教学的应用,计算机基础知识中项目教学法的应用
  19. 谷歌排名影响因素最新研究(SEM RUSH版)
  20. Transformer基础

热门文章

  1. html2canvas解决图片偏移并下载
  2. Matlab实现美颜系统
  3. 不是机器人韩剧所有歌曲_不是机器人主题曲、插曲、片尾曲OST歌曲歌词
  4. CATIA使用问题之登陆时,提示服务器可能已关闭,请与管理员联系”
  5. 漏电保护器跳闸原因的查找和解决
  6. 舆情监控软件优势及缺点,TOOM介绍舆情监控软件都有哪些?
  7. 使用puppeteer抓取网站数据
  8. matlab filter函数原理,基于python实现matlab filter函数过程详解
  9. 我的微信又被限制了!
  10. Android 修改 SELinux avc 权限的方法