ASP.net中读取excel内容,并显示在界面上

来源:互联网 发布:2020年中国 知乎 编辑:程序博客网 时间:2024/05/22 11:38
  1. // 上传按钮   
  2.     protectedvoid btnUp_Click(object sender, EventArgs e)   
  3.      {   
  4.         bool b = Upload(fuExcel);  // 上传excel文件  
  5.         if (!b)   
  6.          {   
  7.             return;   
  8.          }   
  9.         string name = fuExcel.FileName;   
  10.         string filepath = Server.MapPath("~/upload/") + name;   
  11.          DataSet ds = ExcelDataSource(filepath, ExcelSheetName(filepath)[0].ToString());   
  12.          GridView1.DataSource = ds;   
  13.          GridView1.DataBind();   
  14.      }   
  15.   
  16.     //上传文件方法   
  17.     privatebool Upload(FileUpload myFileUpload)   
  18.      {   
  19.         bool flag =false;   
  20.         //是否允许上载  
  21.         bool fileAllow =false;   
  22.         //设定允许上载的扩展文件名类型  
  23.         string[] allowExtensions = {".xls" };   
  24.   
  25.         //取得网站根目录路径  
  26.         string path = HttpContext.Current.Request.MapPath("~/upload/");   
  27.         //检查是否有文件案  
  28.         if (myFileUpload.HasFile)   
  29.          {   
  30.             //取得上传文件之扩展文件名,并转换成小写字母  
  31.             string fileExtension = System.IO.Path.GetExtension(myFileUpload.FileName).ToLower();   
  32.             //检查扩展文件名是否符合限定类型  
  33.             for (int i = 0; i < allowExtensions.Length; i++)   
  34.              {   
  35.                 if (fileExtension == allowExtensions[i])   
  36.                  {   
  37.                      fileAllow = true;   
  38.                  }   
  39.              }   
  40.   
  41.             if (fileAllow)   
  42.              {   
  43.                 try  
  44.                  {   
  45.                     //存储文件到文件夹  
  46.                      myFileUpload.SaveAs(path + myFileUpload.FileName);   
  47.                      lblMes.Text = "文件导入成功";   
  48.                      flag = true;   
  49.                  }   
  50.                 catch (Exception ex)   
  51.                  {   
  52.                      lblMes.Text += ex.Message;   
  53.                      flag = false;   
  54.                  }   
  55.              }   
  56.             else  
  57.              {   
  58.                  lblMes.Text = "不允许上载:" + myFileUpload.PostedFile.FileName +",只能上传xls的文件,请检查!";   
  59.                  flag = false;   
  60.              }   
  61.          }   
  62.         else  
  63.          {   
  64.              lblMes.Text = "请选择要导入的excel文件!";   
  65.              flag = false;   
  66.          }   
  67.         return flag;   
  68.      }   
  69.   
  70.     //该方法实现从Excel中导出数据到DataSet中,其中filepath为Excel文件的绝对路径, sheetname为excel文件中的表名  
  71.     public DataSet ExcelDataSource(string filepath,string sheetname)   
  72.      {   
  73.         string strConn;   
  74.          strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath +";Extended Properties=Excel 8.0;";   
  75.          OleDbConnection conn = new OleDbConnection(strConn);   
  76.          OleDbDataAdapter oada = new OleDbDataAdapter("select * from [" + sheetname + "]", strConn);   
  77.          DataSet ds = new DataSet();   
  78.          oada.Fill(ds);   
  79.          conn.Close();   
  80.         return ds;   
  81.      }   
  82.   
  83.     //获得Excel中的所有sheetname。  
  84.     public ArrayList ExcelSheetName(string filepath)   
  85.      {   
  86.          ArrayList al = new ArrayList();   
  87.         string strConn;   
  88.          strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath +";Extended Properties=Excel 8.0;";   
  89.          OleDbConnection conn = new OleDbConnection(strConn);   
  90.          conn.Open();   
  91.          DataTable sheetNames = conn.GetOleDbSchemaTable   
  92.          (System.Data.OleDb.OleDbSchemaGuid.Tables, newobject[] {null,null,null,"TABLE" });   
  93.          conn.Close();   
  94.         foreach (DataRow drin sheetNames.Rows)   
  95.          {   
  96.              al.Add(dr[2]);   
  97.          }   
  98.         return al;   
  99.      }