简体   繁体   English

如何使用Excel Sheet包含C#代码中的函数?

[英]How to use Excel Sheet contains functions in C# code?

I have an excel file that contains some methods written in VB I think. 我有一个excel文件,其中包含一些用VB编写的方法。 I want to know any way makes me use these methods in C# code. 我想知道任何方式让我在C#代码中使用这些方法。 I tried to make a connection with the file using ( System.Data.Odbc.OdbcConnection ), and then use the UPDATE statement by using Command , but error toke place. 我尝试使用( System.Data.Odbc.OdbcConnection )与该文件建立连接,然后使用Command使用UPDATE语句,但错误地址放置。 So does any one know how to do this? 那么有谁知道如何做到这一点?

I uploaded the file on : Download The Excel File 我上传了该文件: 下载Excel文件

I want to change C20 and C21 , then get the data from C22:C29 thanks 我想更改C20C21 ,然后从C22:C29获取数据,谢谢

What you are looking at is likely not VB.NET, but VBA (Visual Basic for Applications). 您正在看的可能不是VB.NET,而是VBA(Visual Basic for Applications)。 VBA is an ancestor of VB.NET, and the language that is used for Excel macros. VBA是VB.NET的祖先,也是用于Excel宏的语言。 The bad news for you is, VBA isn't a .NET language, and you can't use C# for macros "attached" to a workbook. 对您而言,坏消息是,VBA不是.NET语言,并且您不能将C#用于“附加”到工作簿的宏。

If you are really motivated to use C#, your best option is to look into ExcelDNA , which among other things allows you to call .NET dlls from Excel. 如果您真的有动力使用C#,那么最好的选择是研究ExcelDNA ,其中包括允许您从Excel调用.NET dll。

However, I would really question the need to convert to C#, given the problem you describe. 但是,鉴于您描述的问题,我真的会质疑转换为C#的必要性。 VBA is the "natural" language for lightweight Excel automation, and is fairly understandable. VBA是轻量级Excel自动化的“自然”语言,并且相当容易理解。 If you have existing code, just modify it. 如果您有现有代码,只需修改它。 And one of the nice features of Excel VBA is the macro recorder, which allows you to record your interactions with Excel as VBA code, which is a great way to figure out how things are used in the object model. Excel VBA的一个不错的功能是宏录制器,它允许您将与Excel的交互记录为VBA代码,这是一种很好的方法来确定对象模型中的事物的使用方式。

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

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