EPPlus Pivot Table - Copy Values to New Sheet

北战南征 提交于 2019-11-28 11:33:41

问题


I've successfully created a pivot table using EPPlus. The pivot table resides in a seperate sheet from the raw data. I would like to copy the pivot table data to a new, third, sheet, but just the values, not the pivot defintions. Does EPPlus support this?


回答1:


You can just copy the datasource range via Cache Definition:

public void PivotDataCopy()
{
    const string FORMATCURRENCY = "#,###;[Red](#,###)";

    var file = new FileInfo(@"c:\temp\temp.xlsx");
    if (file.Exists)
        file.Delete();

    var pck = new ExcelPackage(file);
    var workbook = pck.Workbook;
    var dataworksheet = workbook.Worksheets.Add("datasheet");

    //The data
    dataworksheet.Cells["A20"].Value = "Col1";
    dataworksheet.Cells["A21"].Value = "sdf";
    dataworksheet.Cells["A22"].Value = "wer";
    dataworksheet.Cells["A23"].Value = "ghgh";
    dataworksheet.Cells["A24"].Value = "sdf";
    dataworksheet.Cells["A25"].Value = "wer";

    dataworksheet.Cells["B20"].Value = "Col2";
    dataworksheet.Cells["B21"].Value = "Group A";
    dataworksheet.Cells["B22"].Value = "Group B";
    dataworksheet.Cells["B23"].Value = "Group A";
    dataworksheet.Cells["B24"].Value = "Group C";
    dataworksheet.Cells["B25"].Value = "Group A";

    dataworksheet.Cells["C20"].Value = "Col3";
    dataworksheet.Cells["C21"].Value = 453.5;
    dataworksheet.Cells["C22"].Value = 634.5;
    dataworksheet.Cells["C23"].Value = 274.5;
    dataworksheet.Cells["C24"].Value = 453.5;
    dataworksheet.Cells["C25"].Value = 634.5;

    dataworksheet.Cells["D20"].Value = "Col4";
    dataworksheet.Cells["D21"].Value = 686468;
    dataworksheet.Cells["D22"].Value = 996440;
    dataworksheet.Cells["D23"].Value = 185780;
    dataworksheet.Cells["D24"].Value = 686468;
    dataworksheet.Cells["D25"].Value = 996440;

    //The pivot table
    var pivotworksheet = workbook.Worksheets.Add("pivotsheet");
    var pivotTable = pivotworksheet.PivotTables.Add(pivotworksheet.Cells["A1"], dataworksheet.Cells["A20:D29"], "test");

    //The label row field
    pivotTable.RowFields.Add(pivotTable.Fields["Col1"]);
    pivotTable.DataOnRows = false;

    //The data fields
    var field = pivotTable.DataFields.Add(pivotTable.Fields["Col3"]);
    field.Name = "Sum of Col2";
    field.Function = DataFieldFunctions.Sum;
    field.Format = FORMATCURRENCY;

    field = pivotTable.DataFields.Add(pivotTable.Fields["Col4"]);
    field.Name = "Sum of Col3";
    field.Function = DataFieldFunctions.Sum;
    field.Format = FORMATCURRENCY;

    //Get the pivot table data source
    var newworksheet = workbook.Worksheets.Add("newworksheet");
    var pivotdatasourcerange = pivotTable.CacheDefinition.SourceRange;
    pivotdatasourcerange.Copy(newworksheet.Cells["A1"]);

    pck.Save();

}

EDIT: Doing with a VBA macro which then resaves the sheet as a non-macro XLSX:

public void PivotDataCopy()
{
    const string FORMATCURRENCY = "#,###;[Red](#,###)";

    var file = new FileInfo(@"c:\temp\temp.xlsm");
    if (file.Exists)
        file.Delete();

    var pck = new ExcelPackage(file);
    var workbook = pck.Workbook;
    var dataworksheet = workbook.Worksheets.Add("datasheet");

    //The data
    dataworksheet.Cells["A20"].Value = "Col1";
    dataworksheet.Cells["A21"].Value = "sdf";
    dataworksheet.Cells["A22"].Value = "wer";
    dataworksheet.Cells["A23"].Value = "ghgh";
    dataworksheet.Cells["A24"].Value = "sdf";
    dataworksheet.Cells["A25"].Value = "wer";

    dataworksheet.Cells["B20"].Value = "Col2";
    dataworksheet.Cells["B21"].Value = "Group A";
    dataworksheet.Cells["B22"].Value = "Group B";
    dataworksheet.Cells["B23"].Value = "Group A";
    dataworksheet.Cells["B24"].Value = "Group C";
    dataworksheet.Cells["B25"].Value = "Group A";

    dataworksheet.Cells["C20"].Value = "Col3";
    dataworksheet.Cells["C21"].Value = 453.5;
    dataworksheet.Cells["C22"].Value = 634.5;
    dataworksheet.Cells["C23"].Value = 274.5;
    dataworksheet.Cells["C24"].Value = 453.5;
    dataworksheet.Cells["C25"].Value = 634.5;

    dataworksheet.Cells["D20"].Value = "Col4";
    dataworksheet.Cells["D21"].Value = 686468;
    dataworksheet.Cells["D22"].Value = 996440;
    dataworksheet.Cells["D23"].Value = 185780;
    dataworksheet.Cells["D24"].Value = 686468;
    dataworksheet.Cells["D25"].Value = 996440;

    //The pivot table
    var pivotworksheet = workbook.Worksheets.Add("pivotsheet");
    var pivotTable = pivotworksheet.PivotTables.Add(pivotworksheet.Cells["A1"], dataworksheet.Cells["A20:D29"], "test");

    //The label row field
    pivotTable.RowFields.Add(pivotTable.Fields["Col1"]);
    pivotTable.DataOnRows = false;

    //The data fields
    var field = pivotTable.DataFields.Add(pivotTable.Fields["Col3"]);
    field.Name = "Sum of Col2";
    field.Function = DataFieldFunctions.Sum;
    field.Format = FORMATCURRENCY;

    field = pivotTable.DataFields.Add(pivotTable.Fields["Col4"]);
    field.Name = "Sum of Col3";
    field.Function = DataFieldFunctions.Sum;
    field.Format = FORMATCURRENCY;

    //add the macro to copy the table data on open
    workbook.Worksheets.Add("newworksheet");

    var sb = new StringBuilder();
    sb.AppendLine("Private Sub Workbook_SheetCalculate(ByVal Sh As Object)");
    sb.AppendLine("    Sheets(\"pivotsheet\").Cells.Copy");
    sb.AppendLine("    Sheets(\"newworksheet\").Range(\"A1\").PasteSpecial Paste:=xlPasteValues");
    sb.AppendLine("    Selection.Clear");
    sb.AppendLine("    Application.DisplayAlerts = False");
    sb.AppendLine(String.Format("    ActiveWorkbook.SaveAs \"{0}\", xlOpenXMLWorkbook", file.FullName.Replace("xlsm", "xlsx")));
    sb.AppendLine("    Application.DisplayAlerts = True");

    sb.AppendLine("End Sub");
    pck.Workbook.CreateVBAProject();
    pck.Workbook.CodeModule.Code = sb.ToString();

    pck.Save();
}


来源:https://stackoverflow.com/questions/26734787/epplus-pivot-table-copy-values-to-new-sheet

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!