使用c#写入excel单元格
我创建了一个 Visual Studio Excel 工作簿项目,其中包含 ThisWorkbook 类,然后包含一个函数类,以便我可以创建自己的 Excel 函数。我正在尝试通过使用该函数使用 C# 将值写入单元格。我可以通过函数类写入 Excel 单元格,但只能通过创建新的应用程序/工作簿。所以每次我使用这个Excel函数时,它都会打开一个新的Excel实例。有没有办法可以写入当前 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看,我尝试过,这段代码有效:
所以如您所见,您可以访问工作表并使用 ws.Cells 来操作单元格值。
这里有很多示例:
了解 Excel 对象.NET 开发人员视角的模型
Look, I tried and this code works:
so as you see you can access the Worksheet and use ws.Cells to manipulate cell values.
there are plenty of examples here:
Understanding the Excel Object Model from a .NET Developer's Perspective
您可能会创建一个新的 Excel 应用程序,而不是连接到活动的 Excel 应用程序。
使用 GetActiveObject 获取参考到 Excel 应用程序对象。
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.