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

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

本文将介绍如何在 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.DisplayAxisFieldButtonsChart.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 天的试用许可证。

相关推荐
慧都小妮子9 小时前
标签打印如何减少手工抄数?用Bartender 直连 Excel 批量打印
excel·bartender·条码打印·标签打印软件·数据驱动打印·工业打印机
SunnyDays101117 小时前
Java 设置 Excel 数据验证:整数、日期、文本长度、下拉列表和时间
java·excel·数据验证·下拉列表
Metaphor69218 小时前
使用 Python 设置 Excel 行列自适应 【代码示例】
python·excel
用户0332126663671 天前
使用 Python 设置 Excel 行列自适应 【代码示例】
python·excel
企业数字化笔记1 天前
固定资产Excel批量导入怎么防重复?资产编码、重复检查和错误回执
java·后端·excel
CORNERSTONE3652 天前
低代码开发有哪些应用场景?
低代码·excel·erp·mes·企业数字化
小葱炖豆腐2 天前
python绘制excel折线图
python·excel·numpy·pandas·matplotlib
cspttty3 天前
2026年财务分析岗JD中的Excel、SQL、BI与业务分析要求
大数据·sql·excel
cspttty3 天前
HR数字化校招准备路线:Excel、SQL、BI和AI工具怎么学
人工智能·sql·excel
鲲穹AI种草4 天前
Excel 表格批量处理与数据整理,多款表格工具能力客观记录
excel·表格处理