Sabarish
Sabarish

Reputation: 74

Pivot Table using OpenXML Power Tool in C#

I am trying to generate Pivot tables programatically using C# OpenXML Power tool. I am trying to export the raw data to sheet 1 and Pivot table on sheet1. When I am adding an integer column as a data value, Pivot table is generated with proper values. But, if I select a non-integer column,count and sum generated is always zero. Following is the snapshot of the excel file it is generating.enter image description here

Data in Pivot.txt is in format as mentioned below: Year,Quarter,Region,Category,Product,Amount 2010,Q1,North,Bicycles,B100,3448.00 2010,Q1,North,Bicycles,B200,8448.00 2010,Q1,North,Bicycles,B300,922.00 2010,Q1,North,Accessories,Mirrors,303.00 2010,Q1,North,Accessories,Carriers,455.00 2010,Q1,South,Bicycles,B100,2887.00 2010,Q1,South,Bicycles,B200,4477.00 2010,Q1,South,Bicycles,B300,1011.00 2010,Q1,South,Accessories,Mirrors,80.00 2010,Q1,South,Accessories,Carriers,205.00

Following is the code I am using:

private static void generatePivot()
        {
            int row;
            row = 1;
            int maxColumn = 1;
            using (OpenXmlMemoryStreamDocument streamDoc = OpenXmlMemoryStreamDocument.CreateSpreadsheetDocument())
            {
                using (SpreadsheetDocument doc = streamDoc.GetSpreadsheetDocument())
                {
                    WorksheetPart sheet = WorksheetAccessor.AddWorksheet(doc, "Range");
                    using (System.IO.StreamReader source = new System.IO.StreamReader("../../PivotData.txt"))
                    {
                        while (!source.EndOfStream)
                        {
                            string line = source.ReadLine();
                            if (line.Length > 3)
                            {
                                string[] fields = line.Split(',');
                                int column = 1;
                                foreach (string item in fields)
                                {
                                    double num;
                                    if (double.TryParse(item, out num))
                                        WorksheetAccessor.SetCellValue(doc, sheet, row, column++, num);
                                    else
                                        WorksheetAccessor.SetCellValue(doc, sheet, row, column++, item);
                                }
                                maxColumn = column - 1;
                            }
                            row++;
                        }
                    }
                    sheet.PutXDocument();
                    WorksheetAccessor.SetRange(doc, "Sales", "Range", 1, 1, row - 1, maxColumn);
                    WorksheetPart pivot = WorksheetAccessor.AddWorksheet(doc, "Pivot");
                    WorksheetAccessor.CreatePivotTable(doc, "Sales", pivot);


                    // Configure pivot table rows, columns, data and filters
                    //WorksheetAccessor.AddPivotAxis(doc, pivot, "Year", WorksheetAccessor.PivotAxis.Column);
                    WorksheetAccessor.AddPivotAxis(doc, pivot, "Quarter", WorksheetAccessor.PivotAxis.Column);
                    WorksheetAccessor.AddPivotAxis(doc, pivot, "Category", WorksheetAccessor.PivotAxis.Row);
                    //WorksheetAccessor.AddPivotAxis(doc, pivot, "Product", WorksheetAccessor.PivotAxis.Row);
                    WorksheetAccessor.AddDataValue(doc, pivot, "Product");
                    WorksheetAccessor.AddPivotAxis(doc, pivot, "Region", WorksheetAccessor.PivotAxis.Page);
            }
            streamDoc.GetModifiedSmlDocument().SaveAs("../../NewPivot.xlsx");
        }
    }

Upvotes: 2

Views: 1723

Answers (0)

Related Questions