Skip to content
This repository has been archived by the owner on Mar 9, 2020. It is now read-only.

Bad date sorting order in Pivot with date grouping #44

Open
cdokolas opened this issue Oct 31, 2017 · 1 comment
Open

Bad date sorting order in Pivot with date grouping #44

cdokolas opened this issue Oct 31, 2017 · 1 comment

Comments

@cdokolas
Copy link

I have the following test code:

class PivotTest
{
	private static string PIVOT_WS_NAME = "Pivot";
	private static string DATA_WS_NAME = "Data";
	internal void RunTest()
	{
		using (ExcelPackage xlp = new ExcelPackage())
		{
			PrepareDoc(xlp);
			GenPivot(xlp);

			FileStream fs = File.Create("pivot.xlsx");
			xlp.SaveAs(fs);
			fs.Close();
		}
	}

	private void PrepareDoc(ExcelPackage xlp)
	{
		//generate date/value pairs for October 2017
		var series = Enumerable.Range(0, 31);
		var data = from x in series
				   select new { d = new DateTime(2017, 10, x + 1), x = x };
		//put data in table
		ExcelWorksheet ws = xlp.Workbook.Worksheets.Add(DATA_WS_NAME);
		int col = 1;
		ws.Cells[1, col++].Value = "Date";
		ws.Cells[1, col].Value = "Value";
		int row = 2;
		foreach (var line in data)
		{
			col = 1;
			ws.Cells[row, col++].Value = line.d;
			ws.Cells[row, col - 1].Style.Numberformat.Format = DateTimeFormatInfo.CurrentInfo.ShortDatePattern;
			ws.Cells[row, col].Value = line.x;
			row++;
		}
	}

	private void GenPivot(ExcelPackage xlp)
	{
		ExcelWorksheet ws = xlp.Workbook.Worksheets.Add(PIVOT_WS_NAME);
		ExcelWorksheet srcws = xlp.Workbook.Worksheets[DATA_WS_NAME];
		ExcelPivotTable piv = ws.PivotTables.Add(ws.Cells[1, 1], srcws.Cells[1, 1, 32, 2], "Pivot1");
		piv.DataFields.Add(piv.Fields["Value"]);
		ExcelPivotTableField dt = piv.RowFields.Add(piv.Fields["Date"]);
		dt.AddDateGrouping(eDateGroupBy.Days | eDateGroupBy.Months);
	}
}

When I open the pivot.xlsx file, I see the following (note that my system is setup with the Greek locale):

image

I un-zipped the xlsx file and the files seem all right; however, if I save the file from Excel without making any changes, the pivotTable1.xml file has the items in the pivotField in the wrong order. From that, I'm guessing that Excel messes-up the data when it reads the file.

@ramtejsudani
Copy link

Hi,
Can anyone let me know if this issue is resolved? Or is there any workaround for this issue?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants