如何使用Java实现Word文档文本的增删改格式化操作?

2026-05-24 05:221阅读0评论SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计1048个文字,预计阅读时间需要5分钟。

如何使用Java实现Word文档文本的增删改格式化操作?

Spire.Cloud.SDK for Java提供了TextRangesApi接口,支持通过addTextRange()添加文本、deleteTextRange()删除文本、updateTextRangeText()替换文本、updateTextRangeFormat()格式化文本等功能。

Spire.Cloud.SDK for Java提供了TextRangesApi接口可通过addTextRange()添加文本、deleteTextRange()删除文本、updateTextRangeText()替换文本、updateTextRangeFormat()格式化文本等。本文将从以上方法介绍如何来实现对文本的操作。可参考以下步骤进行准备:

一、导入jar文件

创建Maven项目程序,通过maven仓库下载导入。以IDEA为例,新建Maven项目,在pom.xml文件中配置maven仓库路径,并指定spire.cloud.sdk的依赖,如下:

<repositories> <repository> <id>com.e-iceblue</id> <name>cloud</name> <url>repo.e-iceblue.cn/repository/maven-public/</url> </repository> </repositories> <dependencies> <dependency> <groupId> cloud </groupId> <artifactId>spire.cloud.sdk</artifactId> <version>3.5.0</version> </dependency> <dependency> <groupId> com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.8.1</version> </dependency> <dependency> <groupId> com.squareup.okapi.e-iceblue.cn"; static Configuration wordConfiguration = new Configuration(appId, appKey, baseUrl); static TextRangesApi textRangesApi = new TextRangesApi(wordConfiguration); public static void main(String[] args) throws ApiException { String name = "testfile.docx";//用于测试的Word源文档 String paragraphPath = "Section/0/Body/0/Paragraph/0";//获取文档中的段落 Integer indexInParagraph = 0; String text = "新添加的文本内容!";//指定需要添加的文本内容 String folder = "input";//源文档所在的云端文件夹 String storage = null;//冰蓝云存储空间 String password = null;//源文档密码 String destFilePath = "output/AddTextRange.docx";//结果文档路径 //调用方法添加文本内容到Word段落 textRangesApi.addTextRange(name, paragraphPath, text, destFilePath, folder, storage, indexInParagraph, password); } }

文本添加效果:

如何使用Java实现Word文档文本的增删改格式化操作?

2. 删除Word中的文本

