Excel导入到DataTable
2014-03-13 13:22
363 查看
1.前台代码
2.后台代码
<asp:FileUpload ID="fupFiles" runat="server" /> <asp:Button ID="btnImprot" runat="server" Text="导入" OnClick="btnImprot_Click" />
2.后台代码
protected void btnImprot_Click(object sender, EventArgs e) { String fileName = System.IO.Path.GetFileName(fupFiles.FileName); String path = Server.MapPath("~/" + fileName); fupFiles.SaveAs(path); DataTable dt = ImportExcelByDB(path); } public static DataTable ImportExcelByDB(string physicalPath) { string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + physicalPath + ";Extended Properties='Excel 8.0;HDR=yes'"; // Excel 2007 if (physicalPath.ToLower().IndexOf(".xlsx") > 0 && physicalPath.ToLower().EndsWith("xlsx")) { //strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + physicalPath + "';Extended Properties='Excel 12.0;HDR=YES'"; //strConn = "'Microsoft.ACE.OLEDB.12.0','Data Source=" + physicalPath + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=1\"'"; strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + physicalPath + ";Extended Properties='Excel 8.0;HDR=yes'"; } // Excel 2003 if (physicalPath.ToLower().IndexOf(".xls") > 0 && physicalPath.ToLower().EndsWith("xls")) { //strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" + physicalPath + "';Extended Properties='Excel 8.0;HDR=YES;'"; strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" + physicalPath + "';Extended Properties=Excel 8.0"; } DataSet dst = new DataSet(); OleDbConnection conn = new OleDbConnection(strConn); try { if (conn.State.ToString() == "Closed") { conn.Open(); } dst.Clear(); string strSql = "SELECT * FROM [Sheet1$]"; OleDbDataAdapter adapter = new OleDbDataAdapter(strSql, conn); adapter.Fill(dst, "[Sheet1$]"); conn.Close(); } catch (Exception ee) { return null; } return dst.Tables[0]; }
相关文章推荐
- 导入Excel到DataTable中的两种实现
- DataTable 导入到Excel的最佳选择Npoi
- 将Excel表格导入DataTable的方法
- 将DataTable 导入Excel并打印
- asp.net导入excel转为datatable
- DataTable数据导入Excel
- DataSet导入到Excel里,多个DataTable分成多个Sheet,Sheet以TableName命名
- Excel导入DataTable(一)
- 【一步一步学NPOI】10.导入Excel到DataTable
- C# DataTable数据导入Excel数据【转载经过本人测试修改】
- 【Dongle】【Web】 Excel导入DataTable
- DataTable导入到Excel文件
- 【C#.NET】将excel或将CSV文件中的sheet1导入到dataGridView1中 ,或者导入到DataTable,导入到DataSet
- Excel导入导出,通过datatable转存(篇一)
- 将DataTable或二维数组导入到Excel
- Excel导入DataTable(一)
- winfrom 使用NPOI导入导出Excel(xls/xlsx)数据到DataTable中
- 使用OpenXml操作Excel,创建excel,导入DataTable的数据等
- 求教。将Excel文件导入DataTable中,然后绑定到GridView中。
- C#中将DataTable数据集导入 Excel