xls表格导入的问题总结
最近对于xls导入到数据库的操作比较多,遇到了些问题就此总结下:
1、表格导入数据库核心代码 (参考网址:http://blog.sina.com.cn/s/blog_5f0dab1e0100oc85.html)
asp.net前端代码
<%@ Page Language="C#" Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "<a href="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd</a>"> <html xmlns="<a href="http://www.w3.org/1999/xhtml">http://www.w3.org/1999/xhtml</a>"> <head runat="server"> <title>无标题页</title> </head> <body> <form id="form1" runat="server"> 请选择要导入的文件:<INPUT id="FileExcel" style="WIDTH: 300px" type="file" size="42" name="FilePhoto" runat="server" /> <asp:button id="btnImport" Text="导 入" CssClass="button" Runat="server" onclick="btnImport_Click"></asp:button><br /> <asp:label id="lblMessage" runat="server" Font-Bold="True" ForeColor="Red"></asp:label> </form> </body> </html>
asp.net后端代码
using System; using System.Configuration; using System.Data; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.IO;public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { }/// <summary> /// 上传Excel文件 /// </summary> /// <param name="inputfile">上传的控件名</param> /// <returns></returns> private string UpLoadXls(System.Web.UI.HtmlControls.HtmlInputFile inputfile) { string orifilename = string.Empty; string uploadfilepath = string.Empty; string modifyfilename = string.Empty; string fileExt = "" ;//文件扩展名 int fileSize = 0;//文件大小 try { if(inputfile.Value != string.Empty) { //得到文件的大小 fileSize = inputfile.PostedFile.ContentLength; if(fileSize == 0 ) { throw new Exception("导入的Excel文件大小为0,请检查是否正确!"); } //得到扩展名 fileExt = inputfile.Value.Substring(inputfile.Value.LastIndexOf(".")+1); if(fileExt.ToLower() != "xls") {throw new Exception("你选择的文件格式不正确,只能导入EXCEL文件!"); } //路径 uploadfilepath = Server.MapPath("~/"); //新文件名 modifyfilename = System.Guid.NewGuid().ToString(); modifyfilename += "."+inputfile.Value.Substring(inputfile.Value.LastIndexOf(".")+1); //判断是否有该目录 System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(uploadfilepath);if (!dir.Exists) { dir.Create(); } orifilename = uploadfilepath+modifyfilename; //如果存在,删除文件 if(File.Exists(orifilename)) { File.Delete(orifilename); } // 上传文件 inputfile.PostedFile.SaveAs(orifilename); } else { throw new Exception("请选择要导入的Excel文件!"); } } catch(Exception ex) { throw ex; } return orifilename; }protected void btnImport_Click(object sender, System.EventArgs e) {try { string strFileName = this.UpLoadXls(this.comFile); string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strFileName + ";Extended Properties='Excel 8.0;HDR=No;IMEX=1'"; OleDbConnection conn = new OleDbConnection(strConn); OleDbDataAdapter myCommand = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", strConn); DataSet myDataSet = new DataSet(); myCommand.Fill(myDataSet); string str = ""; } catch (Exception ex) { throw ex; } finally { DeleteFile(strFileName); }
}/// <summary> /// 删除文件 /// </summary> /// <param name="filename">待删除的文件名</param> private void DeleteFile(string filename) { if (filename != string.Empty && File.Exists(filename)) { File.Delete(filename); } } }
2、ISAM错误问题
参考网址:http://www.cnblogs.com/mczhu/archive/2008/07/17/1244978.html
连接OLEDB可能出现ISAM报错的问题,这个问题一般出现在string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strFileName + ";Extended Properties='Excel 8.0;HDR=No;IMEX=1'";这条语句上,要注意这个连接词中的空格部分如:Data Source ;Extended Properties 等,这些中都含有一个空格符且不能多。同时在Extended Properties中如果出现多个属性时必须如Extended Properties='Excel 8.0;HDR=No;IMEX=1' 一样加上单引号
参数说明:
Microsoft Jet 提供程序用于连接到 Excel 工作簿。在以下连接字符串中,Extended Properties 关键
字设置 Excel 特定的属性。“HDR=Yes;”指示第一行中包含列名,而不是数据,“IMEX=1;”通知驱动程
序始终将“互混”数据列作为文本读取。注意
http://msdn2.microsoft.com/zh-cn/library/ms254978.aspx
默认情况下,系统认为 Excel 数据源的第一行包含可用作字段名的列标题。如果不是这种情况,则必须将该设置关闭,否则,第一行数据将会“消失”,而被用作字段名称。这可通过向连接字符串的扩展属性
添加可选的 HDR= 设置来完成。默认情况下(无需指定)是 HDR=Yes。如果没有列标题,则需要指定
HDR=No;提供程序将字段命名为 F1、F2 等等。因为扩展属性字符串现在包含了多个值,所以必须用引号
单独包起来,如下例所示(为便于看清楚,添加了额外的空格)。
如第1个中hdr=no的话,where时就会报错