使用 C# 在Word中插入圖表

Word中的圖表功能將數據可視化地呈現在文檔中。這爲展示數據和進行數據分析提供了一種方便且易於使用的工具,使作者能夠以直觀的方式傳達信息。要通過C#代碼來實現在Word中繪製圖表,可以藉助 Spire.Doc for .NET 控件,具體操作參考下文。

  • C# 在Word中插入柱狀圖
  • C# 在Word中插入折線圖

 

Dll引用

有兩種安裝Spire.Doc for .NET庫的方法:

  1. 在Visual Studio中通過NuGet搜索“Spire.Doc”,然後點擊“安裝”將其引用到程序中。
  2. 點擊以下鏈接將Spire.Doc for .NET下載到本地,解壓後,然後手動將BIN文件夾下的Spire.Doc.dll文件添加引用至程序。

 https://www.e-iceblue.cn/Downloads/Spire-Doc-NET.html

 

C# 在Word中插入柱狀圖

柱狀圖可以快速比較不同類別或組之間的數量差異,幫助人們識別趨勢和模式。要在Word中插入柱狀圖,可以使用Paragraph.AppenChart(ChartType.Column, float width, float height) 方法。完整代碼如下:

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields.Shapes.Charts;
using Spire.Doc.Fields;

namespace CreateColumnChart
{
    class Program
    {
        static void Main(string[] args)
        {
            //創建 Document 對象
            Document document = new Document();

            //添加一節
            Section section = document.AddSection();

            //添加一個段落
            Paragraph paragraph = section.AddParagraph();

            //將一個指定大小的柱狀圖添加到段落中
            ShapeObject shape = paragraph.AppendChart(ChartType.Column, 450, 250);

            //獲取該圖表
            Chart chart = shape.Chart;

            //清除圖表的默認系列數據
            chart.Series.Clear();

            //添加一個具有指定系列名稱、類別名稱和系列值的自定義系列到圖表中
            chart.Series.Add("銷售1組",
                new[] { "第一季度", "第二季度", "第三季度", "第四季度" },
                new double[] { 5000, 8000, 9000, 8500 });

            //添加另一個系列
            chart.Series.Add("銷售2組",
            new[] { "第一季度", "第二季度", "第三季度", "第四季度" },
            new double[] { 3000, 5000, 7000, 6000 });

            //設置圖標標題
            chart.Title.Text = "各組季度銷售額";

            //設置 Y 軸的數字格式
            chart.AxisY.NumberFormat.FormatCode = "#,##0";

            //設置圖例位置
            chart.Legend.Position = LegendPosition.Bottom;

            //保存結果文檔
            document.SaveToFile("柱狀圖.docx", FileFormat.Docx2019);
        }
    }
}

 

 

C# 在Word中插入折線圖

折線圖是一種常用的統計圖表,用於展示數據隨着時間、順序或其他連續變量的變化趨勢。它由一系列連接在一起的數據點組成,通過連線來表示數據的變化。插入折線圖步驟與插入柱狀圖類似,完整代碼如下:

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields.Shapes.Charts;
using Spire.Doc.Fields;

namespace WordLineChart
{
    class Program
    {
        static void Main(string[] args)
        {
            //創建Document對象
            Document document = new Document();

            //添加一節
            Section section = document.AddSection();

            //添加一個段落
            Paragraph newPara = section.AddParagraph();

            //將指定大小的折線圖添加到段落中
            ShapeObject shape = newPara.AppendChart(ChartType.Line, 460, 300);

            //獲取該圖表
            Chart chart = shape.Chart;

            //設置圖表標題
            chart.Title.Text = "銷售報表";

            //清除圖表的默認系列數據
            chart.Series.Clear();

            //將具有指定系列名稱、類別名稱和系列值的三個自定義系列添加到圖表中
            string[] categories = { "第一季度", "第二季度", "第三季度", "第四季度" };
            chart.Series.Add("銷售1組", categories, new double[] { 1200, 2500, 2500, 3800 });
            chart.Series.Add("銷售2組", categories, new double[] { 1500, 1800, 3000, 4000 });
            chart.Series.Add("銷售3組", categories, new double[] { 1200, 2000, 3200, 3600 });

            //設置圖例位置
            chart.Legend.Position = LegendPosition.Top;

            //保存結果文檔
            document.SaveToFile("折線圖.docx", FileFormat.Docx);
            document.Dispose();

        }
    }
}

 

Spire.Doc for .NET 提供的 Paragraph.AppenChart(ChartType chartType, float width, float height) 方法中的 ChartType 枚舉包含了 MS Word 中預定義的各種圖表類型。因此除了柱狀圖和折線圖外,你還可以創建二維或三維的條形圖 (ChartType.Bar)、氣泡圖 (ChartType.Bubble)、餅圖 (ChartType.Pie)、散點圖 (ChartType.Scatter)、三維曲面圖 (ChartType.Surface3D) 等。

 

----如果想去除生成文檔中的紅色水印,可以點擊申請一個月的試用授權進行測試。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章