vb.net - 如何在数据透视表上设置列名称

标签 vb.net pivot-table epplus

使用this answer我能够在 EPPlus 中创建具有表格布局的数据透视表。

但是,列标题无法正确显示。当我使用 Excel 创建数据透视表时,我得到列标题“GA Category”和“Container”: Pivot table by Excel


当我通过 EPPlus 创建数据透视表时,我得到列标题“行标签”和“列标签” Pivot table by EPPlus

我想知道如何通过 EPPlus 创 build 置列标题。

最佳答案

当前软件包无法通过 EPPLus 设置列标题。为此,我需要修改项目中的 ExcelPivotTable.cs,添加以下代码:

public string ColHeaderCaption
{
    get
    {
        return GetXmlNodeString("@colHeaderCaption");
    }
    set
    {
        SetXmlNodeString("@colHeaderCaption");
    }
}


然后可以通过数据透视表类设置行和列标题:

'Sheet containing the data
Dim dataSheet as ExcelWorksheet
dataSheet = package.Workbook.WorkSheets(0)

'Data used on pivot table - default to entire sheet
Dim dataRange as ExcelRangeBase
dataRange = dataSheet.Cells(dataSheet.Dimension.Address)

'New sheet for the pivot table
Dim sheet as ExcelWorkSheet
sheet = package.Workbook.Worksheets.Add("Pivot")
package.Workbook.Worksheets.MoveToStart(sheet.Name)
sheet.View.TabSelected = true

'Create the pivot table
Dim pivot as Table.PivotTable.ExcelPivotTable
pivot = sheet.PivotTables.Add(sheet.Cells("A1"), dataRange, "PivotTable")

'Add row field
pivot.RowFields.Add(pivot.Fields("RowField"))

'Set row caption
pivot.RowHeaderCaption = "My Row Caption"

'Add column field
pivot.ColumnFields.Add(pivot.Fields("ColumnField"))

'Set column caption
pivot.ColumnHeaderCaption = "My Column Caption"

如果您不能或不想修改 EPPlus,您仍然可以通过在创建数据透视表后修改 XML 来将标题添加到数据透视表中:

pivot.PivotTableXml.DocumentElement.SetAttribute("colHeaderCaption", "My Column Caption");

关于vb.net - 如何在数据透视表上设置列名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14444744/

相关文章:

c++ - VB.net 等效于 C++ 代码

vb.net - 如何在执行期间诊断我的VB.NET程序崩溃?

C#:EPPLUS:LoadFromCollection

c# - 如何使用 EPPlus 库为 Excel 创建多样式单元格

c# - 将日期时间格式 -"yyyyMMddhhmmssfff"的字符串转换为“DD-MM-YYYY HH :MM) before exporting DataTable to excel

vb.net - 异步事件被触发

c# - 有没有办法在计算机连接到 VPN 时检测其真实的外部 IP?

C# Excel VSTO - 可以移动数据透视表吗?

php - MySQL 中的数据透视表未正确返回

MySQL:两次链接到同一个表但不同的行