最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

使用C#代碼修改Word文檔中的內(nèi)容控件

 更新時間:2026年04月07日 08:30:35   作者:2501_93070778  
本文介紹了如何在C#項目中使用Spire.Docfor.NET庫對Word文檔中的內(nèi)容控件進行修改,包括修改正文、段落、表格行、單元格以及單元格內(nèi)的內(nèi)容控件等內(nèi)容控件的具體步驟和示例代碼,需要的朋友可以參考下

在 Word 文檔中,內(nèi)容控件可以實現(xiàn)內(nèi)容的動態(tài)更新與修改,為用戶提供更加靈活的編輯和管理方式。借助內(nèi)容控件,用戶可以在指定區(qū)域中輕松插入、刪除或修改內(nèi)容,而不會影響文檔的整體結(jié)構(gòu)。

本文將介紹如何在 C# 項目中使用 Spire.Doc for .NET,對 Word 文檔中的內(nèi)容控件進行修改。

安裝 Spire.Doc for .NET

首先,需要將 Spire.Doc for .NET 安裝包中的 DLL 文件添加為 .NET 項目的引用。你可以通過下載官方安裝包獲取這些 DLL 文件,或者直接通過 NuGet 進行安裝。

PM> Install-Package Spire.Doc

使用 C# 修改正文中的內(nèi)容控件

在 Spire.Doc 中,正文區(qū)域的內(nèi)容控件對應(yīng)的對象類型為 StructureDocumentTag。你可以通過遍歷 Section.Body 中的子對象集合,查找該類型的對象,并對其進行修改。具體步驟如下:

  1. 創(chuàng)建一個 Document 對象。
  2. 使用 Document.LoadFromFile() 方法加載文檔。
  3. 通過 Section.Body 訪問文檔中某一節(jié)的正文內(nèi)容。
  4. 遍歷正文中的子對象集合 Body.ChildObjects,查找類型為 StructureDocumentTag 的對象。
  5. 訪問 StructureDocumentTag.ChildObjects 集合,根據(jù)子對象的類型執(zhí)行相應(yīng)的修改操作。
  6. 使用 Document.SaveToFile() 方法保存文檔。

示例代碼如下:

using Spire.Doc;
using Spire.Doc.Documents;
using System.Collections.Generic;

namespace SpireDocDemo
{
    internal class Program
    {
        static void Main(string[] args)
        {
            // 創(chuàng)建一個新的文檔對象
            Document doc = new Document();

            // 從文件加載文檔內(nèi)容
            doc.LoadFromFile("Sample1.docx");

            // 獲取文檔的正文內(nèi)容
            Body body = doc.Sections[0].Body;

            // 創(chuàng)建段落和表格的列表
            List<Paragraph> paragraphs = new List<Paragraph>();
            List<Table> tables = new List<Table>();
            for (int i = 0; i < body.ChildObjects.Count; i++)
            {
                // 獲取文檔對象
                DocumentObject documentObject = body.ChildObjects[i];

                // 如果是 StructureDocumentTag(內(nèi)容控件)對象
                if (documentObject.DocumentObjectType == DocumentObjectType.StructureDocumentTag)
                {
                    StructureDocumentTag structureDocumentTag = (StructureDocumentTag)documentObject;

                    // 如果標簽為 "c1" 或別名為 "c1"
                    if (structureDocumentTag.SDTProperties.Tag == "c1" || structureDocumentTag.SDTProperties.Alias == "c1")
                    {
                        for (int j = 0; j < structureDocumentTag.ChildObjects.Count; j++)
                        {
                            // 如果是段落對象
                            if (structureDocumentTag.ChildObjects[j].DocumentObjectType == DocumentObjectType.Paragraph)
                            {
                                Paragraph paragraph = (Paragraph)structureDocumentTag.ChildObjects[j];
                                paragraphs.Add(paragraph);
                            }

                            // 如果是表格對象
                            if (structureDocumentTag.ChildObjects[j].DocumentObjectType == DocumentObjectType.Table)
                            {
                                Table table = (Table)structureDocumentTag.ChildObjects[j];
                                tables.Add(table);
                            }
                        }
                    }
                }
            }

            // 修改第一個段落的文本內(nèi)容
            paragraphs[0].Text = "Spire.Doc for .NET 是一個完全獨立的 .NET Word 類庫,無需在系統(tǒng)中安裝 Microsoft Office。";

            // 重置第一個表格的單元格(5 行 4 列)
            tables[0].ResetCells(5, 4);

            // 將修改后的文檔保存到文件
            doc.SaveToFile("ModifyBodyContentControls.docx", FileFormat.Docx2016);

            // 釋放文檔資源
            doc.Dispose();
        }
    }
}

使用 C# 修改段落中的內(nèi)容控件

在 Spire.Doc 中,段落內(nèi)的內(nèi)容控件對應(yīng)的對象類型為 StructureDocumentTagInline。要對其進行修改,需要遍歷 Paragraph.ChildObjects 集合,查找該類型的對象,并執(zhí)行相應(yīng)的操作。具體步驟如下:

  1. 創(chuàng)建一個 Document 對象。
  2. 使用 Document.LoadFromFile() 方法加載文檔。
  3. 通過 Section.Body 訪問文檔中某一節(jié)的正文內(nèi)容。
  4. 使用 Body.Paragraphs[0] 獲取正文中的第一個段落。
  5. 遍歷段落的子對象集合 Paragraph.ChildObjects,查找類型為 StructureDocumentTagInline 的對象。
  6. 訪問 StructureDocumentTagInline.ChildObjects 集合,根據(jù)子對象類型執(zhí)行相應(yīng)的修改操作。
  7. 使用 Document.SaveToFile() 方法保存文檔。

示例代碼如下:

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Collections.Generic;

namespace SpireDocDemo
{
    internal class Program
    {
        static void Main(string[] args)
        {
            // 創(chuàng)建一個新的 Document 對象
            Document doc = new Document();

            // 從文件加載文檔內(nèi)容
            doc.LoadFromFile("Sample2.docx");

            // 獲取文檔的正文內(nèi)容
            Body body = doc.Sections[0].Body;

            // 獲取正文中的第一個段落
            Paragraph paragraph = body.Paragraphs[0];

            // 遍歷段落中的子對象
            for (int i = 0; i < paragraph.ChildObjects.Count; i++)
            {
                // 判斷子對象是否為 StructureDocumentTagInline(行內(nèi)內(nèi)容控件)
                if (paragraph.ChildObjects[i].DocumentObjectType == DocumentObjectType.StructureDocumentTagInline)
                {
                    // 將子對象轉(zhuǎn)換為 StructureDocumentTagInline 類型
                    StructureDocumentTagInline structureDocumentTagInline = (StructureDocumentTagInline)paragraph.ChildObjects[i];

                    // 判斷 Tag 或 Alias 是否為 "text1"
                    if (structureDocumentTagInline.SDTProperties.Tag == "text1" || structureDocumentTagInline.SDTProperties.Alias == "text1")
                    {
                        // 遍歷該內(nèi)容控件中的子對象
                        for (int j = 0; j < structureDocumentTagInline.ChildObjects.Count; j++)
                        {
                            // 判斷子對象是否為 TextRange(文本)
                            if (structureDocumentTagInline.ChildObjects[j].DocumentObjectType == DocumentObjectType.TextRange)
                            {
                                // 轉(zhuǎn)換為 TextRange 類型
                                TextRange range = (TextRange)structureDocumentTagInline.ChildObjects[j];

                                // 設(shè)置文本內(nèi)容
                                range.Text = "97-2003/2007/2010/2013/2016/2019";
                            }
                        }
                    }

                    // 判斷 Tag 或 Alias 是否為 "logo1"
                    if (structureDocumentTagInline.SDTProperties.Tag == "logo1" || structureDocumentTagInline.SDTProperties.Alias == "logo1")
                    {
                        // 遍歷該內(nèi)容控件中的子對象
                        for (int j = 0; j < structureDocumentTagInline.ChildObjects.Count; j++)
                        {
                            // 判斷子對象是否為圖片
                            if (structureDocumentTagInline.ChildObjects[j].DocumentObjectType == DocumentObjectType.Picture)
                            {
                                // 轉(zhuǎn)換為 DocPicture 類型
                                DocPicture docPicture = (DocPicture)structureDocumentTagInline.ChildObjects[j];

                                // 加載指定圖片
                                docPicture.LoadImage("Doc-NET.png");

                                // 設(shè)置圖片的寬度和高度
                                docPicture.Width = 100;
                                docPicture.Height = 100;
                            }
                        }
                    }
                }
            }

            // 將修改后的文檔保存為新文件
            doc.SaveToFile("ModifiedContentControlsInParagraph.docx", FileFormat.Docx2016);

            // 釋放 Document 對象資源
            doc.Dispose();
        }
    }
}

使用 C# 修改包裹表格行的內(nèi)容控件

在 Spire.Doc 中,表格行級別的內(nèi)容控件對應(yīng)的對象類型為 StructureDocumentTagRow。要對其進行修改,需要遍歷 Table.ChildObjects 集合,查找該類型的對象,并執(zhí)行相應(yīng)的操作。具體步驟如下:

  1. 創(chuàng)建一個 Document 對象。
  2. 使用 Document.LoadFromFile() 方法加載文檔。
  3. 通過 Section.Body 訪問文檔中某一節(jié)的正文內(nèi)容。
  4. 使用 Body.Tables[0] 獲取正文中的第一個表格。
  5. 遍歷表格的子對象集合 Table.ChildObjects,查找類型為 StructureDocumentTagRow 的對象。
  6. 訪問 StructureDocumentTagRow.Cells(表格行內(nèi)容控件中的單元格集合),并對單元格內(nèi)容進行相應(yīng)修改。
  7. 使用 Document.SaveToFile() 方法保存文檔。

示例代碼如下:

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;

namespace SpireDocDemo
{
    internal class Program
    {
        static void Main(string[] args)
        {
            // 創(chuàng)建一個新的文檔對象
            Document doc = new Document();

            // 從文件加載文檔
            doc.LoadFromFile("Sample3.docx");

            // 獲取文檔的正文內(nèi)容
            Body body = doc.Sections[0].Body;

            // 獲取第一個表格
            Table table = (Table)body.Tables[0];

            // 遍歷表格中的子對象
            for (int i = 0; i < table.ChildObjects.Count; i++)
            {
                // 判斷子對象是否為 StructureDocumentTagRow(表格行內(nèi)容控件)
                if (table.ChildObjects[i].DocumentObjectType == DocumentObjectType.StructureDocumentTagRow)
                {
                    // 轉(zhuǎn)換為 StructureDocumentTagRow 對象
                    StructureDocumentTagRow structureDocumentTagRow = (StructureDocumentTagRow)table.ChildObjects[i];

                    // 判斷 Tag 或 Alias 是否為 "row1"
                    if (structureDocumentTagRow.SDTProperties.Tag == "row1" || structureDocumentTagRow.SDTProperties.Alias == "row1")
                    {
                        // 清除該單元格中的段落內(nèi)容
                        structureDocumentTagRow.Cells[0].Paragraphs.Clear();

                        // 在單元格中添加段落并設(shè)置文本
                        TextRange textRange = structureDocumentTagRow.Cells[0].AddParagraph().AppendText("Arts");
                        textRange.CharacterFormat.TextColor = System.Drawing.Color.Blue;
                    }
                }
            }

            // 將修改后的文檔保存到文件
            doc.SaveToFile("ModifiedTableRowContentControl.docx", FileFormat.Docx2016);

            // 釋放文檔資源
            doc.Dispose();
        }
    }
}

使用 C# 修改包裹表格單元格的內(nèi)容控件

在 Spire.Doc 中,表格單元格中的內(nèi)容控件對應(yīng)的對象類型為 StructureDocumentTagCell。要對其進行修改,需要遍歷 TableRow.ChildObjects 集合,查找該類型的對象,并對其執(zhí)行相應(yīng)操作。具體步驟如下:

  1. 創(chuàng)建一個 Document 對象。
  2. 使用 Document.LoadFromFile() 方法加載文檔。
  3. 通過 Section.Body 獲取文檔中某一節(jié)的正文內(nèi)容。
  4. 使用 Body.Tables[0] 獲取正文中的第一個表格。
  5. 遍歷表格行集合 Table.Rows,逐一獲取每個 TableRow 對象。
  6. 遍歷每一行的子對象集合 TableRow.ChildObjects,查找類型為 StructureDocumentTagCell 的對象。
  7. 訪問 StructureDocumentTagCell 中的段落集合,并對內(nèi)容進行相應(yīng)修改。
  8. 使用 Document.SaveToFile() 方法保存文檔。

示例代碼如下:

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;

namespace SpireDocDemo
{
    internal class Program
    {
        static void Main(string[] args)
        {
            // 創(chuàng)建一個新的文檔對象
            Document doc = new Document();

            // 從文件加載文檔
            doc.LoadFromFile("Sample4.docx");

            // 獲取文檔的正文內(nèi)容
            Body body = doc.Sections[0].Body;

            // 獲取文檔中的第一個表格
            Table table = (Table)body.Tables[0];

            // 遍歷表格的每一行
            for (int i = 0; i < table.Rows.Count; i++)
            {
                // 遍歷每一行中的子對象
                for (int j = 0; j < table.Rows[i].ChildObjects.Count; j++)
                {
                    // 判斷子對象是否為 StructureDocumentTagCell(單元格內(nèi)容控件)
                    if (table.Rows[i].ChildObjects[j].DocumentObjectType == DocumentObjectType.StructureDocumentTagCell)
                    {
                        // 轉(zhuǎn)換為 StructureDocumentTagCell 類型
                        StructureDocumentTagCell structureDocumentTagCell = (StructureDocumentTagCell)table.Rows[i].ChildObjects[j];

                        // 判斷 Tag 或 Alias 是否為 "cell1"
                        if (structureDocumentTagCell.SDTProperties.Tag == "cell1" || structureDocumentTagCell.SDTProperties.Alias == "cell1")
                        {
                            // 清除單元格中的段落內(nèi)容
                            structureDocumentTagCell.Paragraphs.Clear();

                            // 添加新的段落并設(shè)置文本
                            TextRange textRange = structureDocumentTagCell.AddParagraph().AppendText("92");
                            textRange.CharacterFormat.TextColor = System.Drawing.Color.Blue;
                        }
                    }
                }
            }

            // 將修改后的文檔保存為新文件
            doc.SaveToFile("ModifiedTableCellContentControl.docx", FileFormat.Docx2016);

            // 釋放文檔對象資源
            doc.Dispose();
        }
    }
}

使用 C# 修改表格單元格內(nèi)的內(nèi)容控件

本示例演示如何修改表格單元格中段落內(nèi)的內(nèi)容控件。需要先獲取單元格中的段落集合 TableCell.Paragraphs,然后遍歷每個段落的子對象集合 Paragraph.ChildObjects,查找類型為 StructureDocumentTagInline 的對象,并對其進行相應(yīng)修改。具體步驟如下:

  1. 創(chuàng)建一個 Document 對象。
  2. 使用 Document.LoadFromFile() 方法加載文檔。
  3. 通過 Section.Body 獲取文檔中某一節(jié)的正文內(nèi)容。
  4. 使用 Body.Tables[0] 獲取正文中的第一個表格。
  5. 遍歷表格行集合 Table.Rows,逐一獲取每個 TableRow 對象。
  6. 遍歷每一行中的單元格集合 TableRow.Cells,獲取每個 TableCell 對象。
  7. 遍歷單元格中的段落集合 TableCell.Paragraphs,獲取每個 Paragraph 對象。
  8. 遍歷段落的子對象集合 Paragraph.ChildObjects,查找類型為 StructureDocumentTagInline 的對象。
  9. 訪問 StructureDocumentTagInline 的 ChildObjects 集合,根據(jù)子對象類型執(zhí)行相應(yīng)的修改操作。
  10. 使用 Document.SaveToFile() 方法保存文檔。