import spire.cloud.word.sdk.client.ApiException; import spire.cloud.word.sdk.client.Configuration; import spire.cloud.word.sdk.client.api.TextRangesApi; public class DeleteTextRange { //配置App账号信息 static String appId = "App ID"; static String appKey = "App Key"; static String baseUrl = "api.e-iceblue.cn"; static Configuration wordConfiguration = new Configuration(appId, appKey, baseUrl); static TextRangesApi textRangesApi = new TextRangesApi(wordConfiguration); public static void main(String[] args) throws ApiException { String name = "testfile.docx";//源文档 String paragraphPath = "Section/0/Body/0/Paragraph/0";//获取段落 Integer index = 0; String folder = "input";//源文档所在文件夹 String storage = null;//冰蓝云存储空间 String password = null;//源文档密码 String destFilePath = "output/DeleteTextRange.docx";//结果文档路径 //调用方法删除Word第一段文本 textRangesApi.deleteTextRange(name, paragraphPath, index, destFilePath,folder, storage, password); } }

文本删除效果:

3. 替换Word中的文本

import spire.cloud.word.sdk.client.ApiException; import spire.cloud.word.sdk.client.Configuration; import spire.cloud.word.sdk.client.api.TextRangesApi; public class UpdateTextRange { //配置App账号信息 static String appId = "App ID"; static String appKey = "App Key"; static String baseUrl = "api.e-iceblue.cn"; static Configuration wordConfiguration = new Configuration(appId, appKey, baseUrl); static TextRangesApi textRangesApi = new TextRangesApi(wordConfiguration); public static void main(String[] args) throws ApiException { String name = "testfile.docx";//源文档 String paragraphPath = "Section/0/Body/0/Paragraph/0";//获取段落 Integer index = 0; String text = "新替换文本";//指定新文本 String folder = "input";//源文档所在文件夹 String storage = null; String password = null; String destFilePath = "output/UpdateTextRangeText.docx";//结果文档路径 //调用方法更新(替换)原有的文本 textRangesApi.updateTextRangeText(name, paragraphPath, index, text, destFilePath, folder, storage, password); } }

文本替换效果:

4. 格式化Word中的文本

import spire.cloud.word.sdk.client.ApiException; import spire.cloud.word.sdk.client.Configuration; import spire.cloud.word.sdk.client.api.TextRangesApi; import spire.cloud.word.sdk.client.model.Color; import spire.cloud.word.sdk.client.model.Font; import spire.cloud.word.sdk.client.model.TextRangeFormat; public class UpdateTextRangeFormat { //配置App账号信息 static String appId = "App ID"; static String appKey = "App Key"; static String baseUrl = "api.e-iceblue.cn"; static Configuration wordConfiguration = new Configuration(appId, appKey, baseUrl); static TextRangesApi textRangesApi = new TextRangesApi(wordConfiguration); public static void main(String[] args) throws ApiException { String name = "testfile.docx";//源文档 String paragraphPath = "Section/0/Body/0/Paragraph/0";//获取段落 Integer index = 0; //创建文本样式,指定字体、颜色、字号,并应用到文本 TextRangeFormat format = new TextRangeFormat(); Color color = new Color(34,139,34); Font font = new Font("宋体", 20f, color); format.setFont(font); TextRangeFormat textRange = format; String folder = "input";//源文档所在文件夹 String storage = null; String password = null; String destFilePath = "output/UpdateTextRangeFormat.docx";//结果文档路径 //调用方法更新(应用)文本样式 textRangesApi.updateTextRangeFormat(name, paragraphPath, index, textRange, destFilePath, folder, storage, password); } }

文本格式设置效果:

到此这篇关于Java 添加、删除、替换、格式化Word中的文本的步骤详解(基于Spire.Cloud.SDK for Java)的文章就介绍到这了,更多相关Java 添加、删除、替换、格式化Word中的文本内容请搜索易盾网络以前的文章或继续浏览下面的相关文章希望大家以后多多支持易盾网络!

标签:文本

本文共计1048个文字,预计阅读时间需要5分钟。

如何使用Java实现Word文档文本的增删改格式化操作?

Spire.Cloud.SDK for Java提供了TextRangesApi接口,支持通过addTextRange()添加文本、deleteTextRange()删除文本、updateTextRangeText()替换文本、updateTextRangeFormat()格式化文本等功能。

Spire.Cloud.SDK for Java提供了TextRangesApi接口可通过addTextRange()添加文本、deleteTextRange()删除文本、updateTextRangeText()替换文本、updateTextRangeFormat()格式化文本等。本文将从以上方法介绍如何来实现对文本的操作。可参考以下步骤进行准备:

一、导入jar文件

创建Maven项目程序,通过maven仓库下载导入。以IDEA为例,新建Maven项目,在pom.xml文件中配置maven仓库路径,并指定spire.cloud.sdk的依赖,如下:

<repositories> <repository> <id>com.e-iceblue</id> <name>cloud</name> <url>repo.e-iceblue.cn/repository/maven-public/</url> </repository> </repositories> <dependencies> <dependency> <groupId> cloud </groupId> <artifactId>spire.cloud.sdk</artifactId> <version>3.5.0</version> </dependency> <dependency> <groupId> com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.8.1</version> </dependency> <dependency> <groupId> com.squareup.okapi.e-iceblue.cn"; static Configuration wordConfiguration = new Configuration(appId, appKey, baseUrl); static TextRangesApi textRangesApi = new TextRangesApi(wordConfiguration); public static void main(String[] args) throws ApiException { String name = "testfile.docx";//用于测试的Word源文档 String paragraphPath = "Section/0/Body/0/Paragraph/0";//获取文档中的段落 Integer indexInParagraph = 0; String text = "新添加的文本内容!";//指定需要添加的文本内容 String folder = "input";//源文档所在的云端文件夹 String storage = null;//冰蓝云存储空间 String password = null;//源文档密码 String destFilePath = "output/AddTextRange.docx";//结果文档路径 //调用方法添加文本内容到Word段落 textRangesApi.addTextRange(name, paragraphPath, text, destFilePath, folder, storage, indexInParagraph, password); } }

文本添加效果:

如何使用Java实现Word文档文本的增删改格式化操作?

2. 删除Word中的文本

import spire.cloud.word.sdk.client.ApiException; import spire.cloud.word.sdk.client.Configuration; import spire.cloud.word.sdk.client.api.TextRangesApi; public class DeleteTextRange { //配置App账号信息 static String appId = "App ID"; static String appKey = "App Key"; static String baseUrl = "api.e-iceblue.cn"; static Configuration wordConfiguration = new Configuration(appId, appKey, baseUrl); static TextRangesApi textRangesApi = new TextRangesApi(wordConfiguration); public static void main(String[] args) throws ApiException { String name = "testfile.docx";//源文档 String paragraphPath = "Section/0/Body/0/Paragraph/0";//获取段落 Integer index = 0; String folder = "input";//源文档所在文件夹 String storage = null;//冰蓝云存储空间 String password = null;//源文档密码 String destFilePath = "output/DeleteTextRange.docx";//结果文档路径 //调用方法删除Word第一段文本 textRangesApi.deleteTextRange(name, paragraphPath, index, destFilePath,folder, storage, password); } }

文本删除效果:

3. 替换Word中的文本

import spire.cloud.word.sdk.client.ApiException; import spire.cloud.word.sdk.client.Configuration; import spire.cloud.word.sdk.client.api.TextRangesApi; public class UpdateTextRange { //配置App账号信息 static String appId = "App ID"; static String appKey = "App Key"; static String baseUrl = "api.e-iceblue.cn"; static Configuration wordConfiguration = new Configuration(appId, appKey, baseUrl); static TextRangesApi textRangesApi = new TextRangesApi(wordConfiguration); public static void main(String[] args) throws ApiException { String name = "testfile.docx";//源文档 String paragraphPath = "Section/0/Body/0/Paragraph/0";//获取段落 Integer index = 0; String text = "新替换文本";//指定新文本 String folder = "input";//源文档所在文件夹 String storage = null; String password = null; String destFilePath = "output/UpdateTextRangeText.docx";//结果文档路径 //调用方法更新(替换)原有的文本 textRangesApi.updateTextRangeText(name, paragraphPath, index, text, destFilePath, folder, storage, password); } }

文本替换效果:

4. 格式化Word中的文本

import spire.cloud.word.sdk.client.ApiException; import spire.cloud.word.sdk.client.Configuration; import spire.cloud.word.sdk.client.api.TextRangesApi; import spire.cloud.word.sdk.client.model.Color; import spire.cloud.word.sdk.client.model.Font; import spire.cloud.word.sdk.client.model.TextRangeFormat; public class UpdateTextRangeFormat { //配置App账号信息 static String appId = "App ID"; static String appKey = "App Key"; static String baseUrl = "api.e-iceblue.cn"; static Configuration wordConfiguration = new Configuration(appId, appKey, baseUrl); static TextRangesApi textRangesApi = new TextRangesApi(wordConfiguration); public static void main(String[] args) throws ApiException { String name = "testfile.docx";//源文档 String paragraphPath = "Section/0/Body/0/Paragraph/0";//获取段落 Integer index = 0; //创建文本样式,指定字体、颜色、字号,并应用到文本 TextRangeFormat format = new TextRangeFormat(); Color color = new Color(34,139,34); Font font = new Font("宋体", 20f, color); format.setFont(font); TextRangeFormat textRange = format; String folder = "input";//源文档所在文件夹 String storage = null; String password = null; String destFilePath = "output/UpdateTextRangeFormat.docx";//结果文档路径 //调用方法更新(应用)文本样式 textRangesApi.updateTextRangeFormat(name, paragraphPath, index, textRange, destFilePath, folder, storage, password); } }

文本格式设置效果:

到此这篇关于Java 添加、删除、替换、格式化Word中的文本的步骤详解(基于Spire.Cloud.SDK for Java)的文章就介绍到这了,更多相关Java 添加、删除、替换、格式化Word中的文本内容请搜索易盾网络以前的文章或继续浏览下面的相关文章希望大家以后多多支持易盾网络!

标签:文本