开发者

Read excel file in c# using dynamic keyword

开发者 https://www.devze.com 2023-03-20 09:28 出处:网络
How can I read the excel file using C# 4.0. Once I saw in some video it becomes very easy to read the file using dynamic keyword but now when I search to find any tutorial, I can\'t seem to find any.

How can I read the excel file using C# 4.0. Once I saw in some video it becomes very easy to read the file using dynamic keyword but now when I search to find any tutorial, I can't seem to find any. Can anybody point me in some direction or give some snippet that reads from excel file (.xlxs). I only want to read the first sheet but that sheet can contains any columns and rows. I want to read them all. The data contained 开发者_如何学Cin the sheet is just simply numbers and strings. No more than that!

Thanks in advance :)


This isn't directly answer, but I would like to recommend you use the Excel Data Reader, which is opensource under the LGPL licence. Using the dynamic keyword means using the COM interop of the Excel, which may be slower than the former and would annoy you with using the dynamic keyword.


If you just want to read it better to go for OleDb rather than going for dynamic

Something like this

using System.Data;
using System.Data.OleDb;

... 

String sConnectionString = 
"Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + [Your Excel File Name Here] + ";" +
"Extended Properties=Excel 8.0;";


OleDbConnection objConn = new OleDbConnection(sConnectionString);

objConn.Open();

OleDbCommand objCmdSelect =new OleDbCommand("SELECT * FROM [Sheet1$]", objConn);

OleDbDataAdapter objAdapter1 = new OleDbDataAdapter();

objAdapter1.SelectCommand = objCmdSelect;

DataSet objDataset1 = new DataSet();

objAdapter1.Fill(objDataset1);

objConn.Close(); 
0

精彩评论

暂无评论...
验证码 换一张
取 消