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

时间:2016-10-27 20:04:04

标签: excel pivot-table epplus epplus-4

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

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

...显示如此(" 201509"和" 201510"列):

enter image description here

我希望这些值显示为" 9月15日和#34;和" 10月15日"

在Excel Interop中,它完成了这样的操作:

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

...但是在EPPlus中,相应的变量(monthYrColField)没有" NumberFormat" (或" Style")成员。

我试过了:

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

...但是,虽然它没有抱怨或造成严重破坏,但也没有改变" 201509"和" 201510"

如何在"未转换"中更改EPPlus中ColumnField列标题的格式?到" MMM yy"格式?

更新

对于VDWWD:

正如您在评论中看到的那样,有很多与数据透视表相关的东西在EPPlus中无法工作或很难开始工作;与EPPlus相比,Excel Interop是一只熊(而不是泰迪熊或考拉,但更像是灰熊),但对于数据透视表来说,与Exterop相比,EPPlus似乎有点半生不熟。 -a-脆度。

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();
}

2 个答案:

答案 0 :(得分:2)

您可以使用内置日期格式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;

答案 1 :(得分:1)

似乎您不能在字段本身上设置格式。您必须通过数据透视表对象进行访问:

pivotTable.DataFields[0].Format = "MMM yy";

应用于基础工作表的任何格式似乎都被完全忽略了。