翻譯|使用教程|編輯:李顯亮|2021-04-01 11:53:34.400|閱讀 354 次
概述:該文將介紹如何使用正則表達(dá)式查找和替換Word 文檔中的文本。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
Spire.Doc for .NET是一個專業(yè)的Word .NET庫,設(shè)計用于幫助開發(fā)人員高效地開發(fā)創(chuàng)建、閱讀、編寫、轉(zhuǎn)換和打印任何來自.NET( C#, VB.NET, ASP.NET)平臺的Word文檔文件的功能。
本文將介紹如何使用正則表達(dá)式查找和替換Word 文檔中的文本。
用于測試的Word源文檔如圖:
C#
using Spire.Doc; using System.Text.RegularExpressions; using System.Drawing; namespace WordDemo { class Program { static void Main(string[] args) { Document doc = new Document(); doc.LoadFromFile("Sample.docx"); //替換以#開頭的字符 Regex regex = new Regex(@"\#\w+\b"); doc.Replace(regex, "Spire.Doc"); //替換[]內(nèi)的字符 Regex regex1 = new Regex(@"[[\s\S]*]"); doc.Replace(regex1, "Spire.Doc for .NET"); //保存文檔 doc.SaveToFile("Result.docx", FileFormat.Docx2013); } } }
VB.NET
Imports Spire.Doc Imports System.Text.RegularExpressions Imports System.Drawing Namespace WordDemo Class Program Private Shared Sub Main(ByVal args() As String) Dim doc As Document = New Document doc.LoadFromFile("Sample.docx") '替換以#開頭的字符 Dim regex As Regex = New Regex("\#\w+\b") doc.Replace(regex, "Spire.Doc") '替換[]內(nèi)的字符 Dim regex1 As Regex = New Regex("[[\s\S]*]") doc.Replace(regex1, "Spire.Doc for .NET") '保存文檔 doc.SaveToFile("Result.docx", FileFormat.Docx2013) End Sub End Class End Namespace
效果圖:
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn