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

C#實(shí)現(xiàn)高效生成Word復(fù)雜表格的實(shí)戰(zhàn)指南

 更新時(shí)間:2025年11月27日 08:20:01   作者:用戶835629078051  
在現(xiàn)代辦公自動(dòng)化中,Word?文檔依然是信息呈現(xiàn)和數(shù)據(jù)輸出的重要載體,本文將系統(tǒng)介紹如何使用?Free?Spire.Doc?for?.NET?處理?Word?表格,希望對(duì)大家有所幫助

在現(xiàn)代辦公自動(dòng)化中,Word 文檔依然是信息呈現(xiàn)和數(shù)據(jù)輸出的重要載體。無(wú)論是月度報(bào)表、客戶合同,還是內(nèi)部匯總文檔,表格都是不可或缺的元素。手動(dòng)創(chuàng)建表格不僅效率低,而且容易出錯(cuò)。借助 C# 編程,我們可以輕松實(shí)現(xiàn) Word 表格的自動(dòng)生成、格式化以及復(fù)雜操作。

本文將系統(tǒng)介紹如何使用 Free Spire.Doc for .NET 處理 Word 表格,包括表格樣式設(shè)置、動(dòng)態(tài)添加刪除行列、以及單元格內(nèi)嵌套表格等操作。通過(guò)這些技巧,您可以快速生成符合業(yè)務(wù)需求的高質(zhì)量文檔。

安裝 Free Spire.Doc for .NET:Install-Package FreeSpire.Doc

1. C# 操作 Word 表格的對(duì)象模型

在操作表格之前,需要理解 Word 文檔的對(duì)象結(jié)構(gòu):

  • Document:文檔整體對(duì)象
  • Section:文檔的分節(jié),每個(gè) Section 可包含多個(gè)段落或表格
  • Table:表格對(duì)象
  • TableRow:表格行
  • TableCell:表格單元格
  • Paragraph:?jiǎn)卧窕蛘闹械亩温?/li>
  • TextRange:段落中實(shí)際文本
  • ParagraphStyle:段落樣式,統(tǒng)一管理字體、字號(hào)、對(duì)齊方式等

掌握這些對(duì)象的層級(jí)關(guān)系,可以更靈活地控制表格內(nèi)容與樣式。

2. 創(chuàng)建并格式化表格

在很多業(yè)務(wù)場(chǎng)景中,表格不僅需要展示數(shù)據(jù),還要美觀、清晰。下面示例展示如何創(chuàng)建一個(gè)銷售數(shù)據(jù)表,設(shè)置列寬、行高、背景色和字體樣式。

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Drawing;

public class WordTableDemo
{
    public static void CreateFormattedTable(string filePath)
    {
        Document doc = new Document();
        Section sec = doc.AddSection();

        // 添加標(biāo)題
        Paragraph title = sec.AddParagraph();
        TextRange tr = title.AppendText("2025年度產(chǎn)品銷售統(tǒng)計(jì)");
        tr.CharacterFormat.FontName = "Yu Gothic UI";
        tr.CharacterFormat.FontSize = 18;
        tr.CharacterFormat.Bold = true;
        title.Format.HorizontalAlignment = HorizontalAlignment.Center;

        sec.AddParagraph().AppendText("\n"); // 間距

        // 創(chuàng)建表格
        Table table = sec.AddTable();
        table.ResetCells(5, 4); // 5行4列
        table.TableFormat.Borders.BorderType = BorderStyle.Single;
        table.TableFormat.Borders.LineWidth = 1f;
        table.TableFormat.Borders.Color = Color.DarkGray;

        // 設(shè)置列寬
        int[] colWidths = { 80, 200, 100, 120 };
        foreach (TableRow row in table.Rows)
        {
            for (int i = 0; i < colWidths.Length; i++)
                row.Cells[i].SetCellWidth(colWidths[i], CellWidthType.Point);
        }

        // 表頭樣式
        ParagraphStyle headerStyle = doc.AddParagraphStyle("headerStyle");
        headerStyle.CharacterFormat.Bold = true;
        headerStyle.CharacterFormat.FontName = "Yu Gothic UI";
        headerStyle.CharacterFormat.FontSize = 13;
        headerStyle.ParagraphFormat.HorizontalAlignment = HorizontalAlignment.Center;

        string[] headers = { "產(chǎn)品編號(hào)", "產(chǎn)品名稱", "銷售數(shù)量", "銷售金額" };
        for (int i = 0; i < headers.Length; i++)
        {
            TableCell cell = table.Rows[0].Cells[i];
            cell.CellFormat.BackColor = Color.LightSteelBlue;
            cell.CellFormat.VerticalAlignment = VerticalAlignment.Middle;
            Paragraph p = cell.AddParagraph();
            p.AppendText(headers[i]);
            p.ApplyStyle(headerStyle.Name);
        }

        // 數(shù)據(jù)樣式
        ParagraphStyle dataStyle = doc.AddParagraphStyle("dataStyle");
        dataStyle.CharacterFormat.FontName = "Yu Gothic UI";
        dataStyle.CharacterFormat.FontSize = 12;
        dataStyle.ParagraphFormat.HorizontalAlignment = HorizontalAlignment.Center;

        string[,] data = {
            { "A101", "超薄筆記本", "120", "180000" },
            { "B202", "智能手機(jī)", "450", "540000" },
            { "C303", "無(wú)線耳機(jī)", "300", "90000" },
            { "D404", "智能手表", "200", "80000" }
        };

        for (int r = 0; r < data.GetLength(0); r++)
        {
            for (int c = 0; c < data.GetLength(1); c++)
            {
                TableCell cell = table.Rows[r + 1].Cells[c];
                Paragraph p = cell.AddParagraph();
                p.AppendText(data[r, c]);
                p.ApplyStyle(dataStyle.Name);
                cell.CellFormat.VerticalAlignment = VerticalAlignment.Middle;
            }
        }

        doc.SaveToFile(filePath, FileFormat.Docx);
        Console.WriteLine($"文檔已生成:{filePath}");
    }
}

此示例展示了如何創(chuàng)建格式化表格、設(shè)置列寬、行高和背景色,同時(shí)應(yīng)用統(tǒng)一字體樣式。

生成結(jié)果預(yù)覽

3. 動(dòng)態(tài)添加與刪除行列

實(shí)際業(yè)務(wù)中,表格行列數(shù)量可能不固定,需要根據(jù)數(shù)據(jù)動(dòng)態(tài)調(diào)整:

// 添加新行
public static void AddRow(Table table, string[] rowData)
{
    TableRow newRow = table.AddRow();
    newRow.Height = 22;
    newRow.HeightType = TableRowHeightType.Auto;

    for (int i = 0; i < rowData.Length; i++)
    {
        TableCell cell = newRow.Cells[i];
        Paragraph p = cell.AddParagraph();
        p.AppendText(rowData[i]);
        p.Format.HorizontalAlignment = HorizontalAlignment.Center;
        cell.CellFormat.VerticalAlignment = VerticalAlignment.Middle;
    }
}

// 刪除行
public static void RemoveRow(Table table, int rowIndex)
{
    if (rowIndex >= 0 && rowIndex < table.Rows.Count)
        table.Rows.RemoveAt(rowIndex);
}

// 添加列
public static void AddColumn(Table table, int colIndex, string[] colData)
{
    for (int r = 0; r < table.Rows.Count; r++)
    {
        TableCell newCell = new TableCell(table.Document);
        table.Rows[r].Cells.Insert(colIndex, newCell);
        Paragraph p = newCell.AddParagraph();
        if (r < colData.Length) p.AppendText(colData[r]);
        p.Format.HorizontalAlignment = HorizontalAlignment.Center;
        newCell.CellFormat.VerticalAlignment = VerticalAlignment.Middle;
    }
}

// 刪除列
public static void RemoveColumn(Table table, int colIndex)
{
    for (int r = 0; r < table.Rows.Count; r++)
    {
        if (colIndex >= 0 && colIndex < table.Rows[r].Cells.Count)
            table.Rows[r].Cells.RemoveAt(colIndex);
    }
}

使用這些方法,表格可以根據(jù)實(shí)際數(shù)據(jù)動(dòng)態(tài)擴(kuò)展或縮減,非常適合報(bào)表和數(shù)據(jù)匯總場(chǎng)景。

4. 單元格內(nèi)嵌套表格

在合同條款、問(wèn)卷或復(fù)雜布局中,可能需要在一個(gè)單元格內(nèi)部嵌入表格:

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


