当前位置: 移动技术网 > IT编程>开发语言>.net > asp.net上传Excel文件并读取数据的实现方法

asp.net上传Excel文件并读取数据的实现方法

2019年03月20日  | 移动技术网IT编程  | 我要评论

前言

本文主要给大家介绍了关于asp.net上传excel文件并读取数据的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧

实现如下:

前台代码:使用服务端控件实现上传

  <form id="form1" runat="server">
   <div>
    <asp:fileupload id="excelfileupload" runat="server" />
    <asp:button id="uploadbtn" runat="server" text="确定上传" onclick="uploadbtn_click" />
   </div>
  </form>

服务端代码:

protected void uploadbtn_click(object sender, eventargs e)
{
 if (excelfileupload.hasfile == false)//hasfile用来检查fileupload是否有文件
 {
  response.write("<script>alert('请您选择excel文件')</script> ");
  return;//当无文件时,返回
 }
 string isxls = path.getextension(excelfileupload.filename).tostring().tolower();//system.io.path.getextension获得文件的扩展名
 if (isxls != ".xlsx" && isxls != ".xls")
 {
  response.write(excelfileupload.filename);
  response.write("<script>alert('只可以选择excel文件')</script>");
  return;//当选择的不是excel文件时,返回
 }

 string filename = excelfileupload.filename;//获取execle文件名 
 string savepath = server.mappath(("uploadexcel\\") + filename);//server.mappath 服务器上的指定虚拟路径相对应的物理文件路径
 //savepath ="d:\vsproject\projects\exceltestweb\exceltestweb\uploadfiles\test.xls"
 //response.write(savepath);
 datatable ds = new datatable();
 excelfileupload.saveas(savepath);//将文件保存到指定路径

 datatable dt = getexceldatatable(savepath);//读取excel数据
 list<regnuminfo> reglist = convertdttoinfo(dt);//将datatable转为list
 file.delete(savepath);//删除文件

 response.write("<script>alert('上传文件读取数据成功!');</script>");
}
/// <summary>
/// 从excel文件中读取数据
/// </summary>
/// <param name="fileurl">实体文件的存储路径</param>
/// <returns></returns>
private static datatable getexceldatatable(string fileurl)
{
 //支持.xls和.xlsx,即包括office2010等版本的;hdr=yes代表第一行是标题,不是数据;
 string cmdtext = "provider=microsoft.ace.oledb.12.0;data source=" + fileurl + "; extended properties=\"excel 12.0;hdr=yes\"";
 system.data.datatable dt = null;
 //建立连接
 oledbconnection conn = new oledbconnection(cmdtext);
 try
 {
  //打开连接
  if (conn.state == connectionstate.broken || conn.state == connectionstate.closed)
  {
   conn.open();
  }

  system.data.datatable schematable = conn.getoledbschematable(oledbschemaguid.tables, null);
  string strsql = "select * from [sheet1$]"; //这里指定表明为sheet1,如果修改过表单的名称,请使用修改后的名称
  oledbdataadapter da = new oledbdataadapter(strsql, conn);
  dataset ds = new dataset();
  da.fill(ds);
  dt = ds.tables[0]; ;
  return dt;
 }
 catch (exception exc)
 {
  throw exc;
 }
 finally
 {
  conn.close();
  conn.dispose();
 }
}
/// <summary>
/// 将datatable转换为list集合
/// </summary>
/// <param name="dt">datatable</param>
/// <returns></returns>
private static list<regnuminfo> convertdttoinfo(datatable dt)
{
 list<regnuminfo> list = new list<regnuminfo>();
 if (dt.rows.count > 0)
 {
  foreach (datarow item in dt.rows)
  {
   regnuminfo info = new regnuminfo();
   info.regnum = item[0].tostring();
   info.name = item[1].tostring();
   info.period = item[2].tostring();
   info.remark = item[3].tostring();
   list.add(info);
  }
 }
 return list;
}
public class regnuminfo
{
 public string regnum { get; set; }
 public string name { get; set; }
 public string period { get; set; }
 public string remark { get; set; }
}

注意:出现“未在本地计算机上注册“microsoft.ace.oledb.12.0”提供程序” 的报错的解决方案

  1、因为读取excel文件使用的是oledb,如果服务器没有安装office,需要安装数据访问组件(accessdatabaseengine);

    *适用于office2010的

    microsoft access database engine 2010 redistributable

    https://www.microsoft.com/zh-cn/download/details.aspx?id=13255

  2、在iis应用程序池中,设置“”启用兼容32位应用程序”;

解决方案具体可参考这篇文章:

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对移动技术网的支持。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网