[C#|.Net] DataGridView 엑셀(Excel) 파일 Import 하는 방법 (xls, xlsx)

DataGridView 컨트롤에 엑셀파일을 불러와서 바인딩하는 예제이다.


String name = "Sheet1"; //Name of your Sheet in the work book String constr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + "textfile.xlsx"+ ";Extended Properties='Excel 12.0 XML;HDR=YES;';"; OleDbConnection Connection = new OleDbConnection(constr); OleDbCommand OleConnection = new OleDbCommand("SELECT * FROM [" + name + "$]", Connection); Connection.Open(); OleDbDataAdapter sda = new OleDbDataAdapter(OleConnection); DataTable data = new DataTable(); sda.Fill(data); dataGridView1.DataSource = data;



ExcelToDataGridViewTest.zip


<예제파일>




 

댓글

Designed by JB FACTORY