1

Resolved

Multiple DataFields in PivotTable cause Excel to crash

description

When adding multpile DataFields based on the same Field, but different Functions to the PivotTable it causes the Excel to crash after opening the file generated in that way.

Smple code:
    public void PT()
    {
        FileInfo fi = new FileInfo(@".\test.xlsx");
        ExcelPackage pck = new ExcelPackage(fi);

        var ws = pck.Workbook.Worksheets.Add("Data");
        var pv = pck.Workbook.Worksheets.Add("Pivot");

        ws.Cells["A1"].Value = "Data1";
        ws.Cells["B1"].Value = "Data2";

        ws.Cells["A2"].Value = "1";
        ws.Cells["B2"].Value = "2";

        ws.Cells["A3"].Value = "3";
        ws.Cells["B3"].Value = "4";

        ws.Select("A1:B3");

        var pt = pv.PivotTables.Add(pv.SelectedRange, ws.SelectedRange, "Pivot");

        pt.RowFields.Add(pt.Fields["Data2"]);

        var df=pt.DataFields.Add(pt.Fields["Data1"]);
        df.Function = DataFieldFunctions.Count;

        df=pt.DataFields.Add(pt.Fields["Data2"]);
        df.Function = DataFieldFunctions.Sum;

        pck.Save();
    }

file attachments

comments

ted_250 wrote Jul 29, 2012 at 9:19 PM

Good morning,

I've built a test application using your code (above) and tested opening the file in Excel 2007 without a problem. My code is later than what you'd have had available in April, so I'd assume it was fixed by something fixing another bug. I'm not sure the current result is correct, though, only that it opens without an error.

I did add one bit to your code to make the new functions a bit more distinct:

var df = pt.DataFields.Add(pt.Fields["Data1"]);
df.Name = "counted"; // ted_250
df.Function = DataFieldFunctions.Count;

df = pt.DataFields.Add(pt.Fields["Data2"]);
df.Name = "summed"; // ted_250
df.Function = DataFieldFunctions.Sum;

My successful output then looked like the attached image. This doesn't look entirely right to me (I don't think either sum should be zero!) but that would be a different bug it the value is wrong.

Ted

ted_250 wrote Jul 29, 2012 at 9:20 PM

Adding my screen shot to this bug.

Ted