使用this answer我能够在EPPlus中创建一个带有表格布局的数据透视表。
但是列标题显示不正确。当我使用Excel创建数据透视表时,我得到列标题“GA类别”和“容器”:
当我通过EPPlus创建数据透视表时,我得到列标题“行标签”和“列标签”
我想知道如何通过EPPlus创建列标题。
答案 0 :(得分:6)
使用当前包无法通过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");