替换 Word 文档中的文本。
接口方法 1:replaceWithRegexInRequest() 使用正则表达式替换文本
HTTP 请求方法:PUT
请求 URL:https://api.e-iceblue.cn/v1/word/document/replaceWithRegexInRequest
请求参数:
参数 | 是否必选 | 类型 | 可选值范围 | 说明 |
file | 是 | file | 原文档 | |
password | 否 | string | 原文档密码,没有则为null | |
regex | 是 | string | 用于匹配文本的正则表达式 | |
newValue | 是 | string | 替换进去的新文本 |
代码示例:
- .NET
- Java
using Spire.Cloud.Word.Sdk.Api;
using Spire.Cloud.Word.Sdk.Client;
using System.IO;
namespace ReplaceWithRegexInRequest
{
class Program
{
static string appId = "your id";
static string appKey = "your key";
static string baseUrl = "https://api.e-iceblue.cn";
static Configuration wordConfiguration = new Configuration(appId, appKey, baseUrl);
static ReplaceTextApi replaceTextApi = new ReplaceTextApi(wordConfiguration);
static void Main(string[] args)
{
string inputFilePath = "D:/inputFile/ReplaceText.docx";
Stream inputFile = new FileStream(inputFilePath, FileMode.Open);
string regex = @"\#\w+\b";
string newValue = "Spire.DOC";
string password = null;
var response = replaceTextApi.ReplaceWithRegexInRequest(inputFile, regex, newValue, password);
}
}
}
import spire.cloud.word.sdk.client.*;
import spire.cloud.word.sdk.client.api.ReplaceTextApi;
import java.io.File;
public class RepalceWithRegexInRequest {
static String appId = "your id";
static String appKey = "your key";
static String baseUrl = "https://api.e-iceblue.cn";
static Configuration wordConfiguration = new Configuration(appId, appKey, baseUrl);
static ReplaceTextApi replaceTextApi = new ReplaceTextApi(wordConfiguration);
public static void main(String[] args) throws ApiException {
String inputFilePath = "D:/inputFile/ReplaceText.docx";
File inputFile = new File(inputFilePath);
String regex = "\\#\\w+\\b";
String newValue = "Spire.DOC";
String password = null;
File response = replaceTextApi.replaceWithRegexInRequest(inputFile, regex, newValue, password);
}
}
接口方法 2:replaceWithRegex() 使用正则表达式替换文本
HTTP 请求方法:PUT
请求 URL:https://api.e-iceblue.cn/v1/word/document/{name}/replaceWithRegex
请求参数:
参数 | 是否必选 | 类型 | 可选值范围 | 说明 |
name | 是 | string | 原文档名称 | |
password | 否 | string | 原文档密码,没有则为null | |
folder | 否 | string | 存放原文档的文件夹,没有则为null | |
storage | 否 | string | 文档存储空间,使用冰蓝云配置的2G空间存贮文档,可设置为null | |
regex | 是 | string | 用于匹配文本的正则表达式 | |
newValue | 是 | string | 替换进去的新文本 | |
destFilePath | 是 | string | 结果文档的存贮路径,如果省略该参数,则默认存到根目录 |
代码示例:
- .NET
- Java
using Spire.Cloud.Word.Sdk.Api;
using Spire.Cloud.Word.Sdk.Client;
namespace ReplaceWithRegex
{
class Program
{
static string appId = "your id";
static string appKey = "your key";
static string baseUrl = "https://api.e-iceblue.cn";
static Configuration wordConfiguration = new Configuration(appId, appKey, baseUrl);
static ReplaceTextApi replaceTextApi = new ReplaceTextApi(wordConfiguration);
static void Main(string[] args)
{
string name = "ReplaceText.docx";
string regex = @"\#\w+\b";
string newValue = "E-iceblue";
string password = null;
string folder = "input";
string storage = null;
string destFilePath = "output/ReplaceWithRegex_output.docx";
replaceTextApi.ReplaceWithRegex(name, regex, newValue, destFilePath, password, folder, storage);
}
}
}
import spire.cloud.word.sdk.client.*;
import spire.cloud.word.sdk.client.api.ReplaceTextApi;
public class ReplaceWithRegex {
static String appId = "your id";
static String appKey = "your key";
static String baseUrl = "https://api.e-iceblue.cn";
static Configuration wordConfiguration = new Configuration(appId, appKey, baseUrl);
static ReplaceTextApi replaceTextApi = new ReplaceTextApi(wordConfiguration);
public static void main(String[] args) throws ApiException {
String name = "ReplaceText.docx";
String regex = "\\#\\w+\\b";
String newValue = "E-iceblue";
String password = null;
String folder = "input";
String storage = null;
String destFilePath = "output/ReplaceWithRegex_output.docx";
replaceTextApi.replaceWithRegex(name, regex, newValue, destFilePath, password, folder, storage);
}
}
接口方法 3: replaceWithTextInRequest() 使用文本进行替换
HTTP 请求方法:PUT
请求 URL:https://api.e-iceblue.cn/v1/word/document/replaceInRequest
请求参数:
参数 | 是否必选 | 类型 | 可选值范围 | 说明 |
file | 是 | file | 原文档 | |
password | 否 | string | 原文档密码,没有则为null | |
oldValue | 是 | string | 被替换的旧文本 | |
newValue | 是 | string | 替换进去的新文本 | |
matchCase | 否 | boolean | 是否匹配大小写,默认不匹配 | |
matchWholeWord | 否 | boolean | 是否在整个文档中进行替换,默认为否 | |
replaceFirst | 否 | boolean | 是否替换第一个出处 |
代码示例:
- .NET
- Java
using Spire.Cloud.Word.Sdk.Api;
using Spire.Cloud.Word.Sdk.Client;
using System.IO;
namespace ReplaceWithTextInRequest
{
class Program
{
static string appId = "your id";
static string appKey = "your key";
static string baseUrl = "https://api.e-iceblue.cn";
static Configuration wordConfiguration = new Configuration(appId, appKey, baseUrl);
static ReplaceTextApi replaceTextApi = new ReplaceTextApi(wordConfiguration);
static void Main(string[] args)
{
string inputFilePath = "D:/inputFile/ReplaceText.docx";
Stream inputFile = new FileStream(inputFilePath, FileMode.Open);
string oldValue = "#test";
string newValue = "Spire.DOC";
string password = null;
bool? matchCase = false;
bool? matchWholeWord = null;
bool? replaceFirst = null;
var response = replaceTextApi.ReplaceWithTextInRequest(inputFile, oldValue, newValue, password, matchCase, matchWholeWord, replaceFirst);
}
}
}
import spire.cloud.word.sdk.client.*;
import spire.cloud.word.sdk.client.api.ReplaceTextApi;
import java.io.File;
public class ReplaceWithTextInRequest{
static String appId = "your id";
static String appKey = "your key";
static String baseUrl = "https://api.e-iceblue.cn";
static Configuration wordConfiguration = new Configuration(appId, appKey, baseUrl);
static ReplaceTextApi replaceTextApi = new ReplaceTextApi(wordConfiguration);
public static void main(String[] args) throws ApiException {
String inputFilePath = "D:/inputFile/ReplaceText.docx";
File inputFile = new File(inputFilePath);
String oldValue = "#test";
String newValue = "Spire.DOC";
String password = null;
Boolean matchCase = false;
Boolean matchWholeWord = null;
Boolean replaceFirst = null;
File response = replaceTextApi.replaceWithTextInRequest(inputFile, oldValue, newValue, password, matchCase, matchWholeWord, replaceFirst);
}
}
接口方法 4: replaceWithText() 使用文本进行替换
HTTP 请求方法:PUT
请求 URL:https://api.e-iceblue.cn/v1/word/document/replaceWithText
请求参数:
参数 | 是否必选 | 类型 | 可选值范围 | 说明 |
file | 是 | file | 原文档 | |
password | 否 | string | 原文档密码,没有则为null | |
oldValue | 是 | string | 被替换的旧文本 | |
newValue | 是 | string | 替换进去的新文本 | |
matchCase | 否 | boolean | 是否匹配大小写,默认不匹配 | |
matchWholeWord | 否 | boolean | 是否在整个文档中进行替换,默认为否 | |
replaceFirst | 否 | boolean | 是否替换第一个出处 | |
destFilePath | 是 | string | 结果文档的存贮路径,如果省略该参数,则默认存到根目录 |
代码示例:
- .NET
- Java
using Spire.Cloud.Word.Sdk.Api;
using Spire.Cloud.Word.Sdk.Client;
namespace ReplaceWithText
{
class Program
{
static string appId = "your id";
static string appKey = "your key";
static string baseUrl = "https://api.e-iceblue.cn";
static Configuration wordConfiguration = new Configuration(appId, appKey, baseUrl);
static ReplaceTextApi replaceTextApi = new ReplaceTextApi(wordConfiguration);
static void Main(string[] args)
{
string name = "ReplaceText.docx";
string oldValue = "#test";
string newValue = "Spire.DOC";
string password = null;
string folder = "input";
string storage = null;
bool matchCase = false;
bool? matchWholeWord = null;
bool? replaceFirst = null;
string destFilePath = "output/ReplaceWithText_output.docx"; ;
replaceTextApi.ReplaceWithText(name, oldValue, newValue, destFilePath, password, folder, storage, matchCase, matchWholeWord, replaceFirst);
}
}
}
import spire.cloud.word.sdk.client.*;
import spire.cloud.word.sdk.client.api.ReplaceTextApi;
public class ReplaceWithText{
static String appId = "your id";
static String appKey = "your key";
static String baseUrl = "https://api.e-iceblue.cn";
static Configuration wordConfiguration = new Configuration(appId, appKey, baseUrl);
static ReplaceTextApi replaceTextApi = new ReplaceTextApi(wordConfiguration);
public static void main(String[] args) throws ApiException {
String name = "ReplaceText.docx";
String oldValue = "#test";
String newValue = "Spire.DOC";
String password = null;
String folder = "input";
String storage = null;
Boolean matchCase = false;
Boolean matchWholeWord = null;
Boolean replaceFirst = null;
String destFilePath = "output/ReplaceWithText_output.docx"; ;
replaceTextApi.replaceWithText(name, oldValue, newValue, destFilePath, password, folder, storage, matchCase, matchWholeWord, replaceFirst);
}
}