public class WordTableDemo
{
    public static void InsertNestedTable(string filePath)
    {
        Document doc = new Document();
        Section sec = doc.AddSection();

        sec.AddParagraph().AppendText("嵌套表格示例").CharacterFormat.FontSize = 16;
        sec.AddParagraph().AppendText("\n");

        Table outer = sec.AddTable();
        outer.ResetCells(2, 2);
        outer.TableFormat.Borders.BorderType = BorderStyle.Single;

        outer.Rows[0].Cells[0].AddParagraph().AppendText("外表格 (0,0)");

        TableCell nestedCell = outer.Rows[0].Cells[1];
        nestedCell.AddParagraph().AppendText("內(nèi)部表格如下:");

        Table inner = nestedCell.AddTable();
        inner.ResetCells(3, 2);
        inner.TableFormat.Borders.BorderType = BorderStyle.Dot;

        string[,] innerData = {
        { "編號(hào)", "名稱" },
        { "001", "筆記本" },
        { "002", "手機(jī)" }
    };

        for (int r = 0; r < innerData.GetLength(0); r++)
        {
            for (int c = 0; c < innerData.GetLength(1); c++)
            {
                TableCell cell = inner.Rows[r].Cells[c];
                cell.AddParagraph().AppendText(innerData[r, c]);
                cell.CellFormat.VerticalAlignment = VerticalAlignment.Middle;
                cell.Paragraphs[0].Format.HorizontalAlignment = HorizontalAlignment.Center;
            }
        }

        outer.Rows[1].Cells[0].AddParagraph().AppendText("外表格 (1,0)");
        outer.Rows[1].Cells[1].AddParagraph().AppendText("外表格 (1,1)");

        doc.SaveToFile(filePath, FileFormat.Docx);
        Console.WriteLine($"嵌套表格文檔已生成:{filePath}");
    }

    static void Main(string[] args)
    {
        InsertNestedTable("NestedTable.docx");
    }
}

嵌套表格能夠在一個(gè)單元格內(nèi)呈現(xiàn)多層信息,例如條款編號(hào)+說(shuō)明內(nèi)容,或分區(qū)統(tǒng)計(jì)數(shù)據(jù)。

生成結(jié)果預(yù)覽

5. 總結(jié)

本文展示了使用 C# 與 Free Spire.Doc for .NET 操作 Word 表格的多種技巧:

  • 創(chuàng)建格式化表格,并統(tǒng)一設(shè)置列寬、行高、背景色和字體樣式
  • 動(dòng)態(tài)添加或刪除行列,適配數(shù)據(jù)量變化
  • 在單元格內(nèi)嵌套表格,實(shí)現(xiàn)更靈活的文檔布局

通過(guò)這些方法,開發(fā)者可以輕松實(shí)現(xiàn)高質(zhì)量的 Word 報(bào)表、合同或匯總文檔。結(jié)合 Free Spire.Doc 的其他 API,還可擴(kuò)展圖片插入、分頁(yè)控制和 PDF 導(dǎo)出等功能,使文檔生成更智能化。

