C# 创建含多层分类标签的Excel图表

C#

浏览数:131

2019-4-19

相较于数据,图表更能直观的体现数据的变化趋势。在数据表格中,同一数据值,可能同时代表不同的数据分类,表现在图表中则是一个数据体现在多个数据分类标签下。通常生成的图表一般默认只有一种分类标签,下面的方法将通过编程的方式来介绍在Excel中如何来生成含多层分类标签的图表。 

使用工具:Spire.XLS for .NET

1.通过官网(https://www.e-iceblue.cn/Introduce/Spire-XLS-NET.html )下载安装包–解压-安装。在程序中添加引用Spire.XLS.dll,dll文件在安装路径下的bin文件夹中。

2.通过 Nuget 官网。(http://www.nuget.org/packages/Spire.XLS/

添加引用完成后,如下图:

C#示例代码

 Step1:创建工作表

//创建Workbook实例,获取工作表
Workbook wb = new Workbook();
Worksheet sheet = wb.Worksheets[0];

Step2:写入数据到表格

 sheet.Range["A2"].Text = "出口前";
 sheet.Range["A5"].Text = "出口后";
 sheet.Range["B1"].Text = "年份";
 sheet.Range["B2"].Text = "2017年";
 sheet.Range["B6"].Text = "2018年";
 sheet.Range["C1"].Text = "季度";
 sheet.Range["C2"].Text = "1季度";
 sheet.Range["C3"].Text = "2季度";
 sheet.Range["C4"].Text = "3季度";
 sheet.Range["C5"].Text = "4季度";
 sheet.Range["C6"].Text = "1季度";
 sheet.Range["C7"].Text = "2季度";
 sheet.Range["C8"].Text = "3季度";
 sheet.Range["C9"].Text = "4季度";
 sheet.Range["D1"].Text = "季度产量\n(万吨)";
 sheet.Range["D2"].Value = "1.56";
 sheet.Range["D3"].Value = "2.3";
 sheet.Range["D4"].Value = "3.21";
 sheet.Range["D5"].Value = "3.5";
 sheet.Range["D6"].Value = "4.8";
 sheet.Range["D7"].Value = "5.2";
 sheet.Range["D8"].Value = "5.79";
 sheet.Range["D9"].Value = "5.58";

Step3:合并单元格并设置单元格对齐方式

sheet.Range["A2:A4"].Merge();
sheet.Range["A5:A9"].Merge();
sheet.Range["B2:B5"].Merge();
sheet.Range["B6:B9"].Merge();
sheet.Range["A1:D9"].Style.HorizontalAlignment = HorizontalAlignType.Center;
sheet.Range["A1:D9"].Style.VerticalAlignment = VerticalAlignType.Center;

Step4:添加图表到Excel

//添加柱状图表
Chart chart = sheet.Charts.Add(ExcelChartType.LineMarkers);
chart.ChartTitle = "季度产量(万吨)";//设置图表标题     
chart.PlotArea.Fill.FillType = ShapeFillType.NoFill; //不填充绘图区域(默认填充灰色)
chart.Legend.Delete();//删除图例

//指定柱状图表在工作表中的位置及宽度
chart.LeftColumn = 5;
chart.TopRow = 1;
chart.RightColumn = 14;

//设置图表系列数据来源
chart.DataRange = sheet.Range["D2:D9"];
chart.SeriesDataFromRange = false;
chart.Series[0].DataPoints.DefaultDataPoint.DataLabels.HasValue = true;
chart.Series[0].Format.LineProperties.Color = Color.BlueViolet;

//设置系列分类标签数据来源
ChartSerie serie = chart.Series[0];
serie.CategoryLabels = sheet.Range["A2:C9"];

Step5:设置是否显示多层分类标签

chart.PrimaryCategoryAxis.MultiLevelLable = true;

Step6: 保存文档

wb.SaveToFile("output.xlsx", ExcelVersion.Version2013);

图表创建结果:

全部代码:

using Spire.Xls;
using Spire.Xls.Charts;
using System.Drawing;

namespace ChartWithMultiLevelCategoryLabels_XLS
{
    class Program
    {
        static void Main(string[] args)
        {
            //创建Workbook实例,获取工作表
            Workbook wb = new Workbook();
            Worksheet sheet = wb.Worksheets[0];

            //写入数据到工作表
            sheet.Range["A2"].Text = "出口前";
            sheet.Range["A5"].Text = "出口后";
            sheet.Range["B1"].Text = "年份";
            sheet.Range["B2"].Text = "2017年";
            sheet.Range["B6"].Text = "2018年";
            sheet.Range["C1"].Text = "季度";
            sheet.Range["C2"].Text = "1季度";
            sheet.Range["C3"].Text = "2季度";
            sheet.Range["C4"].Text = "3季度";
            sheet.Range["C5"].Text = "4季度";
            sheet.Range["C6"].Text = "1季度";
            sheet.Range["C7"].Text = "2季度";
            sheet.Range["C8"].Text = "3季度";
            sheet.Range["C9"].Text = "4季度";
            sheet.Range["D1"].Text = "季度产量\n(万吨)";
            sheet.Range["D2"].Value = "1.56";
            sheet.Range["D3"].Value = "2.3";
            sheet.Range["D4"].Value = "3.21";
            sheet.Range["D5"].Value = "3.5";
            sheet.Range["D6"].Value = "4.8";
            sheet.Range["D7"].Value = "5.2";
            sheet.Range["D8"].Value = "5.79";
            sheet.Range["D9"].Value = "5.58";
            

            //合并单元格,设置单元格对齐方式
            sheet.Range["A2:A4"].Merge();
            sheet.Range["A5:A9"].Merge();
            sheet.Range["B2:B5"].Merge();
            sheet.Range["B6:B9"].Merge();
            sheet.Range["A1:D9"].Style.HorizontalAlignment = HorizontalAlignType.Center;
            sheet.Range["A1:D9"].Style.VerticalAlignment = VerticalAlignType.Center;


            //添加柱状图表
            Chart chart = sheet.Charts.Add(ExcelChartType.LineMarkers);
            chart.ChartTitle = "季度产量(万吨)";//设置图表标题     
            chart.PlotArea.Fill.FillType = ShapeFillType.NoFill; //不填充绘图区域(默认填充灰色)
            chart.Legend.Delete();//删除图例

            //指定柱状图表在工作表中的位置及宽度
            chart.LeftColumn = 5;
            chart.TopRow = 1;
            chart.RightColumn = 14;

            //设置图表系列数据来源
            chart.DataRange = sheet.Range["D2:D9"];
            chart.SeriesDataFromRange = false;
            chart.Series[0].DataPoints.DefaultDataPoint.DataLabels.HasValue = true;
            chart.Series[0].Format.LineProperties.Color = Color.BlueViolet;

            //设置系列分类标签数据来源
            ChartSerie serie = chart.Series[0];
            serie.CategoryLabels = sheet.Range["A2:C9"];


            //显示多层分类标签
            chart.PrimaryCategoryAxis.MultiLevelLable = true;

            //保存文档
            wb.SaveToFile("output.xlsx", ExcelVersion.Version2013);
            System.Diagnostics.Process.Start("output.xlsx");
        }
    }
}

(本文完)