我无法使用ExcelDNA在C#中使用Excel编写REST响应。它可以很好地读取特定列中的值。
xlCelli,1= "TEST";// System.Runtime.InteropServices.COMException:‘System.Runtime.InteropServices.COMException异常: 0x800A03EC’currentSheet.Cellsi,2 2.Value = "TEST";//同一问题
Excel读取代码--
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)则不是这样。
有人能帮忙吗。
发布于 2022-11-09 20:05:28
从单元格函数中修改工作表值基本上是个坏主意。执行单元函数时,Excel通常很忙.因此,修改某个单元格值的尝试可能失败。
你的代码是正确的。您所缺少的唯一东西是ExcelFunction属性的ExcelFunction属性。属性改变了函数与工作表的交互方式。有关更多细节,请参见这里。代码示例如下所示。
[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准备就绪时,它将安全地运行您的委托。有关该函数的更多详细信息,请参见这里。下面是一个例子。
[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";
}
完整的代码如下:
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";
}
}
}
https://stackoverflow.com/questions/73024554
复制相似问题