网站首页 > 教程文章 正文
程序环境:
方法1:手动引入。将 Free Spire.Doc for Java下载到本地,解压,找到lib文件夹下的Spire.Doc.jar文件。在IDEA中打开如下界面,将本地路径中的jar文件引入Java程序
方法2: 如果您想通过 Maven 安装,则可以在 pom.xml 文件中添加以下代码导入 JAR 文件。
<pre class="prettyprint hljs xml" deep="5" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto;"><repositories>
<repository>
<id>com.e-iceblue</id>
<url>https://repo.e-iceblue.cn/repository/maven-public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.doc.free</artifactId>
<version>5.2.0</version>
</dependency>
</dependencies>
具体步骤:
- 创建Document类的对象并使用 Document.loadFromFile() 方法加载示例文档。
- 用 Document.getSections() 方法获取节集合,然后使用 SectionCollection.get() 方法获取特定节。
- 用 Section.getTables() 方法获取表集合,然后使用 TableCollection.get() 方法获取所需的表。
- 调用 mergeCell(Table table, boolean isHorizontalMerge, int index, int start, int end) 方法垂直或水平合并表格单元格。该方法将判断要合并的单元格是否具有相同的值,并在合并后的单元格中只保留一个值。
- 使用 Document.saveToFile() 方法保存文档。
完整代码:
【Java】
<pre class="prettyprint hljs verilog" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto;">import com.spire.doc.*;
public class RemoveDuplicateValues {
public static void main(String[] args) throws Exception {
//创建Document类的对象并加载示例文档。
Document document = new Document();
document.loadFromFile("水果信息.docx");
//获取第一节
Section section = document.getSections().get(0);
//获取第一个表格
Table table = section.getTables().get(0);
//调用mergeCell()方法纵向合并单元格
mergeCell(table, false, 0, 1, 3);
//调用mergeCell()方法横向合并单元格
mergeCell(table, true, 0, 0, 1);
//保存文档
document.saveToFile("输出文档.docx",FileFormat.Docx_2013);
}
//自定义合并的 Cell() 方法以在合并单元格时删除重复值
public static void mergeCell(Table table, boolean isHorizontalMerge, int index, int start, int end) {
if (isHorizontalMerge) {
//从表格获取单元格
TableCell firstCell = table.get(index, start);
//调用 getCellText() 方法获取单元格的文本
String firstCellText = getCellText(firstCell);
for (int i = start + 1; i <= end; i++) {
TableCell cell1 = table.get(index, i);
//检查文本是否与第一个单元格相同
if (firstCellText.equals(getCellText(cell1))) {
//如果是,清除单元格中的所有段落
cell1.getParagraphs().clear();
}
}
//横向合并单元格
table.applyHorizontalMerge(index, start, end);
}
else {
TableCell firstCell = table.get(start, index);
String firstCellText = getCellText(firstCell);
for (int i = start + 1; i <= end; i++) {
TableCell cell1 = table.get(i, index);
if (firstCellText.equals(getCellText(cell1))) {
cell1.getParagraphs().clear();
}
}
//纵向合并单元格
table.applyVerticalMerge(index, start, end);
}
}
public static String getCellText(TableCell cell) {
StringBuilder text = new StringBuilder();
//遍历单元格中的段落
for (int i = 0; i < cell.getParagraphs().getCount(); i++) {
//获取每个段落的文本并将其附加到 StringBuilder
text.append(cell.getParagraphs().get(i).getText().trim());
}
return text.toString();
}
}
效果图:
猜你喜欢
- 2025-07-17 一课译词:一刀两断(一刀两断成语解释)
- 2025-07-17 核心短语break up用法解析(breakd短语)
- 2025-07-17 HTML+CSS 实现商品图片列表放大视觉效果 复制完整代码即可马上调用
- 2025-07-17 前端实现右键自定义菜单(前端实现右键自定义菜单怎么设置)
- 2025-07-17 Python中docx与docxcompose批量合并多个Word文档并添加分页符
- 2025-07-17 Java 将Excel转为XML(java将xls转换成xlsx)
- 2025-07-17 jq+ajax+bootstrap改了一个动态分页的表格
- 2025-07-17 css兼容性问题及一些常见问题汇总大全,赶快收藏!
- 2025-07-17 Java 的业务逻辑验证框架 之-fluent-validator
- 2025-07-17 小程序cover-view踩坑系列2(微信小程序overflow)
- 最近发表
-
- 一课译词:一刀两断(一刀两断成语解释)
- 核心短语break up用法解析(breakd短语)
- HTML+CSS 实现商品图片列表放大视觉效果 复制完整代码即可马上调用
- 前端实现右键自定义菜单(前端实现右键自定义菜单怎么设置)
- Python中docx与docxcompose批量合并多个Word文档并添加分页符
- Java 将Excel转为XML(java将xls转换成xlsx)
- jq+ajax+bootstrap改了一个动态分页的表格
- css兼容性问题及一些常见问题汇总大全,赶快收藏!
- Java 的业务逻辑验证框架 之-fluent-validator
- 小程序cover-view踩坑系列2(微信小程序overflow)
- 标签列表
-
- location.href (44)
- document.ready (36)
- git checkout -b (34)
- 跃点数 (35)
- 阿里云镜像地址 (33)
- qt qmessagebox (36)
- mybatis plus page (35)
- vue @scroll (38)
- 堆栈区别 (33)
- 什么是容器 (33)
- sha1 md5 (33)
- navicat导出数据 (34)
- 阿里云acp考试 (33)
- 阿里云 nacos (34)
- redhat官网下载镜像 (36)
- srs服务器 (33)
- pico开发者 (33)
- https的端口号 (34)
- vscode更改主题 (35)
- 阿里云资源池 (34)
- os.path.join (33)
- redis aof rdb 区别 (33)
- 302跳转 (33)
- http method (35)
- js array splice (33)