如何更改EPPlus中的ColumnField列标题的格式?

我在EPPlus中创build了一个列字段,如下所示:

// Column field[s] var monthYrColField = pivotTable.Fields["MonthYr"]; pivotTable.ColumnFields.Add(monthYrColField); 

…如此显示(“201509”和“201510”列):

在这里输入图像描述

我希望这些值显示为“9月15日”和“10月15日”

在Excel Interop中,它是这样做的:

 var monthField = pvt.PivotFields("MonthYr"); monthField.Orientation = XlPivotFieldOrientation.xlColumnField; monthField.NumberFormat = "MMM yy"; 

…但在EPPlus中,相应的variables(monthYrColField)没有“NumberFormat”(或“Style”)成员。

我试过这个:

 pivotTableWorksheet.Column(2).Style.Numberformat.Format = "MMM yy"; 

…但是,虽然没有抱怨或肆虐,也没有改变“201509”和“201510”

如何将EPPlus中的ColumnField列标题的格式从“未转换”更改为“MMM yy”格式?

UPDATE

对于VDWWD:

正如您在评论中看到的那样,有许多与数据透视表相关的东西无法使用,或者难​​以在EPPlus中工作; 与EPPlus相比,Excel Interop是一只熊(而不是泰迪熊或考拉,但更像是灰熊),但对于数据透视表来说,与Exterop的油炸脆脆性相比,EPPlus似乎是一半。

 private void PopulatePivotTableSheet() { string NORTHWEST_CORNER_OF_PIVOT_TABLE = "A6"; AddPrePivotTableDataToPivotTableSheet(); var dataRange = pivotDataWorksheet.Cells[pivotDataWorksheet.Dimension.Address]; dataRange.AutoFitColumns(); var pivotTable = pivotTableWorksheet.PivotTables.Add( pivotTableWorksheet.Cells[NORTHWEST_CORNER_OF_PIVOT_TABLE], dataRange, "PivotTable"); pivotTable.MultipleFieldFilters = true; pivotTable.GridDropZones = false; pivotTable.Outline = false; pivotTable.OutlineData = false; pivotTable.ShowError = true; pivotTable.ErrorCaption = "[error]"; pivotTable.ShowHeaders = true; pivotTable.UseAutoFormatting = true; pivotTable.ApplyWidthHeightFormats = true; pivotTable.ShowDrill = true; // Row field[s] var descRowField = pivotTable.Fields["Description"]; pivotTable.RowFields.Add(descRowField); // Column field[s] var monthYrColField = pivotTable.Fields["MonthYr"]; pivotTable.ColumnFields.Add(monthYrColField); // Data field[s] var totQtyField = pivotTable.Fields["TotalQty"]; pivotTable.DataFields.Add(totQtyField); var totPriceField = pivotTable.Fields["TotalPrice"]; pivotTable.DataFields.Add(totPriceField); // Don't know how to calc these vals here, so had to put them on the data sheet var avgPriceField = pivotTable.Fields["AvgPrice"]; pivotTable.DataFields.Add(avgPriceField); var prcntgOfTotalField = pivotTable.Fields["PrcntgOfTotal"]; pivotTable.DataFields.Add(prcntgOfTotalField); // TODO: Get the sorting (by sales, descending) working: // These two lines don't seem that they would do so, but they do result in the items // being sorted by (grand) total purchases descending //var fld = ((PivotField)pvt.PivotFields("Description")); //fld.AutoSort(2, "Total Purchases"); //int dataCnt = pivotTable.ra //DataBodyRange.Columns.Count + 1; FormatPivotTable(); } private void FormatPivotTable() { int HEADER_ROW = 7; if (DateTimeFormatInfo.CurrentInfo != null) pivotTableWorksheet.Column(2).Style.Numberformat.Format = DateTimeFormatInfo.CurrentInfo.YearMonthPattern; // Pivot Table Header Row - bold and increase height using (var headerRowFirstCell = pivotTableWorksheet.Cells[HEADER_ROW, 1]) { headerRowFirstCell.Style.VerticalAlignment = ExcelVerticalAlignment.Center; headerRowFirstCell.Style.Font.Bold = true; headerRowFirstCell.Style.Font.Size = 12; pivotTableWorksheet.Row(HEADER_ROW).Height = 25; } ColorizeContractItemBlocks(contractItemDescs); // TODO: Why is the hiding not working? HideItemsWithFewerThan1PercentOfSales(); } 

您可以使用内置date格式YearMonthPattern 。 这将格式为september 2016

 pivotTableWorksheet.Column(2).Style.Numberformat.Format = DateTimeFormatInfo.CurrentInfo.YearMonthPattern; 

如果你真的想要MMM yy作为模式,你需要覆盖文化格式:

 Thread.CurrentThread.CurrentCulture = new CultureInfo("nl-NL") { DateTimeFormat = { YearMonthPattern = "MMM yy" } }; pivotTableWorksheet.Column(2).Style.Numberformat.Format = DateTimeFormatInfo.CurrentInfo.YearMonthPattern;