C# 代码转换-MFC++;到VSTO Excel加载项(在C中)#

C# 代码转换-MFC++;到VSTO Excel加载项(在C中)#,c#,excel,vsto,add-in,excel-dna,C#,Excel,Vsto,Add In,Excel Dna,我将MFC C++ +COADIN转换成VSTO Excel插件 C#.NET4.0。其中有很多代码引用了C API。 这里有一个例子。我可能会使用Excel DNA来简化我的工作 转换任务 仅供参考:xlr是XLSDK中的一个类 //allocate local storage for the reference m_pControllerReference = new xlr( controllerRange ) ; //get the name of the book and sheet

我将MFC C++ +COADIN转换成VSTO Excel插件 C#.NET4.0。其中有很多代码引用了C API。 这里有一个例子。我可能会使用Excel DNA来简化我的工作 转换任务

仅供参考:xlr是XLSDK中的一个类

//allocate local storage for the reference
m_pControllerReference = new xlr( controllerRange ) ;

//get the name of the book and sheet with the reference
   g->Excel.fn( xlSheetNm, *m_pControllerReference ) ; 

   if ( g->Excel.resultXltype() != xltypeErr )
   {
       m_referenceSheetName = g->Excel.resultCString() ;

//    and get the sheet ID
       g->Excel.fn( xlSheetId, xl(m_referenceSheetName) ) ; 

       if ( g->Excel.resultXltype() != xltypeErr )
       {
           m_controllerSheetId = g->Excel.resultSheetId() ;

           xlr theRange( m_pControllerReference->GetLPXLOPER(),
0, TRUE ) ;

           if(theRange.nRows() >6)

           ........etc
这个是这样转换的吗

          m_pControllerReference = (Excel.Range)controllerRange;

          m_referenceSheetName =
(string)XlCall.Excel(XlCall.xlSheetNm, m_pControllerReference );

          m_controllerSheetId =  XlCall.Excel(XlCall.xlSheetId,
m_referenceSheetName);    // and get the sheet ID

          //how to convert this ?
          //xlr theRange( m_pControllerReference->GetLPXLOPER(),
0, TRUE ) ;
或者有没有更好的转换方式而无需求助于第三方实用程序?我可以在VSTO中做所有事情吗?有C API到C#转换的图表吗?

“Everyting”是一个大词,但在VSTO中可以做很多事情:-)

您的C#代码可能可以工作,但这种语法不是您通常在VSTO中使用的语法。您可以这样做:

Range controllerRange = <your code here> //... get your range
string referenceSheetName = controllerRange.Worksheet.Name;
// I'm not aware of any sheet ID in VSTO
if (controllerRange.Rows.Count > 6)
... etc...
范围控制器错误=/。。。去你的靶场
字符串referenceSheetName=controllerRange.Worksheet.Name;
//我不知道VSTO中有任何工作表ID
如果(controllerage.Rows.Count>6)
... 等
如您所见,您可以直接在范围对象上工作,无需使用引用并将引用用作函数的参数

< P>确实应该使您的Excel C++到.NET转换变得更容易。 你应该小心尝试混合VSTO和Excel DNA。它们不能在同一个外接程序中愉快地生活在一起,因此您应该将所有内容都基于Excel DNA(它允许您访问C API和COM接口),或者创建两个单独的外接程序(VSTO有一些功能区和其他方便的高级包装)

要从ExcelDNA访问C API,您可以使用XlCall类,正如您所注意到的,以及ExcelReference类,该类包含引用类型XLOPER的信息。使用Excel DNA,您永远不必显式地处理XLOper,所有类型转换都会在进行XlCall.Excel(…)调用时自动完成

您不应该将C API帮助程序类型ExcelReference与COM类型范围混淆。您可以来回转换,但它们不能互换。对于C API调用,您需要ExcelReference类型

关于您的示例,不清楚ControlErrange是什么,但我猜类型xlr type相当于ExcelDNA的ExcelReference类型,而不是您正在使用的COM范围类型(如Excel.Range)。下面是一篇关于在ExcelReference和Range之间转换的帖子:

当您有ExcelReference时,您的呼叫是正确的。因此,这应该是可行的:

  m_pControllerReference = new ExcelReference(0,0,0,0, "Sheet1"); // Cell A1
  m_referenceSheetName = (string)XlCall.Excel(XlCall.xlSheetNm, m_pControllerReference ); 
  m_controllerSheetId =  XlCall.Excel(XlCall.xlSheetId, m_referenceSheetName);
  // or just: 
  m_controllerSheetId =  m_pControllerReference.SheetId;
现在我不知道你的最后一行是做什么的-它似乎创建了另一个卡侬对象。
ExcelReference具有属性RowFirst和RowLast,用于检查它有多少行。

类xlr派生自xl0,后者派生自CObject。卡侬(LPXLOPER xltypeRef_LPXLOPER,int-idx,BOOL-readData=FALSE);//来自不相交引用的单范围引用(通过基于零的索引)//可选地读取inOK中的数据,然后它似乎包装了xltypeRef类型的XLOPER,因此ExcelReference将是Excel DNA的对应项。