I'm using EPPlus in .Net and have created a pivot table with a row field and one summary data field.
Does anyone know how to create charts from a pivot table? And I don't know pivot table's range.
Here is my code:
static void DT2XL(DataTable dt, ExcelPackage excel)
{
var wsData = excel.Workbook.Worksheets["Eco-Data"];
var wsPivot = excel.Workbook.Worksheets["Eco-Pivot"];
if (wsData == null) wsData = excel.Workbook.Worksheets.Add("Eco-Data");
if (wsPivot == null) wsPivot = excel.Workbook.Worksheets.Add("Eco-Pivot");
wsData.Cells["A1"].LoadFromDataTable(dt, true, OfficeOpenXml.Table.TableStyles.Medium6);
foreach (DataColumn col in dt.Columns)
{
if (col.DataType == typeof(System.DateTime))
{
var colNumber = col.Ordinal + 1;
var range = wsData.Cells[2, colNumber, dt.Rows.Count + 1, colNumber];
range.Style.Numberformat.Format = "dd.MM.yyyy";
}
}
var dataRange = wsData.Cells[wsData.Dimension.Address.ToString()];
dataRange.AutoFitColumns();
var pivotTable = wsPivot.PivotTables.Add(wsPivot.Cells["A1"], dataRange, "EcoPivotTable");
pivotTable.MultipleFieldFilters = true;
pivotTable.RowGrandTotals = true;
pivotTable.ColumGrandTotals = true;
pivotTable.Compact = true;
pivotTable.CompactData = true;
pivotTable.GridDropZones = false;
pivotTable.Outline = false;
pivotTable.OutlineData = false;
pivotTable.ShowError = true;
pivotTable.ErrorCaption = "[error]";
pivotTable.ShowHeaders = true;
pivotTable.UseAutoFormatting = true;
pivotTable.ApplyWidthHeightFormats = true;
pivotTable.ShowDrill = true;
pivotTable.FirstDataCol = 3;
pivotTable.RowHeaderCaption = "Үзүүлэлтүүд";
pivotTable.GrandTotalCaption = "Нийт";
foreach (DataColumn c in dt.Columns)
{
var field = pivotTable.Fields[c.ColumnName];
if (c.ColumnName.ToLower().StartsWith("column") ||
c.ColumnName.ToLower().StartsWith("row"))
pivotTable.RowFields.Add(field);
else if (c.ColumnName.ToLower().StartsWith("data"))
pivotTable.DataFields.Add(field);
}
//I want to create a chart from 'pivotTable'
//...
//...
//...
//...
//...
}
Thank you!