带有错误列字段顺序的EPPlus的数据透视表

时间:2016-07-27 14:50:54

标签: c# excel pivot epplus

我创建了一个数据透视表,其中包含以下列字段YEAR,MONTH和DAY。

以下是列的代码:

            //Column Year
            pvt.ColumnFields.Add(pvt.Fields[sheet.Cells[2, 2].Value.ToString()]);
            //Column Month
            pvt.ColumnFields.Add(pvt.Fields[sheet.Cells[2, 3].Value.ToString()]);
            //Column Day
            pvt.ColumnFields.Add(pvt.Fields[sheet.Cells[2, 4].Value.ToString()]);
            //Sort by the last column = day because if first row from data table is for example 10 all the other months start 10 in the columns fields and then after the 30/31 the library adds 1 to 9.
            pvt.ColumnFields[pvt.ColumnFields.Count - 1].Sort = OfficeOpenXml.Table.PivotTable.eSortType.Ascending;

Excel中的结果:

列年份>月份>一天(好)

Picture1

它工作正常,但如果我在开头添加另一列,则分组列出错!!!

            //Column account
            if (addAccount == true)
            {
                pvt.ColumnFields.Add(pvt.Fields[sheet.Cells[2, 9].Value.ToString()]);
            }
            //Column Year
            pvt.ColumnFields.Add(pvt.Fields[sheet.Cells[2, 2].Value.ToString()]);
            //Column Month
            pvt.ColumnFields.Add(pvt.Fields[sheet.Cells[2, 3].Value.ToString()]);
            //Column Day
            pvt.ColumnFields.Add(pvt.Fields[sheet.Cells[2, 4].Value.ToString()]);
            //Sort by the last column = day because if first row from data table is for example 10 all the other months start 10 in the columns fields and then after the 30/31 the library adds 1 to 9.
            pvt.ColumnFields[pvt.ColumnFields.Count - 1].Sort = OfficeOpenXml.Table.PivotTable.eSortType.Ascending;

结果excel:

列:帐户>日>月份>年份(错误)

应该是:帐户>年份>月份>一天(好)

enter image description here

那么,这有什么问题?

0 个答案:

没有答案