首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >无法在Excel中使用ExcelDNA在C#中写入响应

无法在Excel中使用ExcelDNA在C#中写入响应
EN

Stack Overflow用户
提问于 2022-07-18 15:07:16
回答 1查看 64关注 0票数 0

我无法使用ExcelDNA在C#中使用Excel编写REST响应。它可以很好地读取特定列中的值。

xlCelli,1= "TEST";// System.Runtime.InteropServices.COMException:‘System.Runtime.InteropServices.COMException异常: 0x800A03EC’currentSheet.Cellsi,2 2.Value = "TEST";//同一问题

Excel读取代码--

代码语言:javascript
运行
复制
Excel.Range xlCell;
Excel.Application xlapp = (Excel.Application)ExcelDnaUtil.Application;
xlapp.Visible = true;
Worksheet currentSheet;
xlCell = xlapp.ActiveCell;
Excel.Workbook wbook = xlapp.ActiveWorkbook;
currentSheet = wbook.ActiveSheet;

请注意,相同的代码片段在VSTO项目中运行良好(通过带状按钮事件单击),但在.net类库中(对于UDF)则不是这样。

有人能帮忙吗。

EN

回答 1

Stack Overflow用户

发布于 2022-11-09 20:05:28

从单元格函数中修改工作表值基本上是个坏主意。执行单元函数时,Excel通常很忙.因此,修改某个单元格值的尝试可能失败。

你的代码是正确的。您所缺少的唯一东西是ExcelFunction属性的ExcelFunction属性。属性改变了函数与工作表的交互方式。有关更多细节,请参见这里。代码示例如下所示。

代码语言:javascript
运行
复制
[ExcelFunction(IsMacroType = true)]
public static int TestWrite1()
{
    Excel.Range xlCell;
    Excel.Application xlapp = (Excel.Application)ExcelDnaUtil.Application;
    xlapp.Visible = true;
    Worksheet currentSheet;
    xlCell = xlapp.ActiveCell;
    Excel.Workbook wbook = xlapp.ActiveWorkbook;
    currentSheet = wbook.ActiveSheet;

    currentSheet.Cells[1, 1].Value = "Hello, world";

    return 0;
}

或者,您可以使用函数ExcelAsyncUtil.QueueAsMacro.手动排队执行代码。当Excel准备就绪时,它将安全地运行您的委托。有关该函数的更多详细信息,请参见这里。下面是一个例子。

代码语言:javascript
运行
复制
[ExcelFunction]
public static int TestWrite2()
{
    ExcelAsyncUtil.QueueAsMacro(new ExcelAction(() => {
        Excel.Range xlCell;
        Excel.Application xlapp = (Excel.Application)ExcelDnaUtil.Application;
        xlapp.Visible = true;
        Worksheet currentSheet;
        xlCell = xlapp.ActiveCell;
        Excel.Workbook wbook = xlapp.ActiveWorkbook;
        currentSheet = wbook.ActiveSheet;

        currentSheet.Cells[1, 1].Value = "Hello, world";
    }));

    return 0;
}

最后,如果您的目标是将某些值从单元函数返回到活动单元格,则可以正常地返回值。

代码语言:javascript
运行
复制
[ExcelFunction]
public static string TestWrite3()
{
    Excel.Range xlCell;
    Excel.Application xlapp = (Excel.Application)ExcelDnaUtil.Application;
    xlapp.Visible = true;
    Worksheet currentSheet;
    xlCell = xlapp.ActiveCell;
    Excel.Workbook wbook = xlapp.ActiveWorkbook;
    currentSheet = wbook.ActiveSheet;

    /* Insert to the current cell where the cell-function is being executed. */
    return "Hello, world"; 
}

完整的代码如下:

代码语言:javascript
运行
复制
using ExcelDna.Integration;
using Microsoft.Office.Interop.Excel;
using Excel = Microsoft.Office.Interop.Excel;

namespace ClassLibraryExcelDna
{
public class UDF
{
    [ExcelFunction(IsMacroType = true)]
    public static int TestWrite1()
    {
        Excel.Range xlCell;
        Excel.Application xlapp = (Excel.Application)ExcelDnaUtil.Application;
        xlapp.Visible = true;
        Worksheet currentSheet;
        xlCell = xlapp.ActiveCell;
        Excel.Workbook wbook = xlapp.ActiveWorkbook;
        currentSheet = wbook.ActiveSheet;

        currentSheet.Cells[1, 1].Value = "Hello, world";

        return 0;
    }

    [ExcelFunction]
    public static int TestWrite2()
    {
        ExcelAsyncUtil.QueueAsMacro(new ExcelAction(() => {
            Excel.Range xlCell;
            Excel.Application xlapp = (Excel.Application)ExcelDnaUtil.Application;
            xlapp.Visible = true;
            Worksheet currentSheet;
            xlCell = xlapp.ActiveCell;
            Excel.Workbook wbook = xlapp.ActiveWorkbook;
            currentSheet = wbook.ActiveSheet;

            currentSheet.Cells[1, 1].Value = "Hello, world";
        }));

        return 0;
    }

    [ExcelFunction]
    public static string TestWrite3()
    {
        Excel.Range xlCell;
        Excel.Application xlapp = (Excel.Application)ExcelDnaUtil.Application;
        xlapp.Visible = true;
        Worksheet currentSheet;
        xlCell = xlapp.ActiveCell;
        Excel.Workbook wbook = xlapp.ActiveWorkbook;
        currentSheet = wbook.ActiveSheet;

        /* Insert to the current cell where the cell-function is being executed. */
        return "Hello, world"; 
    }
}
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73024554

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档