Read Excel to DataTable

Aprile 23rd, 2007

Description: Read Excel to DataTable

Link: http://www.codekeep.net/snippets/9c43beed-1312-4ac2-b19c-c60e56c93c65.aspx

using System.Data.OleDb
.
.
.

string fileLocation = @"c:\temp\testfile.xls";

OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileLocation +";Extended Properties=\"Excel 8.0;HDR=YES\"");
OleDbDataAdapter da = new OleDbDataAdapter(); 

dt = new DataTable();
da.SelectCommand = new OleDbCommand("SELECT * FROM [Sheet1$]", conn);
da.Fill(dt);  // We could be filling a DataSet too
.
.
.
conn.Dispose();
da.Dispose();
dt.Dispose();

  • .NET
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

Trackback URI | Comments RSS

Leave a Reply

You must be logged in to post a comment.