数据透视图是数据透视表的图形化表示。数据透视表能够汇总数据并允许用户对其进行深入分析,而数据透视图则为这些汇总数据提供了直观的可视化效果。数据透视图能够及时更新,以反映数据透视表中的数据变化,因此它们在报告和分析过程中成为不可或缺的重要工具。本文将演示如何使用 Spire.XLS for .NET 和 C# 在 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] 属性获取 Excel 文件中的特定工作表。
- 使用 Worksheet.PivotTables[index] 属性获取工作表中的特定数据透视表。
- 使用 Worksheet.Charts.Add(pivotChartType:ExcelChartType, pivotTable:IPivotTable) 方法向工作表添加数据透视图。
- 设置数据透视图的位置和标题。
- 使用 Workbook.SaveToFile() 方法保存生成的文件。
- C#
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("示例.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;
// 将图表标题设置为 null
pivotChart.ChartTitle = "";
// 保存生成的文件
workbook.SaveToFile("创建数据透视图.xlsx", ExcelVersion.Version2013);
workbook.Dispose();
}
}
}
C# 显示或隐藏 Excel 数据透视图的字段按钮
你可以使用 Spire.XLS for .NET 在数据透视图中显示或隐藏以下字段按钮:
- 所有字段按钮
- 报告筛选字段按钮
- 图例字段按钮
- 轴字段按钮
- 数值字段按钮
详细步骤如下:
- 创建 Workbook 类的对象。
- 使用 Workbook.LoadFromFile() 方法加载 Excel 文件。
- 使用 Workbook.Worksheets[index] 属性获取 Excel 文件中的特定工作表。
- 使用 Worksheet.PivotTables[index] 属性获取工作表中的特定数据透视表。
- 使用 Worksheet.Charts.Add(ExcelChartType pivotChartType, IPivotTable pivotTable) 方法向工作表添加数据透视图。
- 设置数据透视图的位置和标题。
- 使用 Chart.DisplayAxisFieldButtons 和 Chart.DisplayValueFieldButtons 等属性隐藏数据透视图中的轴字段按钮和数值字段按钮等。
- 使用 Workbook.SaveToFile() 方法保存生成的文件。
- C#
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("创建数据透视图.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;
// 将图表标题设置为 null
pivotChart.ChartTitle = "";
// 隐藏特定字段按钮
pivotChart.DisplayAxisFieldButtons = false;
pivotChart.DisplayValueFieldButtons = false;
// pivotChart.DisplayLegendFieldButtons = false;
// pivotChart.ShowReportFilterFieldButtons = false;
// pivotChart.DisplayEntireFieldButtons = false;
// 保存生成的文件
workbook.SaveToFile("隐藏字段按钮.xlsx", ExcelVersion.Version2013);
workbook.Dispose();
}
}
}
C# 给 Excel 数据透视图系列设置格式
当使用 Spire.XLS for .NET 基于数据透视表生成数据透视图时,数据透视图的系列不会自动创建。你需要向数据透视图添加系列,然后再应用所需的格式。详细步骤如下:
- 创建 Workbook 类的对象。
- 使用 Workbook.LoadFromFile() 方法加载 Excel 文件。
- 使用 Workbook.Worksheets[index] 属性获取 Excel 文件中的特定工作表。
- 使用 Worksheet.PivotTables[index] 属性获取工作表中的特定数据透视表。
- 使用 Worksheet.Charts.Add(ExcelChartType pivotChartType, IPivotTable pivotTable) 方法向工作表添加数据透视图。
- 设置数据透视图的位置和标题。
- 使用 Chart.Series.Add() 方法向数据透视图添加系列,然后对系列应用所需的格式。
- 使用 Workbook.SaveToFile() 方法保存生成的文件。
- C#
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("创建数据透视图.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;
// 将图表标题设置为 null
pivotChart.ChartTitle = "";
// 添加图表系列
ChartSerie series = pivotChart.Series.Add(ExcelChartType.ColumnClustered);
// 设置数据条的宽度
series.GetCommonSerieFormat().GapWidth = 10;
// series.GetCommonSerieFormat().Overlap = 100;
// 保存生成的文件
workbook.SaveToFile("设置数据透视图系列格式.xlsx", ExcelVersion.Version2013);
workbook.Dispose();
}
}
}
申请临时 License
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。