Java删除word合并单元格时的重复值
Spire.Doc提供了Table.applyVerticalMerge()方法来垂直合并word文档里面的表格单元格,Table.applyHorizontalMerge()方法来水平合并表格单元格。默认情况下,如果要合并的单元格包含相同的值,那么合并后的单元格会有重复的值。
【程序环境】
本次测试时,在程序中引入Spire.Doc for Java。可通过以下方法引用Spire.Doc.jar文件:
方法1:将Spire.Doc for Java下载到本地,解压,安装。安装完成后,找到安装路径下BIN文件夹中的Spire.Doc.jar。然后在Visual Studio中打开“解决方案资源管理器”,鼠标右键点击“引用”,“添加引用”,将本地路径BIN文件夹下的jar文件添加引用至程序。
方法2:使用Maven进行安装,你可以通过在项目的pom.xml文件中添加以下代码,在你的应用程序中轻松导入该JAR文件。
1 <repositories>
2 <repository>
3 <id>com.e-iceblue</id>
4 <url>https://repo.e-iceblue.cn/repository/maven-public/</url>
5 </repository>
6 </repositories>
7 <dependencies>
8 <dependency>
9 <groupId>e-iceblue</groupId>
10 <artifactId>spire.doc</artifactId>
11 <version>10.9.0</version>
12 </dependency>
13 </dependencies>
方法3:通过NuGet安装。可通过以下2种方法安装:
(1)可以在Visual Studio中打开“解决方案资源管理器”,鼠标右键点击“引用”,“管理NuGet包”,然后搜索“FreeSpire.Doc”,点击“安装”。等待程序安装完成。
(2)将以下内容复制到PM控制台安装。
Install-Package FreeSpire.Doc -Version 10.9.0
【删除合并单元格时的重复值】
- 创建一个Document实例,使用Document.loadFromFile()方法加载示例文档。
- 使用Document.getSections()方法获得节集合,然后使用SectionCollection.get()方法获得特定的节。
- 使用Section.getTables()方法获得表格集合,然后使用TableCollection.get()方法获得想要的表。
- 调用mergeCell(Table table,boolean isHorizontalMerge,int index,int start,int end)方法来垂直或水平地合并表格单元格。这个方法将确定要合并的单元格是否有相同的值,并在合并的单元格中只保留一个值。
- 使用Document.saveToFile()方法保存文件。
【代码示例】
1 import com.spire.doc.*;
2 import com.spire.doc.interfaces.ITable;
3
4 public class MergeCells {
5 public static void main(String[] args) throws Exception {
6
7 //Create an object of Document class and load the sample document.
8 Document document = new Document();
9 document.loadFromFile("Sample.docx");
10
11 //Get the first section
12 Section section = document.getSections().get(0);
13
14 //Get the first table
15 Table table = section.getTables().get(0);
16
17 //Invoike mergeCell()method to merge cells vertically
18 mergeCell(table, false, 0, 1, 3);
19
20 //Invoike mergeCell()method to merge cell horizontally
21 mergeCell(table, true, 0, 3, 4);
22
23 //Save the document to file
24 document.saveToFile("MergeTable.docx",FileFormat.Docx_2013);
25 }
26
27 //Customize a mergeCell() method to remove the duplicate values while merging cells
28 public static void mergeCell(Table table, boolean isHorizontalMerge, int index, int start, int end) {
29
30 if (isHorizontalMerge) {
31 //Get a cell from table
32 TableCell firstCell = table.get(index, start);
33 //Invoke getCellText() method to get the cell’s text
34 String firstCellText = getCellText(firstCell);
35 for (int i = start + 1; i <= end; i++) {
36 TableCell cell1 = table.get(index, i);
37 //Check if the text is the same as the first cell
38 if (firstCellText.equals(getCellText(cell1))) {
39 //If yes, clear all the paragraphs in the cell
40 cell1.getParagraphs().clear();
41 }
42 }
43 //Merge cells horizontally
44 table.applyHorizontalMerge(index, start, end);
45
46 }
47 else {
48 TableCell firstCell = table.get(start, index);
49 String firstCellText = getCellText(firstCell);
50 for (int i = start + 1; i <= end; i++) {
51 TableCell cell1 = table.get(i, index);
52 if (firstCellText.equals(getCellText(cell1))) {
53 cell1.getParagraphs().clear();
54 }
55 }
56 //Merge cells vertically
57 table.applyVerticalMerge(index, start, end);
58 }
59 }
60 public static String getCellText(TableCell cell) {
61
62 StringBuilder text = new StringBuilder();
63 //Traverse all the paragraphs of a cell
64 for (int i = 0; i < cell.getParagraphs().getCount(); i++) {
65 //Get every paragraph’s text and append it to StringBuilder
66 text.append(cell.getParagraphs().get(i).getText().trim());
67 }
68 return text.toString();
69 }
70 }
【效果图】
Java删除word合并单元格时的重复值的更多相关文章
- 20190407 Word合并单元格
很长一段时间没处理word合并单元格,又忘记了采取忽略错误的方式测试出相应单元格的行列坐标这种方式.真是浪费时间.以后再也不想为此在深夜熬命. 今晚算是和它杠上了,很想弄清楚合并单元格之后行列坐标重新 ...
- java使用freemarker模板导出word(带有合并单元格)文档
来自:https://blog.csdn.net/qq_33195578/article/details/73790283 前言:最近要做一个导出word功能,其实网上有很多的例子,但是我需要的是合并 ...
- java导出标题多行且合并单元格的EXCEL
场景:项目中遇到有需要导出Excel的需求,并且是多行标题且有合并单元格的,参考网上的文章,加上自己的理解,封装成了可自由扩展的导出工具 先上效果,再贴代码: 调用工具类进行导出: public st ...
- C# 获取Excel中的合并单元格
C# 获取Excel中的合并单元格 我们在制作表格时,有时经常需要合并及取消合并一些单元格.在取消合并单元格时需要逐个查找及取消,比较麻烦.这里分享一个简单的方法来识别Excel中的合并单元格,识别这 ...
- NPOI操作EXCEL(五)——含合并单元格复杂表头的EXCEL解析
我们在第三篇文章中谈到了那些非常反人类的excel模板,博主为了养家糊口,也玩命做出了相应的解析方法... 我们先来看看第一类复杂表头: ...... 博主称这类excel模板为略复杂表头模板(蓝色部 ...
- NPOI扩展--判断指定单元格是否为合并单元格和输出该单元格的行列跨度(维度)
因工作需要用到跨合并单元格获取数据,所以写了个NPOI扩展类. 主要方法如下: 1.判断指定行/列索引(单元格)是否为合并单元格. 2.获取指定列索引的实际含有数据的单元格. 3.返回指定行/列索引的 ...
- poi 合并单元格、设置边框
HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet = wb.createSheet(); //创建一个样式 HSSFCellStyle sty ...
- apache poi合并单元格设置边框
HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet = wb.createSheet(); //创建一个样式 HSSFCellStyle sty ...
- python-利用xlrd模块中读取有合并单元格的excel数据
前言 对于excel中有合并单元格的情况,合并的单元格只能取到第一个单元格的值,合并的单元格后面的单元格内容的值为空,针对这个情况,写了下面一段代码实现, 对单元格进行判断,如果是传入的索引是合并单元 ...
随机推荐
- 弹性布局( display: flex;)
参考: https://www.cnblogs.com/hellocd/p/10443237.html
- 小A的柱状图_via牛客网
题目 链接:https://ac.nowcoder.com/acm/contest/28537/Q 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语 ...
- 第十四天python3 面向对象
1.面向对象 是对现实世界中的事物进行抽象的方式: 一切皆对象: 对象是数据和操作的封装: 对象之间相互独立,但也可以相互作用: 三要素: 封装: 数据与方法的集合: 提供一个或者多个接口来访问:隐藏 ...
- php apache 和mysql查看版本常用方法收集
php: 1.命令行查询,下图是因为添加php进系统环境变量了 2.预定义常量PHP_VERSION查询 3.phpversion()函数查询 4.phpinfo()查询 apache: mysql: ...
- JavaScript 里三个点 ...,可不是省略号啊···
摘要:Three dots ( - ) in JavaScript. 本文分享自华为云社区<JavaScript 里三个点 ... 的用法>,作者: Jerry Wang . Rest P ...
- Linux系列之压缩命令
前言 Linux 有三个常用的压缩命令:gzip.bzip2和 tar .本文介绍它们的区别和简单用法. 压缩文件 数据压缩是去除数据中冗余部分的过程,需要通过压缩算法完成.这些算法可分为两类: 无损 ...
- 无痕模式下 this.StorageManager.setItem) 本地存储丢失
在无痕模式下,存的this.StorageManager.setItem("recharge", JSON.stringify(recharge))本地存储会丢失,所以我们改成使用 ...
- MySQL通配符与正则表达式
通配符 通配符必须全文匹配时才为真,使用LIKE关键字 字符 示例 含义 _ "a_b" 任意一个字符"axb",其中x可以使任意字符,包括汉字 % " ...
- 从0搭建Vue3组件库:button组件
button组件几乎是每个组件库都有的:其实实现一个button组件是很简单的.本篇文章将带你一步一步的实现一个button组件.如果你想了解完整的组件库搭建,你可以先看使用Vite和TypeScri ...
- bulk collect 在KingbaseES和Oracle的使用方法比较
概述 BULK COLLECT 子句会批量检索结果,即一次性将结果集绑定到一个集合变量中,并从SQL引擎发送到PL/SQL引擎.通常可以在SELECT INTO.FETCH INTO以及RETURNI ...