使用C#代码在 Excel 中创建数据透视图

张开发
2026/4/15 9:53:02 15 分钟阅读

分享文章

使用C#代码在 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 中快速创建数据透视图。具体步骤如下创建Workbook类的对象。使用Workbook.LoadFromFile()方法加载 Excel 文件。通过Workbook.Worksheets[index]获取指定工作表。通过Worksheet.PivotTables[index]获取指定数据透视表。使用Worksheet.Charts.Add(ExcelChartType pivotChartType, IPivotTable pivotTable)方法基于数据透视表添加数据透视图。设置数据透视图的位置和标题。使用Workbook.SaveToFile()方法保存生成的文件。示例代码如下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可以方便地控制数据透视图中不同类型的字段按钮从而自定义图表的显示效果。可控制的字段按钮类型包括全部字段按钮报表筛选字段按钮图例字段按钮坐标轴字段按钮值字段按钮具体步骤如下创建Workbook类的对象。使用Workbook.LoadFromFile()方法加载 Excel 文件。通过Workbook.Worksheets[index]获取指定工作表。通过Worksheet.PivotTables[index]获取指定数据透视表。使用Worksheet.Charts.Add(ExcelChartType pivotChartType, IPivotTable pivotTable)方法基于数据透视表添加数据透视图。设置数据透视图的位置和标题。通过Chart.DisplayAxisFieldButtons和Chart.DisplayValueFieldButtons属性隐藏指定字段按钮例如坐标轴字段按钮和值字段按钮。使用Workbook.SaveToFile()方法保存生成的文件。示例代码如下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 中基于数据透视表创建数据透视图时需要注意图表系列不会自动生成需要手动添加系列并根据需求进行格式化设置。具体步骤如下创建Workbook类的对象。使用Workbook.LoadFromFile()方法加载 Excel 文件。通过Workbook.Worksheets[index]获取指定工作表。通过Worksheet.PivotTables[index]获取指定数据透视表。使用Worksheet.Charts.Add(ExcelChartType pivotChartType, IPivotTable pivotTable)方法基于数据透视表添加数据透视图。设置数据透视图的位置和标题。使用Chart.Series.Add()方法手动添加图表系列并对系列进行样式和格式设置。使用Workbook.SaveToFile()方法保存生成的文件。示例代码如下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(); } } }申请临时许可证如果您希望移除生成文档中的评估提示信息或解除功能限制可以申请一个 30 天的试用许可证。

更多文章