Đọc dữ liệu từ Excel

Mã nguồn dưới đây hướng dẫn cách đọc dữ liệu từ Excel vào cơ sở dữ liệu

var fullFileName =”PathOfExcelFile”;
var connectionString = string.Format(“Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties=Excel 8.0;data source={0};”, fullFileName);
var adapter = new OleDbDataAdapter(“select * from [Ten_Sheet$]”, connectionString);
var ds = new DataSet();
string tableName = “tableName”;
adapter.Fill(ds, tableName);
DataTable data = ds.Tables[tableName];
dataGridView1.Columns.Clear();
dataGridView1.DataSource = data;

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s