为工作表var分配工作表时,为什么会出现类型不匹配?

时间:2016-11-15 16:37:45

标签: worksheet type-mismatch aspose-cells

我正在尝试将Aspose Cells for .NET合并到这个使用Excel Interop的类中(而不是重写整个使用Aspose Cells的东西,我现在正在尝试,我想至少从混合开始方法)。

问题是我得到了#34;类型不匹配"此代码的异常:

private Workbook _xlBook;
. . .
Worksheet asposePT = _xlBook.Worksheets[_xlBook.Worksheets.Add()];

我的项目引用了Aspose.Cells,版本16.11.0.0(运行时版本v4.0.30319)

我在类文件中有这些用法:

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Threading;
using System.Windows.Forms;
using ReportRunner.SharedCode;
using Microsoft.Office.Interop.Excel;
using System.Runtime.InteropServices;
using System.Text;
using Excel = Microsoft.Office.Interop.Excel;

这是一个例外细节:

System.Runtime.InteropServices.COMException was caught
  HResult=-2147352571
  Message=Type mismatch. (Exception from HRESULT: 0x80020005 (DISP_E_TYPEMISMATCH))
  Source=System.Dynamic
  ErrorCode=-2147352571
  StackTrace:
       at System.Dynamic.ComRuntimeHelpers.CheckThrowException(Int32 hresult, ExcepInfo& excepInfo, UInt32 argErr, String 
message)
       at CallSite.Target(Closure , CallSite , ComObject , Object )
       at System.Dynamic.UpdateDelegates.UpdateAndExecute2[T0,T1,TRet](CallSite site, T0 arg0, T1 arg1)
       at CallSite.Target(Closure , CallSite , Sheets , Object )
       at System.Dynamic.UpdateDelegates.UpdateAndExecute2[T0,T1,TRet](CallSite site, T0 arg0, T1 arg1)
       at ReportRunner.ProduceUsage.ProduceUsageRpt.PopulatePivotTableSheetAsposeCells() in c:\Projects\ReportRunner - 
Private Build\ReportRunner\ProduceUsage\ProduceUsageRpt.cs:line 1550
       at ReportRunner.ProduceUsage.ProduceUsageRpt.GenerateProduceUsageRpt() in c:\Projects\ReportRunner - Private Build
\ReportRunner\ProduceUsage\ProduceUsageRpt.cs:line 167
  InnerException: 
ProduceUsageRpt.cs:line 1550 is:
Worksheet asposePT = _xlBook.Worksheets[_xlBook.Worksheets.Add()];

我需要使用Aspose"或类似的东西?我是否需要将Worksheet显式声明为Aspose工作表(而不是Excel Interop),或者是什么?

更新

即使我改变了代码:

Worksheet asposePT = _xlBook.Worksheets[_xlBook.Worksheets.Add()];
asposePT.Name = "AsposePivotTable";
Aspose.Cells.Pivot.PivotTableCollection pivotTables = asposePT.PivotTables();

...对此(不是在最后一行末尾删除了parens):

Aspose.Cells.Worksheet asposePT = _xlBook.Worksheets[_xlBook.Worksheets.Add()];
asposePT.Name = "AsposePivotTable";
Aspose.Cells.Pivot.PivotTableCollection pivotTables = asposePT.PivotTables;

......我仍然得到同样的例外。

1 个答案:

答案 0 :(得分:2)

嗯,Aspose.Cells API(独立的.NET库)和MS Excel Interop(Office Automation)API在很多方面都有所不同,它们都有不同的架构,所以你不能只解析有关其API的对象b / w他们。我认为您应该只使用Aspose.Cells API来实例化工作簿(电子表格),在工作表中添加/更新或操作数据等,最后保存Excel文件。我猜您的基础_xlBook对象可能是您可能已经实例化的MS Excel Interop的罪魁祸首。的API。

我是Aspose的支持开发人员/传播者。