2020年6月9日 星期二

[研究][C#][ASP.NET] 用 DocX 1.6.0 ( Xceed Words for .NET) 替換 Word (.docx) 中的文字

[研究][C#][ASP.NET] 用 DocX 1.6.0 ( Xceed Words for .NET) 替換 Word (.docx) 中的文字 

2020-06-09

Visual Studio 2019 v16.6.1 + C# + ASP.NET + WebForm + WebApplication  

建立 C:\Temp\Test.docx 檔案,裡面放一個字串 {$name}
本程式會把該字串替換掉。

NuGet 安裝 DocX 元件。


NOTE: There is a new Master branch as of Oct. 3, 2017. Please read about the Classic branch if you were using this project before the change.

DocX is the free, open source version of Xceed Words for .NET. Originally written by Cathal Coffey, and maintained by Przemyslaw Klys, it is now maintained by Xceed. Starting at v1.5.0, this free and open source product is provided under the Xceed Community License agreement(for non-commercial use).

How to Replace a List of Words in a .DOCX File using the DocX Library

Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WordDocxReplaceByDocX.Default" %>
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
        </div>
    </form>
</body>
</html>



Default.aspx.cs

using System;
using Xceed.Words.NET;

namespace WordDocxReplaceByDocX
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            using (var document = DocX.Load(@"C:\Temp\Test.docx"))
            {
                document.ReplaceText("{$name}", "趙錢孫");
                document.SaveAs(@"C:\Temp\Test3.docx");
        }
    }
}


(完)

相關

[研究][C#][ASP.NET] 用 NPOI 替換 Word (.docx) 中的文字

[研究][C#][ASP.NET] 用 DocXCore 1.0.7 ( Novacode ) 替換 Word (.docx) 中的文字

[研究][C#][ASP.NET] 用 DocX 1.6.0 ( Xceed Words for .NET) 替換 Word (.docx) 中的文字

沒有留言:

張貼留言