2014-11-04 44 views
1

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

回答

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

} 

编辑:

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(); 
} 
+0

:与VBA宏然后重新保存在片材作为非宏XLSX否则谢谢厄尼。我会很快尝试。这只会复制值而不是数据透视表的定义?我想让用户编辑新工作表上的值。 – 2014-11-04 13:49:40

+0

@RandyMinder它将直接将值复制到新工作表,以便数据透视表仍将链接到原始数据,而不是复制的。根据你的文章,我认为这是你想要的? – Ernie 2014-11-04 14:06:28

+0

这不是我想要的。您的代码正在制作源数据的副本,而不是旋转数据。我想将旋转的数据值复制到新的工作表。 – 2014-11-04 14:14:36

相关问题