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. 1 <repositories>
  2. 2 <repository>
  3. 3 <id>com.e-iceblue</id>
  4. 4 <url>https://repo.e-iceblue.cn/repository/maven-public/</url>
  5. 5 </repository>
  6. 6 </repositories>
  7. 7 <dependencies>
  8. 8 <dependency>
  9. 9 <groupId>e-iceblue</groupId>
  10. 10 <artifactId>spire.doc</artifactId>
  11. 11 <version>10.9.0</version>
  12. 12 </dependency>
  13. 13 </dependencies>

方法3:通过NuGet安装。可通过以下2种方法安装:

(1)可以在Visual Studio中打开“解决方案资源管理器”,鼠标右键点击“引用”,“管理NuGet包”,然后搜索“FreeSpire.Doc”,点击“安装”。等待程序安装完成。

(2)将以下内容复制到PM控制台安装。

Install-Package FreeSpire.Doc -Version 10.9.0

删除合并单元格时的重复值

  1. 创建一个Document实例,使用Document.loadFromFile()方法加载示例文档。
  2. 使用Document.getSections()方法获得节集合,然后使用SectionCollection.get()方法获得特定的节。
  3. 使用Section.getTables()方法获得表格集合,然后使用TableCollection.get()方法获得想要的表。
  4. 调用mergeCell(Table table,boolean isHorizontalMerge,int index,int start,int end)方法来垂直或水平地合并表格单元格。这个方法将确定要合并的单元格是否有相同的值,并在合并的单元格中只保留一个值。
  5. 使用Document.saveToFile()方法保存文件。

代码示例

  1. 1 import com.spire.doc.*;
  2. 2 import com.spire.doc.interfaces.ITable;
  3. 3
  4. 4 public class MergeCells {
  5. 5 public static void main(String[] args) throws Exception {
  6. 6
  7. 7 //Create an object of Document class and load the sample document.
  8. 8 Document document = new Document();
  9. 9 document.loadFromFile("Sample.docx");
  10. 10
  11. 11 //Get the first section
  12. 12 Section section = document.getSections().get(0);
  13. 13
  14. 14 //Get the first table
  15. 15 Table table = section.getTables().get(0);
  16. 16
  17. 17 //Invoike mergeCell()method to merge cells vertically
  18. 18 mergeCell(table, false, 0, 1, 3);
  19. 19
  20. 20 //Invoike mergeCell()method to merge cell horizontally
  21. 21 mergeCell(table, true, 0, 3, 4);
  22. 22
  23. 23 //Save the document to file
  24. 24 document.saveToFile("MergeTable.docx",FileFormat.Docx_2013);
  25. 25 }
  26. 26
  27. 27 //Customize a mergeCell() method to remove the duplicate values while merging cells
  28. 28 public static void mergeCell(Table table, boolean isHorizontalMerge, int index, int start, int end) {
  29. 29
  30. 30 if (isHorizontalMerge) {
  31. 31 //Get a cell from table
  32. 32 TableCell firstCell = table.get(index, start);
  33. 33 //Invoke getCellText() method to get the cell’s text
  34. 34 String firstCellText = getCellText(firstCell);
  35. 35 for (int i = start + 1; i <= end; i++) {
  36. 36 TableCell cell1 = table.get(index, i);
  37. 37 //Check if the text is the same as the first cell
  38. 38 if (firstCellText.equals(getCellText(cell1))) {
  39. 39 //If yes, clear all the paragraphs in the cell
  40. 40 cell1.getParagraphs().clear();
  41. 41 }
  42. 42 }
  43. 43 //Merge cells horizontally
  44. 44 table.applyHorizontalMerge(index, start, end);
  45. 45
  46. 46 }
  47. 47 else {
  48. 48 TableCell firstCell = table.get(start, index);
  49. 49 String firstCellText = getCellText(firstCell);
  50. 50 for (int i = start + 1; i <= end; i++) {
  51. 51 TableCell cell1 = table.get(i, index);
  52. 52 if (firstCellText.equals(getCellText(cell1))) {
  53. 53 cell1.getParagraphs().clear();
  54. 54 }
  55. 55 }
  56. 56 //Merge cells vertically
  57. 57 table.applyVerticalMerge(index, start, end);
  58. 58 }
  59. 59 }
  60. 60 public static String getCellText(TableCell cell) {
  61. 61
  62. 62 StringBuilder text = new StringBuilder();
  63. 63 //Traverse all the paragraphs of a cell
  64. 64 for (int i = 0; i < cell.getParagraphs().getCount(); i++) {
  65. 65 //Get every paragraph’s text and append it to StringBuilder
  66. 66 text.append(cell.getParagraphs().get(i).getText().trim());
  67. 67 }
  68. 68 return text.toString();
  69. 69 }
  70. 70 }

效果图

 
注:测试代码中的文件路径为程序Debug路径,文件路径可自定义为其他路径。
 
—THE END—

