且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用c#写入excel中的单元格

更新时间:2023-02-13 17:33:37

你可能会创建一个新的Excel应用程序,而不是连接到活动的Excel应用程序。



使用 GetActiveObject 以获取对Excel应用程序对象的引用。


I created a visual studio excel workbook project which contains ThisWorkbook class and I then included a functions class so that I can create my own excel function. I am trying to write a value to a cell using c# through the use of the function. I can write to the excel cell through the function class but only by creating a new application/workbook. So everytime I use this excel function, it will open new instance of excel. Is there a way I can write to a cell in the current excel workbook that is already opened?

You probably create a new Excel application, instead of connecting to the active Excel application.

Use the GetActiveObject to get your reference to the Excel application object.