示例代碼如下:

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;

namespace SpireDocDemo
{
    internal class Program
    {
        static void Main(string[] args)
        {
            // 創(chuàng)建一個新的 Document 對象
            Document doc = new Document();

            // 從文件加載文檔內(nèi)容
            doc.LoadFromFile("Sample5.docx");

            // 獲取文檔的正文內(nèi)容
            Body body = doc.Sections[0].Body;

            // 獲取第一個表格
            Table table = (Table)body.Tables[0];

            // 遍歷表格的每一行
            for (int r = 0; r < table.Rows.Count; r++)
            {
                // 遍歷每一行中的單元格
                for (int c = 0; c < table.Rows[r].Cells.Count; c++)
                {
                    // 遍歷單元格中的段落
                    for (int p = 0; p < table.Rows[r].Cells[c].Paragraphs.Count; p++)
                    {
                        // 獲取段落對象
                        Paragraph paragraph = table.Rows[r].Cells[c].Paragraphs[p];

                        // 遍歷段落中的子對象
                        for (int i = 0; i < paragraph.ChildObjects.Count; i++)
                        {
                            // 判斷子對象是否為 StructureDocumentTagInline(行內(nèi)內(nèi)容控件)
                            if (paragraph.ChildObjects[i].DocumentObjectType == DocumentObjectType.StructureDocumentTagInline)
                            {
                                // 轉(zhuǎn)換為 StructureDocumentTagInline 對象
                                StructureDocumentTagInline structureDocumentTagInline = (StructureDocumentTagInline)paragraph.ChildObjects[i];

                                // 判斷 Tag 或 Alias 是否為 "test1"
                                if (structureDocumentTagInline.SDTProperties.Tag == "test1" || structureDocumentTagInline.SDTProperties.Alias == "test1")
                                {
                                    // 遍歷該內(nèi)容控件中的子對象
                                    for (int j = 0; j < structureDocumentTagInline.ChildObjects.Count; j++)
                                    {
                                        // 判斷子對象是否為 TextRange(文本)
                                        if (structureDocumentTagInline.ChildObjects[j].DocumentObjectType == DocumentObjectType.TextRange)
                                        {
                                            // 轉(zhuǎn)換為 TextRange 對象
                                            TextRange textRange = (TextRange)structureDocumentTagInline.ChildObjects[j];

                                            // 設(shè)置文本內(nèi)容
                                            textRange.Text = "89";

                                            // 設(shè)置文本顏色
                                            textRange.CharacterFormat.TextColor = System.Drawing.Color.Blue;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            // 將修改后的文檔保存為新文件
            doc.SaveToFile("ModifiedContentControlInParagraphOfTableCell.docx", FileFormat.Docx2016);

            // 釋放 Document 對象資源
            doc.Dispose();
        }
    }
}

到此這篇關(guān)于使用C#代碼修改Word文檔中的內(nèi)容控件的文章就介紹到這了,更多相關(guān)C#修改Word內(nèi)容控件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • c# 獲取機器唯一識別碼的示例

    c# 獲取機器唯一識別碼的示例

    這篇文章主要介紹了c# 獲取機器唯一識別碼的示例,幫助大家更好的理解和學習使用c#,感興趣的朋友可以了解下
    2021-03-03
  • datatable去掉重復行的方法

    datatable去掉重復行的方法

    這篇文章主要介紹了datatable去掉重復行的方法,需要的朋友可以參考下
    2014-02-02
  • 學習Winform文本類控件(Label、Button、TextBox)

    學習Winform文本類控件(Label、Button、TextBox)

    這篇文章主要和大家一起學習Winform文本類控件,包含標簽控件(Label),按鈕控件(Button),文本框控件(TextBox)和格式文本控件(RichTextBox),感興趣的小伙伴們可以參考一下
    2016-05-05
  • C#使用foreach語句搜索數(shù)組元素的方法

    C#使用foreach語句搜索數(shù)組元素的方法

    這篇文章主要介紹了C#使用foreach語句搜索數(shù)組元素的方法,涉及C#使用foreach語句遍歷數(shù)組實現(xiàn)搜索功能的技巧,非常具有實用價值,需要的朋友可以參考下
    2015-04-04
  • 詳解C#中HashTable的用法

    詳解C#中HashTable的用法

    在.NET Framework中,Hashtable是System.Collections命名空間提供的一個容器,用于處理和表現(xiàn)類似keyvalue的鍵值對,其中key通??捎脕砜焖俨檎遥瑫rkey是區(qū)分大小寫;value用于存儲對應(yīng)于key的值
    2016-02-02
  • C#中 Json 序列化去掉null值的方法

    C#中 Json 序列化去掉null值的方法

    要將一個對象序列化,可是如果對象的屬性為null的時候,我們想將屬性為null的都去掉,怎么處理呢?其實方法很簡單的,下面就跟隨腳本之家小編一起學習C#中 Json 序列化去掉null值的方法吧
    2017-09-09
  • C#常用知識點簡單回顧(有圖有真相)

    C#常用知識點簡單回顧(有圖有真相)

    C#知識點記錄編程的點點滴滴,本文整理了一些(傳值調(diào)用與引用調(diào)用/打印三角形/遞歸求階乘/多態(tài)性..等等),感興趣的朋友可以了解下的,不要走開(有圖有真相)
    2013-01-01
  • C#調(diào)用Java類的實現(xiàn)方法

    C#調(diào)用Java類的實現(xiàn)方法

    以下是對使用C#調(diào)用Java類的實現(xiàn)方法進行了詳細的介紹,需要的朋友可以過來參考下
    2013-09-09
  • C# XML基礎(chǔ)入門小結(jié)(XML文件內(nèi)容增刪改查清)

    C# XML基礎(chǔ)入門小結(jié)(XML文件內(nèi)容增刪改查清)

    本文主要介紹了C# XML基礎(chǔ)入門小結(jié)(XML文件內(nèi)容增刪改查清),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-04-04
  • C#借助Free Spire.Doc for .NET實現(xiàn)將HTML轉(zhuǎn)換為RTF富文本格式

    C#借助Free Spire.Doc for .NET實現(xiàn)將HTML轉(zhuǎn)換為RTF富文本格式

    在辦公自動化、文檔導出、報表生成等場景中,HTML 與RTF格式的轉(zhuǎn)換是常見需求,本文將詳細講解如何使用該免費庫通過 C# 代碼實現(xiàn) HTML 到 RTF 的轉(zhuǎn)換,感興趣的小伙伴可以了解下
    2025-11-11

最新評論

二连浩特市| 富源县| 那曲县| 青州市| 阿拉尔市| 博野县| 门头沟区| 奉贤区| 保定市| 襄垣县| 张北县| 彭州市| 新丰县| 宁安市| 东乌珠穆沁旗| 图们市| 枞阳县| 德化县| 大安市| 中西区| 当阳市| 孟连| 曲松县| 浦江县| 阿勒泰市| 邻水| 临漳县| 吉首市| 乌兰县| 昭觉县| 行唐县| 项城市| 曲靖市| 齐河县| 新竹市| 万宁市| 克拉玛依市| 淮阳县| 兴文县| 芒康县| 阿拉尔市|