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中有合并单元格的情况,合并的单元格只能取到第一个单元格的值,合并的单元格后面的单元格内容的值为空,针对这个情况,写了下面一段代码实现, 对单元格进行判断,如果是传入的索引是合并单元 ...
随机推荐
- Taurus.MVC 如何升级并运行在NET6、NET7
前言: 之前计划帮某公司架构一个从WPF转向Web的低代码的开发平台,并构思为Taurus.MVC 新增微服务的基础功能模块,提供便捷的微服务开发方式,因中途合作中止,代码开发部分后续再上. 最近看到 ...
- ACWing94.递归实现指数型枚举
https://www.acwing.com/problem/content/description/94/ 题面 \92. 递归实现指数型枚举 从 1∼n 这 n 个整数中随机选取任意多个,输出所有 ...
- 【Azure 应用服务】部署Kafka Trigger Function到Azure Function服务中,解决自定义域名解析难题
问题描述 经过前两篇文章,分别使用VM搭建了Kafka服务,创建了Azure Function项目,并且都在本地运行成功. [Azure Developer]在Azure VM (Windows) 中 ...
- iNeuOS工业互联网操作系统,设备运维业务和“低代码”表单开发工具
目 录 1. 概述... 2 2. 设备运维业务... 3 3. "低代码"表单开发工具... 6 1. 概述 iNeuOS工业互联网 ...
- linux学习随笔3之linux安全
1.history显示时间,用户和命令 vim /etc/profile export HISTTIMEFORMAT=" %F %T `who -u am i 2>/dev/null| ...
- .NET 7 发布的最后一个预览版Preview 7, 下个月发布RC
微软在2022年8月9日 发布了.NET 7 Preview 7[1],这是它在11月10日 RTM 之前进入发布候选阶段之前的最后预览版. 预览版 7 已在 Visual Studio 17.4 预 ...
- 解决linux下U盘变成只读模式
在最近的一个项目里,需要将linux下的些文件拷贝出来.插入U盘后,发现是只读模式. U盘可以写出,不能写入. 折腾了一翻: 最后这样解决的: 将U盘插入到windows下 执行 chkdsk g: ...
- 手把手教你分析MySQL查询性能瓶颈,包教包会
当一条SQL执行较慢,需要分析性能瓶颈,到底慢在哪? 我们一般会使用Explain查看其执行计划,从执行计划中得知这条SQL有没有使用索引?使用了哪个索引? 但是执行计划显示内容不够详细,如果显示用到 ...
- Rust 从入门到精通05-数据类型
Rust 是 静态类型(statically typed)语言,也就是说在编译时就必须知道所有变量的类型. 在 Rust 中,每一个值都属于某一个 数据类型(data type),分为两大类: ①.标 ...
- 048_末晨曦Vue技术_处理边界情况之使用$root访问根实例
处理边界情况之使用$root访问根实例 点击打开视频教程 在每个 new Vue 实例的子组件中,其根实例可以通过 $root property 进行访问. 例如,在这个根实例中: src\main. ...