当前位置导航:炫浪网>>网络学院>>网页制作>>ASP.NET教程

asp.net导入excel转为datatable

  1 前台代码:

  <asp:FileUpload runat="server" ID="fupFiles" Width="300px" />excel表名<asp:TextBox ID="txtSheet" runat="server">Sheet1</asp:TextBox>//表名为excel左下角的表名,默认为sheet1,sheet2,...

  2 后台:

  1 string excelName = txtSheet.Text;

  01 string excelPath=  fupFiles.PostedFile.FileName;

  02 string strConn;

  03 strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath + ";Extended Properties=Excel 8.0;";

  04 OleDbConnection conn = new OleDbConnection(strConn);

  05 OleDbDataAdapter oada = new OleDbDataAdapter ( "select * from [" + sheetname + "$]", strConn );

  06 DataTable dt = new DataTable();

  07 try {

  08       oada.Fill(dt);//获得datatable

  09  }

  10  catch( Exception ex) {      }

相关内容
赞助商链接