EPPlus数据透视表 - 折叠整个字段

时间:2012-10-11 18:38:33

标签: c# .net vb.net openxml epplus

有谁知道怎么做?

我在.Net中使用EPPlus并创建了一个包含2个行字段和一个摘要数据字段的数据透视表:

Dim Pivot As OfficeOpenXml.Table.PivotTable.ExcelPivotTable
Pivot = wksPivot.PivotTables.Add(wksPivot.Cells("A1"), Datarange, "pName")

Pivot.RowFields.Add(Pivot.Fields("Fld1")).Sort = Table.PivotTable.eSortType.Ascending
Pivot.RowFields.Add(Pivot.Fields("Fld2")).Sort = Table.PivotTable.eSortType.Ascending

Dim dtaFld As OfficeOpenXml.Table.PivotTable.ExcelPivotTableDataField
dtaFld = Pivot.DataFields.Add(Pivot.Fields("XYZ"))
dtaFld.Function = Table.PivotTable.DataFieldFunctions.Sum

一切都很好,但我希望在用户打开工作簿时将数据透视表开始折叠(在Excel中,当您创建数据透视表时,可以右键单击数据元素并选择“展开/折叠“>”折叠整个字段“

我可以通过代码来做到这一点吗? (如果EPPlus还不支持,我愿意使用直接的OpenXML ......)

ALSO ,有没有办法从工作簿中删除原始数据,以便数据透视表仍然有效?我试过,当我打开工作簿时,我的数据透视表是空白的? - 我现在的逻辑让我this question ......有什么想法吗?

(我知道我在VB中写了这个问题。但我在这个问题上添加了C#& VB标签 - 我对任何一种语言的代码感到满意 - 谢谢!!)

2 个答案:

答案 0 :(得分:2)

可以使它成为xlsm并向其添加vba。这可能是这个解决方案最糟糕的答案,但它完全崩溃了。我提供了一个工作示例,只需将过去复制到一个新的控制台应用程序中。添加epplus依赖项,“F5”。

修改/取自http://epplus.codeplex.com/SourceControl/latest#SampleApp/Sample15.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OfficeOpenXml.Table;
using OfficeOpenXml.Table.PivotTable;
using OfficeOpenXml;
using System.IO;

namespace pTable
{
    class Program
    {
        static void Main(string[] args)
        {
            //ExcelPackage _pck = new ExcelPackage();

            Directory.CreateDirectory(string.Format("Test"));
            //ExcelPackage _pck = new ExcelPackage(new FileInfo("Test\\Worksheet.xlsx"));
            ExcelPackage _pck = new ExcelPackage(new FileInfo("Test\\Worksheet.xlsm"));

            var wsPivot1 = _pck.Workbook.Worksheets.Add("Rows-Data on columns");

            var ws = _pck.Workbook.Worksheets.Add("Data");
            ws.Cells["K1"].Value = "Item";
            ws.Cells["L1"].Value = "Category";
            ws.Cells["M1"].Value = "Stock";
            ws.Cells["N1"].Value = "Price";
            ws.Cells["O1"].Value = "Date for grouping";

            ws.Cells["K2"].Value = "Crowbar";
            ws.Cells["L2"].Value = "Hardware";
            ws.Cells["M2"].Value = 12;
            ws.Cells["N2"].Value = 85.2;
            ws.Cells["O2"].Value = new DateTime(2010, 1, 31);

            ws.Cells["K3"].Value = "Crowbar";
            ws.Cells["L3"].Value = "Hardware";
            ws.Cells["M3"].Value = 15;
            ws.Cells["N3"].Value = 12.2;
            ws.Cells["O3"].Value = new DateTime(2010, 2, 28);

            ws.Cells["K4"].Value = "Hammer";
            ws.Cells["L4"].Value = "Hardware";
            ws.Cells["M4"].Value = 550;
            ws.Cells["N4"].Value = 72.7;
            ws.Cells["O4"].Value = new DateTime(2010, 3, 31);

            ws.Cells["K5"].Value = "Hammer";
            ws.Cells["L5"].Value = "Hardware";
            ws.Cells["M5"].Value = 120;
            ws.Cells["N5"].Value = 11.3;
            ws.Cells["O5"].Value = new DateTime(2010, 4, 30);

            ws.Cells["K6"].Value = "Crowbar";
            ws.Cells["L6"].Value = "Hardware";
            ws.Cells["M6"].Value = 120;
            ws.Cells["N6"].Value = 173.2;
            ws.Cells["O6"].Value = new DateTime(2010, 5, 31);

            ws.Cells["K7"].Value = "Hammer";
            ws.Cells["L7"].Value = "Hardware";
            ws.Cells["M7"].Value = 1;
            ws.Cells["N7"].Value = 4.2;
            ws.Cells["O7"].Value = new DateTime(2010, 6, 30);

            ws.Cells["K8"].Value = "Saw";
            ws.Cells["L8"].Value = "Hardware";
            ws.Cells["M8"].Value = 4;
            ws.Cells["N8"].Value = 33.12;
            ws.Cells["O8"].Value = new DateTime(2010, 6, 28);

            ws.Cells["K9"].Value = "Screwdriver";
            ws.Cells["L9"].Value = "Hardware";
            ws.Cells["M9"].Value = 1200;
            ws.Cells["N9"].Value = 45.2;
            ws.Cells["O9"].Value = new DateTime(2010, 8, 31);

            ws.Cells["K10"].Value = "Apple";
            ws.Cells["L10"].Value = "Groceries";
            ws.Cells["M10"].Value = 807;
            ws.Cells["N10"].Value = 1.2;
            ws.Cells["O10"].Value = new DateTime(2010, 9, 30);

            ws.Cells["K11"].Value = "Butter";
            ws.Cells["L11"].Value = "Groceries";
            ws.Cells["M11"].Value = 52;
            ws.Cells["N11"].Value = 7.2;
            ws.Cells["O11"].Value = new DateTime(2010, 10, 31);
            ws.Cells["O2:O11"].Style.Numberformat.Format = "yyyy-MM-dd";

            var pt = wsPivot1.PivotTables.Add(wsPivot1.Cells["A1"], ws.Cells["K1:N11"], "Pivottable1");

            pt.Compact = true;
            pt.CompactData = true;

            pt.GrandTotalCaption = "Total amount";
            pt.RowFields.Add(pt.Fields[1]);
            pt.RowFields.Add(pt.Fields[0]);
            pt.DataFields.Add(pt.Fields[3]);
            pt.DataFields.Add(pt.Fields[2]);
            pt.DataFields[0].Function = DataFieldFunctions.Product;
            pt.DataOnRows = false;



            _pck.Workbook.CreateVBAProject();

            var sb = new StringBuilder();

            sb.AppendLine("Private Sub Workbook_Open()");
            sb.AppendLine("    Range(\"A1\").Select");
            sb.AppendLine("    ActiveSheet.PivotTables(\"Pivottable1\").PivotFields(\"Category\").PivotItems(\"Hardware\").ShowDetail = False");
            sb.AppendLine("End Sub");

            _pck.Workbook.CodeModule.Code = sb.ToString();

            _pck.Save();


        }

    }
}

答案 1 :(得分:-1)

使用EPPlus你可以尝试类似的东西(取自this SO post):

(from pf in pivot.Fields
 select pf).ToList().ForEach(f =>
 {
     f.Compact = false;
     f.Outline = false;
 });

或者更简单地说,不是像以下那样的工作?

pvtTable.Compact = False
pvtTable.CompactData = False
pvtTable.Outline = False
pvtTable.OutlineData = False
pvtTable.ShowDrill = True

另请参阅this SO post详细介绍一种不错的逆向工程方法,以了解excel如何实现“事物”。

P.S。无法帮助你解决其他'也'的问题。