简体   繁体   English

在Excel Pivot中过滤DateTime字段以获取年,月和日

[英]Filter a DateTime field for year,month and day in excel pivot

I'm using EPPlus to create Excel reports. 我正在使用EPPlus创建Excel报告。 Now i'm trying to create a Pivot with a DateTime PageField , so that the user can filter the period he want to see by himself. 现在,我尝试使用DateTime PageField创建一个Pivot,以便用户可以自己过滤要查看的时间段。 But although i can filter this for year,month or days in the according data worksheet by default, i don't get it working in the Pivot. 但是尽管默认情况下我可以在相应的数据工作表中针对年,月或日对此进行过滤,但我无法在Pivot中使用它。

Here is what i have: 这是我所拥有的:

Dim wsPivot = excel.Workbook.Worksheets.Add("Pivot")
Dim wsData = excel.Workbook.Worksheets.Add("Data")
Dim source = workSheet.GetDataSource
wsData.Cells("A1").LoadFromDataTable(source, True, OfficeOpenXml.Table.TableStyles.Medium6)
For Each col As DataColumn In source.Columns
    If col.DataType = GetType(Date) Then
        Dim colNumber = col.Ordinal + 1
        Dim range = wsData.Cells(1, colNumber, source.Rows.Count, colNumber)
        range.Style.Numberformat.Format = "dd.mm.yyyy"
   End If
Next
Dim dataRange = wsData.Cells(wsData.Dimension.Address.ToString())
dataRange.AutoFitColumns()
Dim pivotTable = wsPivot.PivotTables.Add(wsPivot.Cells("A3"), dataRange, "Open Contacts")
pivotTable.PageFields.Add(pivotTable.Fields("OwnedAt")) '** this is the date column i want to group **'
pivotTable.PageFields.Add(pivotTable.Fields("Owner"))
pivotTable.RowFields.Add(pivotTable.Fields("Country"))
pivotTable.ColumnFields.Add(pivotTable.Fields("Status"))
pivotTable.DataFields.Add(pivotTable.Fields("Count"))

It's only possible to select datetime values from the list including hours and minutes and not to group by month fe(see picture below). 只能从列表中选择日期时间值,包括小时和分钟,而不能按月份fe分组(请参见下图)。

在此处输入图片说明

Here is the filter-list from the data-worksheet. 这是数据工作表中的过滤器列表。 How to get this in pivot? 如何获得这一点?

在此处输入图片说明

Thank you in advance. 先感谢您。

Note : I've also asked this on codeplex without success. 注意 :我也曾在Codeplex上询问此问题但没有成功。 Maybe someone else can help even if he has no experiences with EPPlus but knows similar issues with other libraries. 即使他没有使用EPPlus的经验,但知道其他库的类似问题,也许其他人也可以提供帮助。 I could split the DateTime values into separate columns per sql(year,month,day). 我可以将DateTime值分成每个sql(年,月,日)单独的列。 But i'm afraid that the pivot will get more confusing with so many fields( i also would have to split the other date fields). 但是我担心枢轴将使许多字段变得更加混乱(我也必须拆分其他日期字段)。 Even with no experiences at all in programatically generating pivots, what would experienced excel users(i'm not) recommend? 即使完全没有以编程方式生成数据透视图的经验,有经验的excel用户(我也不)会推荐什么?

The reason why excel could not group by my date-field neither in PageField nor in RowField was because there was one unformatted row in the data sheet. Excel无法在PageField和RowField中都不按日期字段分组的原因是因为数据表中有一个未格式化的行。 The following lines cause this issue(see complete code in my question): 以下几行会导致此问题(请参阅我的问题中的完整代码):

Dim range = wsData.Cells(1, colNumber, source.Rows.Count, colNumber)
range.Style.Numberformat.Format = "dd.mm.yyyy"

The parameters in the range's Cells-Method are: 范围的“单元格方法”中的参数为:

1. From-Row 2. From-Column 3. To-Row 4. To-Column

Because LoadFromDataTable loaded the DataTable with the Colums' names as header, the last row kept unformatted, therefore the pivot cannot group the date. 由于LoadFromDataTable以Colums的名称作为标题加载了DataTable,因此最后一行保持未格式化,因此数据透视表无法对日期进行分组。

So this fixed it: 因此,此修复程序:

Dim range = wsData.Cells(2, colNumber, source.Rows.Count + 1, colNumber)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM