gpt4 book ai didi

c# - 为什么创建数据透视表(Excel 互操作)的尝试会失败?

转载 作者:行者123 更新时间:2023-12-04 20:11:41 27 4
gpt4 key购买 nike

根据我找到的代码 here ,我正在尝试像这样创建数据透视表:

var range = _xlSheet.Range[_xlSheet.Cells[6, 1],
_xlSheet.Cells[6, _grandTotalsColumn]];
ListObject tbl = WriteToExcelTable(_xlSheet, "tbl", "A6");
var pch = _xlBook.PivotCaches();

pch.Add(XlPivotTableSourceType.xlDatabase, "Produce Usage by Month!A6:F94")
.CreatePivotTable(tbl, "PivTab1", Type.Missing, Type.Missing);
. . .

public ListObject WriteToExcelTable(Worksheet WSheet, string TableName, string CellStr = "A1", bool ClearSheetContent = false)
{
Range range;

if (ClearSheetContent)
WSheet.Cells.ClearContents(); // clear sheet content

// get upper left corner of range defined by CellStr
range = (Range)WSheet.get_Range(CellStr).Cells[1, 1];

// Write table to range [ - don't have the helperfunc code commented this out ]
//HelperFunc.WriteTableToExcelSheet(WSheet, this._tbl, range.Address);

// derive range for table, +1 row for table header
// [doesn't compile, so commented out] rang
//range.get_Resize(this.RowCount + 1, this.ColumnCount);
range = range.get_Resize(102, 5); // faking it to see what happens

// add ListObject to sheet
ListObject tbl = WSheet.ListObjects.AddEx(
XlListObjectSourceType.xlSrcRange,
range,
XlListObjectHasHeaders: XlYesNoGuess.xlYes);

// set name of excel table
tbl.Name = TableName;

// return excel table (ListObject)
return tbl;
}

...但它在这一行崩溃:
pch.Add(XlPivotTableSourceType.xlDatabase, "Produce Usage by Month!A6:F94")
.CreatePivotTable(tbl, "PivTab1", Type.Missing,
Type.Missing);

...和:
System.ArgumentException was caught
HResult=-2147024809
Message=The parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARG))
Source=""
StackTrace:
at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)
at Microsoft.Office.Interop.Excel.PivotCache.CreatePivotTable(Object TableDestination, Object TableName, Object ReadData, Object DefaultVersion)
at ReportRunner.ProduceUsage.ProduceUsageRpt.AddPivotTable() in c:\Projects\ReportRunner\ReportRunner\ProduceUsage\ProduceUsageRpt.cs:line 3312
at ReportRunner.ProduceUsage.ProduceUsageRpt.GenerateProduceUsageRpt() in c:\Projects\ReportRunner\ReportRunner\ProduceUsage\ProduceUsageRpt.cs:line 180
InnerException:

我的方法有什么问题?请注意,我必须从借用的方法 (WriteToExcelTable()) 中注释掉两行,因为它们不会为我编译。

更新

我也尝试过这个替代方案:
private void AddPivotTable()
{
int rowIdx = 42; // try 42 for now; adjust as/if necessary
var pch = _xlBook.PivotCaches();
pch.Add(XlPivotTableSourceType.xlDatabase, "PivotData!A6:D" + rowIdx)
.CreatePivotTable(_xlSheet.Cells[6, 1], "PivTab1", Type.Missing, Type.Missing);
. . .

...在显示的最后一行崩溃:
System.Runtime.InteropServices.COMException was caught
HelpLink=C:\Program Files (x86)\Microsoft Office\Office12\1033\XLMAIN11.CHM
HResult=-2146827284
Message=Cannot open PivotTable source file 'C:\Users\cshannon\Documents\PivotData'.
Source=Microsoft Office Excel
ErrorCode=-2146827284
StackTrace:
at System.Dynamic.ComRuntimeHelpers.CheckThrowException(Int32 hresult, ExcepInfo& excepInfo, UInt32 argErr, String message)
at CallSite.Target(Closure , CallSite , ComObject , Object , String , Object , Object )
at System.Dynamic.UpdateDelegates.UpdateAndExecute5[T0,T1,T2,T3,T4,TRet](CallSite site, T0 arg0, T1 arg1, T2 arg2, T3 arg3, T4 arg4)
at CallSite.Target(Closure , CallSite , PivotCache , Object , String , Object , Object )
at System.Dynamic.UpdateDelegates.UpdateAndExecuteVoid5[T0,T1,T2,T3,T4](CallSite site, T0 arg0, T1 arg1, T2 arg2, T3 arg3, T4 arg4)
at ReportRunner.ProduceUsage.ProduceUsageRpt.AddPivotTable() in c:\Projects\ReportRunner\ReportRunner\ProduceUsage\ProduceUsageRpt.cs:line 3172
at ReportRunner.ProduceUsage.ProduceUsageRpt.GenerateProduceUsageRpt() in c:\Projects\ReportRunner\ReportRunner\ProduceUsage\ProduceUsageRpt.cs:line 180
InnerException:

更新 2

即使在删除“PivotData!”之后,它显然是指一个名为的文件,从中读取数据,因此该行现在是:
pch.Add(XlPivotTableSourceType.xlDatabase, "A6:D" + rowIdx)

...它仍然崩溃,现在:
System.Runtime.InteropServices.COMException was caught
HelpLink=C:\Program Files (x86)\Microsoft Office\Office12\1033\XLMAIN11.CHM
HResult=-2146827284
Message=Cannot change part of a merged cell.
Source=Microsoft Office Excel
ErrorCode=-2146827284
StackTrace:
at System.Dynamic.ComRuntimeHelpers.CheckThrowException(Int32 hresult, ExcepInfo& excepInfo, UInt32 argErr, String message)
at CallSite.Target(Closure , CallSite , ComObject , Object , String , Object , Object )
at System.Dynamic.UpdateDelegates.UpdateAndExecute5[T0,T1,T2,T3,T4,TRet](CallSite site, T0 arg0, T1 arg1, T2 arg2, T3 arg3, T4 arg4)
at CallSite.Target(Closure , CallSite , PivotCache , Object , String , Object , Object )
at System.Dynamic.UpdateDelegates.UpdateAndExecuteVoid5[T0,T1,T2,T3,T4](CallSite site, T0 arg0, T1 arg1, T2 arg2, T3 arg3, T4 arg4)
at ReportRunner.ProduceUsage.ProduceUsageRpt.AddPivotTable() in c:\Projects\ReportRunner\ReportRunner\ProduceUsage\ProduceUsageRpt.cs:line 3174
at ReportRunner.ProduceUsage.ProduceUsageRpt.GenerateProduceUsageRpt() in c:\Projects\ReportRunner\ReportRunner\ProduceUsage\ProduceUsageRpt.cs:line 180
InnerException:

更新 3

因为异常是指合并单元格的问题,所以我添加了这个:
var pivotData = _xlSheet.Range["A6:D42"];
pivotData.UnMerge();

...但它仍然失败(现在与“数据透视表类的数据透视字段方法失败异常来源:Microsoft Office Excel
异常 StackTrace:在 System.RuntimeType.ForwardCallToInvokeMember(字符串成员名称,BindingFlags 标志,对象目标,Int32 [] aWrapperTypes,MessageData 和 msgData)“)。

注意:我收到“您要替换 [Book3] 按月生产使用情况中目标单元格的内容吗?” (两次)在它失败之前

最佳答案

如果不查看工作簿的具体细节,就很难诊断您列出的一些问题。生成数据透视表的基本方法是:

Range sourceData = _xlBook.Worksheets["Produce Usage by Month"].Range["A6:F94"];

PivotCache pc = pch.Create(XlPivotTableSourceType.xlDatabase, sourceData);
PivotTable pvt = pc.CreatePivotTable(sheet2.Range["A1"], "SummaryPivot");

pvt.PivotFields("Col1").Orientation = XlPivotFieldOrientation.xlRowField;
pvt.PivotFields("Col2").Orientation = XlPivotFieldOrientation.xlColumnField;
pvt.AddDataField(pvt.PivotFields("Col3"), "Sum of Col3", XlConsolidationFunction.xlSum);

看起来您的代码对某些对象并没有真正明确。例如 Produce Usage by Month!A6:F94对 Excel 意味着某些东西(并且可能由 VBA 解释),但我认为互操作库需要更明确的对象声明。在强类型中,这将是我为 sourceData 列出的内容。目的。 sheet2因此是 Excel.Worksheet目的。

关于一些警告,例如覆盖警告,如果您更改 DisplayAlerts Excel 应用程序对象的属性为 false,它将处理其中的许多。

关于c# - 为什么创建数据透视表(Excel 互操作)的尝试会失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39883962/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com