以ASP.net 導入 Excel檔 轉DataTable

  • 2083
  • 0
  • 2011-08-04

摘要:以ASP.net 導入 Excel檔 轉DataTable

前臺代碼:

<asp:fileupload runat="server" id="fupFiles" width="300px">excel表名<asp:textbox id="txtSheet" runat="server">Sheet1</asp:textbox>//表名為excel左下角的表名,默認為sheet1,sheet2,...

後臺:


string excelName = txtSheet.Text;
string excelPath=  fupFiles.PostedFile.FileName;
string strConn;
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath + ";Extended Properties=Excel 8.0;";//filepath Excel檔案路徑
OleDbConnection conn = new OleDbConnection(strConn);
OleDbDataAdapter oada = new OleDbDataAdapter ( "select * from [" + sheetname + "$]", strConn );//sheetname Excel 表名稱
DataTable dt = new DataTable();
oada.Fill(dt);//獲得datatable