简体   繁体   English

如何使用Excel-DNA创建一个新的工作表并用数据行填充它?

[英]How do I create a new worksheet and populate it with rows of data using Excel-DNA?

I have c# code behind my Excel-dna addin which is successfully downloading data from a service. 我的Excel-dna插件背后有c#代码,该代码已成功从服务下载数据。 I have created a ribbon in Excel-dna with a button which triggers the download, and now I want to display the data in a new worksheet. 我在Excel-dna中创建了一个带按钮的功能区,该按钮触发了下载,现在我想在新工作表中显示数据。 How do I create a worksheet and add rows? 如何创建工作表并添加行?

I tried calling xlcWorkbookInsert from my c# code using: 我尝试使用以下方法从C#代码中调用xlcWorkbookInsert

  ExcelReference newSheet = (ExcelReference)XlCall.Excel(XlCall.xlcWorkbookInsert, 1);

but I always get a ExcelDna.Integration.XlCallException exception. 但是我总是得到一个ExcelDna.Integration.XlCallException异常。 Is this the correct approach, or is there a simpler way to go about doing this? 这是正确的方法,还是有更简单的方法来执行此操作?

I also tried pasting an object[,] of data to an existing sheet: 我还尝试将数据的object[,]粘贴到现有工作表中:

   ExcelReference sheet1 = (ExcelReference)XlCall.Excel(XlCall.xlSheetId, "Sheet1");

   ExcelReference myTargetPasteArea = new ExcelReference(1, 1, 2, 10, sheet1.SheetId);

   myTargetPasteArea.SetValue(result);

There are no errors this time, but nothing happens (although I can see the code being executed when I step through in debug). 这次没有错误,但是什么也没有发生(尽管我在调试中逐步看到了正在执行的代码)。

Your code is calling to Excel via the C API (that's how the XlCall.Excel(...) and ExcelReference stuff in Excel-DNA works). 您的代码通过C API调用Excel(这就是Excel-DNA中 XlCall.Excel(...)和ExcelReference的工作方式)。 But you can't call the C API directly from your ribbon event handler. 但是您不能直接从功能区事件处理程序中调用C API。 You have two options: 您有两种选择:

  • Make a detour via a macro. 通过宏绕行。 This is easy if you change your ribbon xml code: 如果您更改功能区xml代码,这很容易:

    <button onAction="RunTagMacro" tag="MyMacro" /> <button onAction =“ RunTagMacro” tag =“ MyMacro” />

and then define a macro: 然后定义一个宏:

public static void MyMacro()
{
    // ... do your work here ....
}

You can also call the macro yourself from the event handler - the RunTagMacro internally just calls 您还可以自己从事件处理程序中调用宏RunTagMacro内部仅调用

object app = ExcelDnaUtil.Application;
app.GetType().InvokeMember("Run", BindingFlags.InvokeMethod, 
    null, app, new object[] { control.Tag }, new CultureInfo(1033));
  • Another option is to change your interaction code to use the COM API. 另一个选择是更改您的交互代码以使用COM API。 For this you'll need to use the 'dynamic' support in .NET 4, or reference an interop assembly - either the version-specific Primary Interop Assemblies for Office, or some version-independent interop assemblies like NetOffice . 为此,您需要使用.NET 4中的“动态”支持,或引用互操作程序集-Office特定于版本的主互操作程序集,或某些独立于版本的互操作程序集,例如NetOffice
XlCall.Excel(XlCall.xlcWorkbookInsert, 1);

returns a bool : true - success, false - failure 返回booltrue成功, false失败

So casting it to ExcelReference is the cause of the exception . 因此,将其强制转换为ExcelReference是导致exception的原因。

You may need an xlcNew before that xlcWorkbookInsert. 在该xlcWorkbookInsert之前,您可能需要一个xlcNew。 Take a look in the Excel-Dna source at the GetApplication method in Excel.cs. 在Excel-Dna源代码中的Excel.cs中查看GetApplication方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM