#前台代码:使用服务端控件实现上传
1 <form id="form1" runat="server"> 2 <div> 3 <asp:FileUpload ID="ExcelFileUpload" runat="server" /> 4 <asp:Button ID="UploadBtn" runat="server" Text="确定上传" OnClick="UploadBtn_Click" /> 5 </div> 6 </form>
#服务端代码:
1 protected void UploadBtn_Click(object sender, EventArgs e) 2 { 3 if (ExcelFileUpload.HasFile == false)//HasFile用来检查FileUpload是否有文件 4 { 5 Response.Write("<script>alert('请您选择Excel文件')</script> "); 6 return;//当无文件时,返回 7 } 8 string IsXls = Path.GetExtension(ExcelFileUpload.FileName).ToString().ToLower();//System.IO.Path.GetExtension获得文件的扩展名 9 if (IsXls != ".xlsx" && IsXls != ".xls") 10 { 11 Response.Write(ExcelFileUpload.FileName); 12 Response.Write("<script>alert('只可以选择Excel文件')</script>"); 13 return;//当选择的不是Excel文件时,返回 14 } 15 16 string filename = ExcelFileUpload.FileName;//获取Execle文件名 17 string savePath = Server.MapPath(("UploadExcel\\") + filename);//Server.MapPath 服务器上的指定虚拟路径相对应的物理文件路径 18 //savePath ="D:\vsproject\Projects\exceltestweb\exceltestweb\uploadfiles\test.xls" 19 //Response.Write(savePath); 20 DataTable ds = new DataTable(); 21 ExcelFileUpload.SaveAs(savePath);//将文件保存到指定路径 22 23 DataTable dt = GetExcelDatatable(savePath);//读取excel数据 24 List<RegNumInfo> regList = ConvertDtToInfo(dt);//将datatable转为list 25 File.Delete(savePath);//删除文件 26 27 Response.Write("<script>alert('上传文件读取数据成功!');</script>"); 28 } 29 /// <summary> 30 /// 从excel文件中读取数据 31 /// </summary> 32 /// <param name="fileUrl">实体文件的存储路径</param> 33 /// <returns></returns> 34 private static DataTable GetExcelDatatable(string fileUrl) 35 { 36 //支持.xls和.xlsx,即包括office2010等版本的;HDR=Yes代表第一行是标题,不是数据; 37 string cmdText = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileUrl + "; Extended Properties=\"Excel 12.0;HDR=Yes\""; 38 System.Data.DataTable dt = null; 39 //建立连接 40 OleDbConnection conn = new OleDbConnection(cmdText); 41 try 42 { 43 //打开连接 44 if (conn.State == ConnectionState.Broken || conn.State == ConnectionState.Closed) 45 { 46 conn.Open(); 47 } 48 49 System.Data.DataTable schemaTable = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null); 50 string strSql = "select * from [Sheet1$]"; //这里指定表明为Sheet1,如果修改过表单的名称,请使用修改后的名称 51 OleDbDataAdapter da = new OleDbDataAdapter(strSql, conn); 52 DataSet ds = new DataSet(); 53 da.Fill(ds); 54 dt = ds.Tables[0]; ; 55 return dt; 56 } 57 catch (Exception exc) 58 { 59 throw exc; 60 } 61 finally 62 { 63 conn.Close(); 64 conn.Dispose(); 65 } 66 } 67 /// <summary> 68 /// 将datatable转换为list集合 69 /// </summary> 70 /// <param name="dt">DataTable</param> 71 /// <returns></returns> 72 private static List<RegNumInfo> ConvertDtToInfo(DataTable dt) 73 { 74 List<RegNumInfo> list = new List<RegNumInfo>(); 75 if (dt.Rows.Count > 0) 76 { 77 foreach (DataRow item in dt.Rows) 78 { 79 RegNumInfo info = new RegNumInfo(); 80 info.RegNum = item[0].ToString(); 81 info.Name = item[1].ToString(); 82 info.Period = item[2].ToString(); 83 info.Remark = item[3].ToString(); 84 list.Add(info); 85 } 86 } 87 return list; 88 }
1 public class RegNumInfo 2 { 3 public string RegNum { get; set; } 4 public string Name { get; set; } 5 public string Period { get; set; } 6 public string Remark { get; set; } 7 }
#注意:出现“未在本地计算机上注册“microsoft.ACE.oledb.12.0”提供程序” 的报错的解决方案
1、因为读取excel文件使用的是OleDb,如果服务器没有安装office,需要安装数据访问组件(AccessDatabaseEngine);
*适用于office2010的
Microsoft Access Database Engine 2010 Redistributable
2、在IIS应用程序池中,设置“”启用兼容32位应用程序”;
解决方案具体可参考这篇文章:https://www.cnblogs.com/willingtolove/p/9630485.html
#参考:
https://blog.csdn.net/eagle_or_snail/article/details/82748226
https://www.cnblogs.com/willingtolove/p/9630485.html