到此這篇關(guān)于C#實(shí)現(xiàn)高效生成Word復(fù)雜表格的實(shí)戰(zhàn)指南的文章就介紹到這了,更多相關(guān)C#生成Word復(fù)雜表格內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 在C#中調(diào)用Python腳本實(shí)現(xiàn)跨語(yǔ)言功能集成

    在C#中調(diào)用Python腳本實(shí)現(xiàn)跨語(yǔ)言功能集成

    隨著現(xiàn)代應(yīng)用程序的復(fù)雜性和多樣性不斷增加,跨語(yǔ)言集成已成為一種常見的開發(fā)實(shí)踐,C# 和 Python 是兩種廣泛使用的編程語(yǔ)言,本文將詳細(xì)介紹如何在 C# 中調(diào)用 Python 腳本,并通過(guò)傳遞參數(shù)實(shí)現(xiàn)功能的跨語(yǔ)言集成,需要的朋友可以參考下
    2026-03-03
  • C#如何安全、高效地玩轉(zhuǎn)任何種類的內(nèi)存之Span的本質(zhì)

    C#如何安全、高效地玩轉(zhuǎn)任何種類的內(nèi)存之Span的本質(zhì)

    為什么要使用指針,什么時(shí)候需要使用它,以及如何安全、高效地使用它?本文將講清楚 What、How 和 Why ,讓你知其然,更知其所以然
    2021-08-08
  • C#中IsNullOrEmpty和IsNullOrWhiteSpace的使用方法及區(qū)別解析

    C#中IsNullOrEmpty和IsNullOrWhiteSpace的使用方法及區(qū)別解析

    今天我們將探討C#中兩個(gè)常用的字符串處理方法:IsNullOrEmpty和IsNullOrWhiteSpace,本文中,我們將詳細(xì)解釋這兩個(gè)方法的功能和使用場(chǎng)景,并幫助您更好地理解它們之間的區(qū)別,本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧
    2023-07-07
  • C#實(shí)現(xiàn)與歐姆龍PLC通信的示例代碼

    C#實(shí)現(xiàn)與歐姆龍PLC通信的示例代碼

    本文主要介紹了C#實(shí)現(xiàn)與歐姆龍PLC通信的示例代碼,包括FINS-TCP和ModbusTCP協(xié)議,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2026-01-01
  • C#將文件復(fù)制到指定文件夾并整理

    C#將文件復(fù)制到指定文件夾并整理

    這篇文章主要介紹了C#將文件復(fù)制到指定文件夾并按照時(shí)間順序來(lái)整理歸檔的方法,另外附上其他網(wǎng)友的2種實(shí)現(xiàn)方式,有需要的小伙伴可以參考下。
    2015-06-06
  • C# 數(shù)據(jù)驗(yàn)證Regex示例詳解

    C# 數(shù)據(jù)驗(yàn)證Regex示例詳解

    文章介紹了C#中使用Regex進(jìn)行數(shù)據(jù)驗(yàn)證的方法,包括整數(shù)和小數(shù)的正負(fù)驗(yàn)證,以及郵箱和身份證號(hào)的格式驗(yàn)證,感興趣的朋友一起看看吧
    2025-02-02
  • c# 兩個(gè)數(shù)組比較,將重復(fù)部分去掉,返回不重復(fù)部分的實(shí)現(xiàn)

    c# 兩個(gè)數(shù)組比較,將重復(fù)部分去掉,返回不重復(fù)部分的實(shí)現(xiàn)

    下面小編就為大家?guī)?lái)一篇c# 兩個(gè)數(shù)組比較,將重復(fù)部分去掉,返回不重復(fù)部分的實(shí)現(xiàn)方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2016-12-12
  • C# datagrid非常規(guī)方法實(shí)現(xiàn)添加合并列

    C# datagrid非常規(guī)方法實(shí)現(xiàn)添加合并列

    這篇文章主要介紹了C# datagrid非常規(guī)方法實(shí)現(xiàn)添加合并列,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-11-11
  • VS2010寫的程序在自己電腦可以運(yùn)行、其他電腦上不能運(yùn)行的解決方案

    VS2010寫的程序在自己電腦可以運(yùn)行、其他電腦上不能運(yùn)行的解決方案

    自己用Visual Studio 2010 旗艦版寫了一個(gè)軟件,在自己電腦上運(yùn)行完全沒(méi)有問(wèn)題,但是拷貝到其他人電腦上之后不管雙擊還是以管理身份運(yùn)行,均沒(méi)有反應(yīng),進(jìn)程管理器中相關(guān)進(jìn)程也只是一閃而過(guò)
    2013-04-04
  • C#基于數(shù)據(jù)庫(kù)存儲(chǔ)過(guò)程的AJAX分頁(yè)實(shí)例

    C#基于數(shù)據(jù)庫(kù)存儲(chǔ)過(guò)程的AJAX分頁(yè)實(shí)例

    這篇文章主要介紹了C#基于數(shù)據(jù)庫(kù)存儲(chǔ)過(guò)程的AJAX分頁(yè)實(shí)現(xiàn)方法,以實(shí)例形式詳細(xì)講述了數(shù)據(jù)庫(kù)存儲(chǔ)過(guò)程的定義、數(shù)據(jù)庫(kù)的訪問(wèn)及Ajax的實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2015-01-01

最新評(píng)論

城步| 方山县| 富阳市| 工布江达县| 米泉市| 房产| 株洲县| 泰来县| 潮安县| 玉林市| 丹凤县| 罗定市| 桦川县| 滦平县| 大新县| 宜兴市| 靖安县| 怀柔区| 宣威市| 鹤峰县| 南丰县| 玛沁县| 潜江市| 旌德县| 安岳县| 仪征市| 沅陵县| 江都市| 和硕县| 建昌县| 南投市| 勃利县| 垫江县| 临安市| 固始县| 阳原县| 阜新市| 平邑县| 易门县| 康乐县| 仲巴县|