我在我的Excel-dna插件后面有c#代码,它可以成功地从服务下载数据。我已经在Excel-dna中创建了一个功能区,其中有一个按钮可以触发下载,现在我想在一个新的工作表中显示数据。如何创建工作表并添加行?
我尝试使用以下命令从我的c#代码调用xlcWorkbookInsert:
ExcelReference newSheet = (ExcelReference)XlCall.Excel(XlCall.xlcWorkbookInsert, 1);但是我总是得到一个ExcelDna.Integration.XlCallException异常。这是正确的方法吗,还是有一种更简单的方法可以做到这一点?
我还尝试将数据的object[,]粘贴到现有的工作表中:
ExcelReference sheet1 = (ExcelReference)XlCall.Excel(XlCall.xlSheetId, "Sheet1");
ExcelReference myTargetPasteArea = new ExcelReference(1, 1, 2, 10, sheet1.SheetId);
myTargetPasteArea.SetValue(result);这一次没有错误,但没有发生任何事情(尽管我在调试过程中可以看到代码正在执行)。
发布于 2012-06-27 21:31:53
您的代码通过C API调用Excel (这就是XlCall.Excel(...)以及Excel-DNA works中的ExcelReference内容)。但是您不能直接从ribbon事件处理程序调用C API。您有两个选择:
然后定义一个宏:
public static void MyMacro()
{
// ... do your work here ....
}您也可以自己从事件处理程序调用宏-- RunTagMacro内部只是调用
object app = ExcelDnaUtil.Application;
app.GetType().InvokeMember("Run", BindingFlags.InvokeMethod,
null, app, new object[] { control.Tag }, new CultureInfo(1033));这样的独立于版本的互操作程序集
发布于 2015-04-20 23:16:54
XlCall.Excel(XlCall.xlcWorkbookInsert, 1);返回失败:true -成功,false -bool
因此,将其转换为ExcelReference是导致exception的原因。
发布于 2015-01-18 19:26:15
在该xlcWorkbookInsert之前,您可能需要一个xlcNew。查看Excel.cs中GetApplication方法的Excel-Dna源代码。
https://stackoverflow.com/questions/11223641
复制相似问题