【问题标题】:VSTO Excel 2007 PivotTable, having a PivotField in more than one columnVSTO Excel 2007 数据透视表,在多列中具有数据透视字段
【发布时间】:2008-09-19 21:03:57
【问题描述】:

我在 Excel 2007 中使用 VSTO 来动态生成数据透视表和数据透视图。 当我需要在多个列中使用 PivotField 时,我遇到了问题。

为了实现这一点,我在 Excel 中创建了一个数据透视表并将其属性序列化为一个 XML 文档,然后我用它来重建数据透视表。

即:作为一个值和一个列

在 Excel 中构建数据透视表时可以做到这一点。找到使用 C# 的方法了吗?

Creating a PivotTable Programmatically

【问题讨论】:

    标签: c# .net-3.5 vsto excel-2007


    【解决方案1】:

    如果您将计算字段添加到数据透视表,并让公式只是您需要复制的字段的名称,以便您可以使用相同的字段两次,则计算字段必须是值字段。

    也许您可以通过编程方式完成此操作。

    【讨论】:

      【解决方案2】:

      一旦你有了你的数据集,你就可以将它转换成一个对象[,] 并将它插入到一个 Excel 文档中。然后您可以将文档保存到磁盘并将其流式传输给用户。

      for (int cIndex = 1; cIndex < 1 + columns; cIndex++)
          sheet.Cells.set_Item(4, cIndex, data.Columns[cIndex - 1].Caption);
      if (rows > 0)
      {
      
          //select the range where the data will be pasted
          Range r = sheet.get_Range(sheet.Cells[5, 1], sheet.Cells[5 + (rows - 1), columns]);
      
          //Convert the datatable to an object array
          object[,] workingValues = new object[rows, columns];
      
          for (int rIndex = 0; rIndex < rows; rIndex++)
              for (int cIndex = 0; cIndex < columns; cIndex++)
                  workingValues[rIndex, cIndex] = data.Rows[rIndex][cIndex].ToString();
      
          r.Value2 = workingValues;
      }
      

      【讨论】:

        猜你喜欢
        • 2010-11-28
        • 2011-11-17
        • 1970-01-01
        • 1970-01-01
        • 2011-09-07
        • 1970-01-01
        • 2021-12-06
        • 2016-04-29
        • 1970-01-01
        相关资源
        最近更新 更多