使用c#从Excell电子表格处理数据 [英] Process data from Excell spreadsheet using c#

查看:81
本文介绍了使用c#从Excell电子表格处理数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含3个数据表的Excel电子表格。如何从excel电子表格中读取这三个不同表中的数据并保存到数据库中。

我必须使用c#.net

解决方案

正如digimanus所说.. ODBC / OLEDB连接是关键。



假设您有一个包含三个WorkSheets的工作簿,即Sheet1,Sheet2和Sheet3,每张表的数据都对应一个SQL表。



您需要做的就是在工作簿上打开OLEDB连接并阅读所有三个使用此连接的工作表。



假设您的数据库中有类似的表结构,您可以将数据放入数据库中。



按照以下步骤完成上述任务:

1)在Excel上打开OLEDB连接:为此,您将大多数时间花时间来制作一个完美的连接字符串,它实际上适用于您的Excel版本(相信我有时候很痛苦! )。查看此链接 [ ^ ]获取连接字符串的帮助。

2)从Excel读取数据:创建OLEDB Command对象从excel表中获取数据就像从SQL数据库中获取数据并填充数据集一样。

  SELECT  *  FROM  [Sheet1 


SELECT * FROM [Sheet2


SELECT * FROM [Sheet3


I have a excel spreadsheet with 3 tables of data in it. How do I read the data from this three different tables from excel spreadsheet and save to the database.
I have to implement this using c#.net

解决方案

As digimanus said.. ODBC/OLEDB connection is the key.

Assuming, you have a WorkBook with three WorkSheets namely Sheet1, Sheet2 and Sheet3 with each sheet having data corresponding to one SQL table.

All you need to do is open an OLEDB connection on your workbook and read all the three sheets using this connection.

Assuming, you have a similar table structure in your database, you can drop this data into your database as it is.

Follow the steps below to achieve the above task:
1) Open OLEDB connection on Excel: To do this, you will mostly spend time to work out a perfect connection string which actually works on your Excel version(trust me it is painful sometimes!). check this link[^] to get some help with your connection string.
2) Read Data from Excel: Create an OLEDB Command object and fetch data from excel sheet just like the way you fetch data from SQL database and populate your data sets.

SELECT * FROM [Sheet1


SELECT * FROM [Sheet2


SELECT * FROM [Sheet3


这篇关于使用c#从Excell电子表格处理数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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