Đọc Nhiều File Excel Cùng Cấu Trúc Vào Gridview | VN-Zoom | Cộng đồng Chia Sẻ Kiến Thức Công Nghệ và Phần Mềm Máy Tính

Adblocker detected! Please consider reading this notice.

We've detected that you are using AdBlock Plus or some other adblocking software which is preventing the page from fully loading.

We need money to operate the site, and almost all of it comes from our online advertising.

If possible, please support us by clicking on the advertisements.

Please add vn-z.vn to your ad blocking whitelist or disable your adblocking software.

×

Đọc Nhiều File Excel Cùng Cấu Trúc Vào Gridview

mtkien30

Gà con
Em chào các bác!
Các bác cho em hỏi, em có đoạn code sau, đọc 1 file excel lên Gridview rất ok, giờ em muốn sửa đoạn code này lại đọc được nhiều file excel cùng cấu trúc lên gridview. Mong các anh chị giúp đỡ ạ. Em xin cám ơn ạ!

private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog F = new OpenFileDialog();
F.ShowDialog();
F.Title = "Chọn File Excel";
F.Multiselect = true;
string name = F.FileName;
if (name != "")
{

String sheet = "ABC";
String constr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + name + ";Extended Properties='Excel 12.0 XML;HDR=YES;';";
OleDbConnection con = new OleDbConnection(constr);
OleDbCommand oconn = new OleDbCommand("Select * From [" + sheet + "$]", con);
try
{
con.Open();
OleDbDataAdapter sda = new OleDbDataAdapter(oconn);
DataTable data = new DataTable();
sda.Fill(data);
gridView1.Columns.Clear();
gridControl1.DataSource = data;
gridView1.Columns[0].Width = 100;
gridView1.BestFitColumns();
gridView1.Columns[0].SummaryItem.SummaryType = DevExpress.Data.SummaryItemType.Count;
gridView1.Columns[0].SummaryItem.DisplayFormat = "Số dòng: {0}";
con.Close();
con.Dispose();

}
catch
{
if (name != "")
XtraMessageBox.Show("File " + name + " không đúng định dạng file Excel.", "Cảnh báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
con.Close();
con.Dispose();
}
}
}
 

malemkhoang

Rìu Chiến
Tôi góp ý cho bạn về mặt nguyên lý thôi.
Bạn đã có code xử lý cho trường hợp 1 file...
Muốn xử lý cho nhiều file thì phải sử dụng vòng lặp dựa trên code xử lý cho trường hợp đơn lẻ trên.
Tức là đi từ cụ thể tới tổng quát...
Chúc bạn thành công.
 

mtkien30

Gà con
Gridview thì thường sẽ cần set DataSource. Bạn có thể có nhiều cách để xử lý. Có thể xử lý viết hàm chung để đọc file, rồi đọc từng file cho ra 1 DataTable, sau đó thì gộp chúng lại rồi set DataSource.
Bạn có thể sửa code này giúp mình, mình cám ơn bạn nhiều!
 


Top