我怎样才能从MS Excel读取值到.Net表格? [英] How i can read the values from MS Excel to .Net form?

查看:88
本文介绍了我怎样才能从MS Excel读取值到.Net表格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想读取excel工作表中的值,并希望在.net表单上显示数据

请解决我的问题吗?

i want to read the values in excel worksheet and want to display the data on the .net form

plz solve my problem?

推荐答案

这里有一些示例代码,是VB,但很容易转换为C#:
您需要添加对Microsoft.Office.Interop.Excel的引用.
Here''s some sample code, is VB but is easily convertible to C#:
You need to add a reference to Microsoft.Office.Interop.Excel.
Dim oXL As Microsoft.Office.Interop.Excel.Application
Dim oBook As Microsoft.Office.Interop.Excel.Workbook
Dim oSheet As Microsoft.Office.Interop.Excel.Worksheet
Dim vValue As Object

oXL = New Microsoft.Office.Interop.Excel.Application
oBook = oXL.Workbooks.Open(OpenFileDialog1.FileName)
oSheet = oBook.Worksheets("Sheet1")
'Here you have cell value, just build a loop to get the data you want
vValue =oSheet.Cells(1, 1).Value 

myTextBox.Text = vValue


有很多方法可以做到这一点:

1)如果除了读取之外还想对数据进行操作,那么自动化是您的选择;
2)如果您想读取一组数据并且知道数据格式,则建议使用OleDbConnection;
3)在某些情况下,您可以将xls文件视为csv文件.

您没有指定.NET编程语言,所以这里有一些教程:
1)在C#.NET中 [在VB .NET中 [
There are lots of ways to do this:

1) If you want to do operations on the data in addition to reading then automation is your choice;
2) If you want to read a set of data and you know the data format I''d suggest to use a OleDbConnection;
3) In some cases you can treat the xls files as csv files.

You didn''t specify a .NET programming language so here are some tutorials:
1) In C# .NET[^]
2) In VB .NET[^]

In the above tutorials you can find lots of info and examples. :)


而不是使用Microsoft.Office.Interop.Excel参考
(早期绑定"),您也可以使用后期绑定"技术.

这样,您不必担心所有不同的情况
正在/正在安装的计算机上使用/安装的Excel版本
稍后再使用您的应用程序.

只是Google使用后期绑定"创建/打开Excel工作表"
您会发现很多示例.
Instead of using the Microsoft.Office.Interop.Excel reference
(''early binding''), you could also use the ''late binding'' technique.

This way, you don''t have to worry about all the different
versions of Excel being used/installed on the machines going
to use your application later on.

Just Google ''create/open Excel sheet using ''late binding''
and you''ll find numerous examples.


这篇关于我怎样才能从MS Excel读取值到.Net表格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