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

New Post: How to create PivotTable

$
0
0
I'll be adding a blog on how to achieved this @ www.bluechilli.com but until then I'll copy here what I'm posting:

ClosedXML and Excel - How to add a pivot table

Pivot tables can be 90% done using ClosedXML by using a excel template as a base.

Excel template steps
  1. In excel setup a named table with appropriate header columns and data.
  2. Create a pivot table as desired using that named table as the source.
  3. Ensure in pivot options data saved is unchecked and refresh on file load is checked
  4. Blank table data leaving 1 blank row in the named table range.
In ClosedXML, expand range of table (rows and columns) and insert new data directly into spreadsheet

The missing 10% is any new columns are not shown by default, the user will need to pick the columns.

Done!

ASP.Net C# Example:

using (var wb = new XLWorkbook(HostingEnvironment.MapPath("~/App_Data/My_Template.xlsx")))
{

....

region Networth

IXLWorksheet wsNWData;
wb.Worksheets.TryGetWorksheet("NWData", out wsNWData);

var nwTable = wsNWData.Table("NWTable");
nwTable.InsertRowsBelow(exportData.Networth.Count - 1, true);
if (exportData.Dates.Count > 2) nwTable.InsertColumnsAfter(exportData.Dates.Count - 2, true); //Handle non-default columns

Then using wsNWData.Cell(x,y) set any new column headers and add data rows

Viewing all articles
Browse latest Browse all 1877

Trending Articles



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