Quantcast
Channel: ClosedXML - The easy way to OpenXML
Viewing all articles
Browse latest Browse all 1877

New Post: PivotTable row and column labels broken by recent change?

$
0
0
I'm using code to dynamically create a spreadsheet to export data from a database.

I've taken my code, put in one row of hard-coded sample data, and removed most of the columns to leave a fairly simple reproduction scenario.

This attempts to create a pivot table using the "Category" and "Item" fields in the pivot table rows (they're the third and fourth columns in the source data), but the spreadsheet when opened gives the error as before, and the pivot table shows the date and quantity as the pivot rows (which were the first two columns in the source data).
    /// <summary>
    /// Create OpenXML spreadsheet for export
    /// </summary>
    /// <returns></returns>
    public XLWorkbook GenerateOrderLineSpreadsheet(IEnumerable<OrderLine> orderLines)
    {
      var workbook = new XLWorkbook();
      workbook.SetDocumentProperties(this, "Order lines");

      var sheet = workbook.Worksheets.Add("orderlines");

      int row = 1;

      int column = 1;
      sheet.Cell(row, column++).Value = "Date";
      sheet.Cell(row, column++).Value = "Quantity";
      sheet.Cell(row, column++).Value = "Category";
      sheet.Cell(row, column++).Value = "Item";
      sheet.Cell(row, column++).Value = "Unit price";
      sheet.Cell(row, column++).Value = "Total price";

      // Sample data row
      row++;
      column = 1;
      sheet.Cell(row, column++).Value = new DateTime(2014, 6, 21);
      sheet.Cell(row, column++).Value = 1;
      sheet.Cell(row, column++).Value = "Widgets";
      sheet.Cell(row, column++).Value = "Pro widget";
      sheet.Cell(row, column++).Value = "1.23";
      sheet.Cell(row, column++).Value = "1.23";

      var dataRange = sheet.RangeUsed();

      // Add a new sheet for our pivot table
      var pivotTableSheet = workbook.Worksheets.Add("PivotTable");

      // Create the pivot table, using the data from the "PastrySalesData" table
      var pt = pivotTableSheet.PivotTables.AddNew("PivotTable", pivotTableSheet.Cell(1, 1), dataRange);

      // The rows in our pivot table will be the names of the categories
      pt.RowLabels.Add("Category");
      pt.RowLabels.Add("Item");

      pt.Values.Add("Total price");

      return workbook;
    }

Viewing all articles
Browse latest Browse all 1877

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>