Java删除word合并单元格时的重复值的更多相关文章

  1. 20190407 Word合并单元格

    很长一段时间没处理word合并单元格,又忘记了采取忽略错误的方式测试出相应单元格的行列坐标这种方式.真是浪费时间.以后再也不想为此在深夜熬命. 今晚算是和它杠上了,很想弄清楚合并单元格之后行列坐标重新 ...

  2. java使用freemarker模板导出word(带有合并单元格)文档

    来自:https://blog.csdn.net/qq_33195578/article/details/73790283 前言:最近要做一个导出word功能,其实网上有很多的例子,但是我需要的是合并 ...

  3. java导出标题多行且合并单元格的EXCEL

    场景:项目中遇到有需要导出Excel的需求,并且是多行标题且有合并单元格的,参考网上的文章,加上自己的理解,封装成了可自由扩展的导出工具 先上效果,再贴代码: 调用工具类进行导出: public st ...

  4. C# 获取Excel中的合并单元格

    C# 获取Excel中的合并单元格 我们在制作表格时,有时经常需要合并及取消合并一些单元格.在取消合并单元格时需要逐个查找及取消,比较麻烦.这里分享一个简单的方法来识别Excel中的合并单元格,识别这 ...

  5. NPOI操作EXCEL(五)——含合并单元格复杂表头的EXCEL解析

    我们在第三篇文章中谈到了那些非常反人类的excel模板,博主为了养家糊口,也玩命做出了相应的解析方法... 我们先来看看第一类复杂表头: ...... 博主称这类excel模板为略复杂表头模板(蓝色部 ...

  6. NPOI扩展--判断指定单元格是否为合并单元格和输出该单元格的行列跨度(维度)

    因工作需要用到跨合并单元格获取数据,所以写了个NPOI扩展类. 主要方法如下: 1.判断指定行/列索引(单元格)是否为合并单元格. 2.获取指定列索引的实际含有数据的单元格. 3.返回指定行/列索引的 ...

  7. poi 合并单元格、设置边框

    HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet = wb.createSheet(); //创建一个样式 HSSFCellStyle sty ...

  8. apache poi合并单元格设置边框

    HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet = wb.createSheet(); //创建一个样式 HSSFCellStyle sty ...

  9. python-利用xlrd模块中读取有合并单元格的excel数据

    前言 对于excel中有合并单元格的情况,合并的单元格只能取到第一个单元格的值,合并的单元格后面的单元格内容的值为空,针对这个情况,写了下面一段代码实现, 对单元格进行判断,如果是传入的索引是合并单元 ...

随机推荐

  1. 迭代器和增强for循环

    iterator 迭代:即Collection集合元素的通过获取方法,在获取元素之前先要判断集合中有没有元素,如果有就把这个元素取出来,然后在判断,如果还有就再去除卡u,一直把集合中的所有元素全部拿出 ...

  2. python中文官方文档记录

    随笔记录 python3.10中文官方文档百度网盘链接:https://pan.baidu.com/s/18XBjPzQTrZa5MLeFkT2whw?pwd=1013 提取码:1013 1.pyth ...

  3. 在生鲜零售业,DolphinScheduler 还能这么玩!

    点击上方 蓝字关注我们 ✎ 编 者 按 2021 年,Apache DolphinScheduler 社区又迎来了新的蓬勃发展,社区活跃度持续提高.目前,项目 GitHub Star 已达 6.7k, ...

  4. BZOJ4580/Luogu3147 [Usaco2016 Open]248

    amazing #include <iostream> #include <cstdio> #include <cstring> #include <algo ...

  5. ShardingSphere-JDBC实战

    一.环境准备 1.数据库 创建2个库2个表: xdclass_shop_order_0 product_order_0 product_order_1 ad_config product_order_ ...

  6. 网安等保-Linux服务器之最新Ubuntu-22.04-LTS系统内核优化与安全加固配置脚本使用分享

    关注「WeiyiGeek」公众号 设为「特别关注」每天带你玩转网络安全运维.应用开发.物联网IOT学习! 希望各位看友[关注.点赞.评论.收藏.投币],助力每一个梦想. 本章目录 目录 0x00 前言 ...

  7. 「题解报告」P2154 虔诚的墓主人

    P2154 虔诚的墓主人 题解 原题传送门 题意 在 \(n\times m\) 一个方格上给你 \(w\) 个点,求方格里每个点正上下左右各选 \(k\) 个点的方案数. \(1 \le N, M ...

  8. Python自学笔记11-函数的定义和调用

    函数是组织代码的非常有效的方式,有了函数,我们就可以编写大规模的项目.可以说,函数是组织代码的最小单元. Python函数的定义 函数是代码封装的一种手段,函数中包含一段可以重复执行的代码,在需要用到 ...

  9. 细数实现全景图VR的几种方式(panorama/cubemap/eac)

    Three.js系列: 在元宇宙看电影,享受 VR 视觉盛宴 Three.js系列: 造个海洋球池来学习物理引擎 Three.js系列: 游戏中的第一.三人称视角 Three.js系列: 数实现全景图 ...

  10. vscode 快速注释和撤回快捷键

    好家伙,天天忘,建议先练个十遍上手 1.快捷行注释 Ctrl + / 2.快捷块注释 Alt + Shift + A 3.撤回 Ctrl + Z 4.恢复撤回(撤回你的撤回) Ctrl + Shift ...