C#教程

关注公众号 jb51net

关闭
首页 > 软件编程 > C#教程 > C# Excel创建数据透视图

C#代码实现在Excel中创建数据透视图

作者:2501_93070778

数据透视图是数据透视表的图形化展示形式,主要是将汇总的数据结果以可视化图表的方式呈现出来,下面我们就来看看如何在 C# 中使用 Spire.XLS for .NET 在 Excel 中创建数据透视图吧

数据透视图是数据透视表的图形化展示形式。数据透视表用于对数据进行汇总并支持灵活分析,而数据透视图则将这些汇总结果以可视化图表的方式呈现出来。随着数据透视表内容的变化,数据透视图也会自动更新,因此在数据分析与报表展示中具有重要作用。

本文将介绍如何在 C# 中使用 Spire.XLS for .NET 在 Excel 中创建数据透视图。

安装 Spire.XLS for .NET

首先,需要将 Spire.XLS for .NET 包中的 DLL 文件添加为 .NET 项目的引用。这些 DLL 文件可以通过官方提供的下载链接获取,也可以通过 NuGet 进行安装。

PM> Install-Package Spire.XLS

在 C# 中创建 Excel 数据透视图

使用 Spire.XLS for .NET,可以通过 Worksheet.Charts.Add(ExcelChartType pivotChartType, IPivotTable pivotTable) 方法,基于现有数据透视表在 Excel 中快速创建数据透视图。具体步骤如下:

示例代码如下:

using Spire.Xls;
using Spire.Xls.Core;

namespace CreatePivotChart
{
    internal class Program
    {
        static void Main(string[] args)
        {
            // 创建 Workbook 对象
            Workbook workbook = new Workbook();

            // 加载 Excel 文件
            workbook.LoadFromFile("PivotTable.xlsx");

            // 获取第一个工作表
            Worksheet sheet = workbook.Worksheets[0];

            // 获取工作表中的第一个数据透视表
            IPivotTable pivotTable = sheet.PivotTables[0];

            // 基于数据透视表创建簇状柱形图
            Chart pivotChart = sheet.Charts.Add(ExcelChartType.ColumnClustered, pivotTable);

            // 设置图表位置
            pivotChart.TopRow = 1;
            pivotChart.LeftColumn = 11;
            pivotChart.RightColumn = 20;
            pivotChart.BottomRow = 15;

            // 设置图表标题为空
            pivotChart.ChartTitle = "";

            // 保存生成文件
            workbook.SaveToFile("CreatePivotChart.xlsx", ExcelVersion.Version2013);
            workbook.Dispose();
        }
    }
}

在 Excel 中使用 C# 显示或隐藏数据透视图中的字段按钮

使用 Spire.XLS for .NET,可以方便地控制数据透视图中不同类型的字段按钮,从而自定义图表的显示效果。可控制的字段按钮类型包括:

具体步骤如下:

示例代码如下:

using Spire.Xls;
using Spire.Xls.Core;

namespace ShowOrHideFieldButtons
{
    internal class Program
    {
        static void Main(string[] args)
        {
            // 创建 Workbook 对象
            Workbook workbook = new Workbook();

            // 加载 Excel 文件
            workbook.LoadFromFile("CreatePivotChart.xlsx");

            // 获取第一个工作表
            Worksheet sheet = workbook.Worksheets[0];

            // 获取工作表中的第一个数据透视表
            IPivotTable pivotTable = sheet.PivotTables[0];

            // 基于数据透视表创建簇状柱形图
            Chart pivotChart = sheet.Charts.Add(ExcelChartType.ColumnClustered, pivotTable);

            // 设置图表位置
            pivotChart.TopRow = 1;
            pivotChart.LeftColumn = 11;
            pivotChart.RightColumn = 20;
            pivotChart.BottomRow = 15;

            // 设置图表标题为空
            pivotChart.ChartTitle = "";

            // 隐藏指定字段按钮
            pivotChart.DisplayAxisFieldButtons = false;
            pivotChart.DisplayValueFieldButtons = false;
            // pivotChart.DisplayLegendFieldButtons = false;
            // pivotChart.ShowReportFilterFieldButtons = false;
            // pivotChart.DisplayEntireFieldButtons = false;

            // 保存生成文件
            workbook.SaveToFile("HideFieldButtons.xlsx", ExcelVersion.Version2013);
            workbook.Dispose();
        }
    }
}

在 C# 中格式化 Excel 数据透视图系列

使用 Spire.XLS for .NET 在 Excel 中基于数据透视表创建数据透视图时需要注意,图表系列不会自动生成,需要手动添加系列并根据需求进行格式化设置。

具体步骤如下:

示例代码如下:

using Spire.Xls;
using Spire.Xls.Charts;
using Spire.Xls.Core;

namespace FormatChartSeries
{
    internal class Program
    {
        static void Main(string[] args)
        {
            // 创建 Workbook 对象
            Workbook workbook = new Workbook();

            // 加载 Excel 文件
            workbook.LoadFromFile("CreatePivotChart.xlsx");

            // 获取第一个工作表
            Worksheet sheet = workbook.Worksheets[0];

            // 获取工作表中的第一个数据透视表
            IPivotTable pivotTable = sheet.PivotTables[0];

            // 基于数据透视表创建簇状柱形图
            Chart pivotChart = sheet.Charts.Add(ExcelChartType.ColumnClustered, pivotTable);

            // 设置图表位置
            pivotChart.TopRow = 1;
            pivotChart.LeftColumn = 11;
            pivotChart.RightColumn = 20;
            pivotChart.BottomRow = 15;

            // 设置图表标题为空
            pivotChart.ChartTitle = "";

            // 添加图表系列
            ChartSerie series = pivotChart.Series.Add(ExcelChartType.ColumnClustered);

            // 设置柱形间距(GapWidth)
            series.GetCommonSerieFormat().GapWidth = 10;

            // 设置系列重叠(取消注释以下代码可启用)
            // series.GetCommonSerieFormat().Overlap = 100;

            // 保存生成文件
            workbook.SaveToFile("FormatChartSeries.xlsx", ExcelVersion.Version2013);
            workbook.Dispose();
        }
    }
}

到此这篇关于C#代码实现在Excel中创建数据透视图的文章就介绍到这了,更多相关C# Excel创建数据透视图内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文