为什么我的数据透视表字段名称被视为无效 and/or 我的数据被视为未组织为带有标签列的列表?

Why is my PivotTable field name considered to be invalid and/or my data viewed as not organized as a list with labeled columns?

注意:我有一个相关的问题here,但是已经完全更改了代码并且仍然得到相同的错误信息,即“The PivotTable field name is无效。要创建 PivotTable 报表,您必须使用组织为带有标签列的列表的数据。如果您要更改 PivotTable 字段的名称,则必须键入新名称对于字段。"

正如您在下面的代码中看到的,Pivot Table 字段正在被命名(首先是 "Description",然后是 "Months"):

private void AddPivotTable()
{
    var pch = _xlBook.PivotCaches();
    pch.Add(XlPivotTableSourceType.xlDatabase, "A6:F94")
        .CreatePivotTable(_xlSheet.Cells[6, 1], "PivTab1", Type.Missing, Type.Missing);

    var pvt = _xlSheet.PivotTables("PivTab1") as PivotTable;
    if (pvt != null)
    {
        pvt.MergeLabels = true;
        pvt.ErrorString = "";
        pvt.DisplayErrorString = true;

        var fld = ((PivotField)pvt.PivotFields("Description"));
        fld.Orientation = XlPivotFieldOrientation.xlRowField;

        fld = ((PivotField)pvt.PivotFields("Month"));
        fld.Orientation = XlPivotFieldOrientation.xlColumnField;
        fld.NumberFormat = "MMM yy";
        fld.DataRange.Interior.Color = ColorTranslator.ToOle(Color.LightBlue);
    }
}

我认为问题可能在于 "Description" 行字段和 "Month" 列字段使用了相同的 PivotField ("fld"),所以我重构了代码为此:

private void AddPivotTable()
{
    var pch = _xlBook.PivotCaches();
    pch.Add(XlPivotTableSourceType.xlDatabase, "A6:F94")
        .CreatePivotTable(_xlSheet.Cells[6, 1], "PivTab1", Type.Missing, Type.Missing);

    var pvt = _xlSheet.PivotTables("PivTab1") as PivotTable;
    if (pvt != null)
    {
        pvt.MergeLabels = true;
        pvt.ErrorString = "";
        pvt.DisplayErrorString = true;

        var descriptionPivotField = (PivotField)pvt.PivotFields("Description");
        descriptionPivotField.Orientation = XlPivotFieldOrientation.xlRowField;

        var monthPivotField = (PivotField)pvt.PivotFields("Month");
        monthPivotField.Orientation = XlPivotFieldOrientation.xlColumnField;
        monthPivotField.NumberFormat = "MMM yy";
        monthPivotField.DataRange.Interior.Color = ColorTranslator.ToOle(Color.LightBlue);
    }
}

...但这没什么区别;它仍然告诉我 PivotTable 字段名称无效 - 但哪个是 "Description" 或 "Month"?两个都?为什么? ISTM 错误消息具有误导性或名称要求很奇怪。

当代码运行并生成 sheet 时,它应该看起来像这样,带有 "Month" 和 "Description" PivotTable 字段:

这是我生成的 sheet 目前的样子,但是没有 PivotTable 或其字段(这是 PivotTable 代码被注释掉时生成的) :

问题的可能真正原因可能与错误消息的第二部分有关,即“...您必须使用组织为带有标签列的列表的数据。" 但正如您在上面的屏幕截图中看到的那样, 数据从 A6(这是 header 行的第一列)开始并且确实使用测试数据继续 F 列和第 94 行:

(实际上,数据在总计行接管之前继续到第 1570 行)。

我收到的完整错误信息是:

PivotTable 字段名称无效。要创建 PivotTable 报表,您必须使用组织为带有标签列的列表的数据。如果要更改 PivotTable 字段的名称,则必须为该字段键入一个新名称。 异常来源:Microsoft Office Excel 异常 StackTrace: 在 System.Dynamic.ComRuntimeHelpers.CheckThrowException(Int32 hresult, ExcepInfo& excepInfo,UInt32 argErr,字符串消息) 在 CallSite.Target(闭包,CallSite,ComObject,Object,字符串,Object,Object) 在 System.Dynamic.UpdateDelegates.UpdateAndExecute5[T0,T1,T2,T3,T4,TRet](CallSite 站点,T0 arg0,T1 arg1,T2 arg2,T3 arg3,T4 arg4) 在 CallSite.Target(闭包,CallSite,PivotCache,Object,字符串,Object,Object) 在 System.Dynamic.UpdateDelegates.UpdateAndExecuteVoid5[T0,T1,T2,T3,T4](CallSite 站点,T0 arg0,T1 arg1, T2 arg2,T3 arg3,T4 arg4) 在 ReportRunner.ProduceUsage.ProduceUsageRpt.AddPivotTable() 在 c:\Projects\ReportRunner\ReportRunner \ProduceUsage\ProduceUsageRpt.cs:第 3195 行 在 ReportRunner.ProduceUsage.ProduceUsageRpt.GenerateProduceUsageRpt() 在 c:\Projects\ReportRunner \ReportRunner\ProduceUsage\ProduceUsageRpt.cs:第 192 行

第 3195 行是这个:

pch.Add(XlPivotTableSourceType.xlDatabase, "A6:F94")
                .CreatePivotTable(_xlSheet.Cells[6, 1], "PivTab1", Type.Missing, Type.Missing);

第 192 行是对 AddPivotTable()

的调用

更新

cyboashu的回答听起来很有希望,但我不知道他例子中的"Sheet1"是sheet的名字object还是sheet的名字。

这是我的代码:

private Worksheet _xlSheet; 

private static readonly String ProduceUsageByMonthSheetName = "Produce Usage by Month";
_xlSheet.Name = ProduceUsageByMonthSheetName;

...我尝试了所有我能想到的排列方式:

//pch.Add(XlPivotTableSourceType.xlDatabase, _xlSheet.Range("A6:F94")) //"PivotData!A1:H" + rowIdx)
//pch.Add(XlPivotTableSourceType.xlDatabase, _xlSheet!"A6:F94") 
//pch.Add(XlPivotTableSourceType.xlDatabase, ProduceUsageByMonthSheetName!"A6:F94")
//pch.Add(XlPivotTableSourceType.xlDatabase, ProduceUsageByMonthSheetName.Range("A6:F94")) 
//pch.Add(XlPivotTableSourceType.xlDatabase, "Produce Usage by Month"!"A6:F94")
pch.Add(XlPivotTableSourceType.xlDatabase, "Produce Usage by Month".Range("A6:F94"))
    .CreatePivotTable(_xlSheet.Cells[6, 1], "PivTab1", Type.Missing, Type.Missing);

...但他们都无法编译:

A7:A10 合并了吗?您不能在数据透视 table 数据

中合并单元格

您没有提供完整的限定范围地址。

.Add(XlPivotTableSourceType.xlDatabase, "A6:F94")
                .CreatePivotTable(_xlSheet.Cells[6, 1], "PivTab1", Type.Missing, Type.Missing);

更改 "A6:F94" 以包含 sheet 名称,例如 Sheet1!"A6:F94"Sheet1.Range("A6:F94"")

它将纠正 Excel 中的 error.Error 消息 - 对象模型有点混乱。

更新

你正在混合它。如果 sheet 名称是 Produce Usage by Month" 那么 而不是 "Produce Usage by Month".Range("A6:F94")) 使用 "Produce Usage by Month!A6:F94"

提示;在 Excel 中录制一个宏以查看语法的工作原理。