EPPlus数据透视表 - 将值复制到新工作表

时间:2014-11-04 12:08:53

标签: epplus

我已经使用EPPlus成功创建了一个数据透视表。数据透视表位于原始数据的单独工作表中。我想将数据透视表数据复制到新的第三张表,但只是值,而不是数据透视定义。 EPPlus支持这个吗?

1 个答案:

答案 0 :(得分:3)

您只需通过缓存定义复制数据源范围:

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();

}

编辑:使用VBA宏,然后将工作表重新保存为非宏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();
}