数据透视图是数据透视表的图形化展示形式。数据透视表用于对数据进行汇总并支持灵活分析,而数据透视图则将这些汇总结果以可视化图表的方式呈现出来。随着数据透视表内容的变化,数据透视图也会自动更新,因此在数据分析与报表展示中具有重要作用。
本文将介绍如何在 C# 中使用 Spire.XLS for .NET 在 Excel 中创建数据透视图。
安装 Spire.XLS for .NET
首先,需要将 Spire.XLS for .NET 包中的 DLL 文件添加为 .NET 项目的引用。这些 DLL 文件可以通过官方提供的下载链接获取,也可以通过 NuGet 进行安装。
cs
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()方法保存生成的文件。
示例代码如下:
cs
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()方法保存生成的文件。
示例代码如下:
cs
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()方法保存生成的文件。
示例代码如下:
cs
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 天的试用许可证。