ASP.NET中讀取Excel內容,並顯示在介面上

來源:互聯網
上載者:User

   用戶端上傳本地excel檔案到伺服器上,並在用戶端顯示。

  // 上傳按鈕     protected void btnUp_Click(object sender, EventArgs e)   {       bool b = Upload(fuExcel);  // 上傳excel檔案        if (!b)       {           return;       }       string name = fuExcel.FileName;       string filepath = Server.MapPath("~/upload/") + name;       DataSet ds = ExcelDataSource(filepath, ExcelSheetName(filepath)[0].ToString());       GridView1.DataSource = ds;       GridView1.DataBind();   }   //上傳檔案方法     private bool Upload(FileUpload myFileUpload)   {       bool flag = false;       //是否允許上傳         bool fileAllow = false;       //設定允許上傳的擴充檔案名稱類型         string[] allowExtensions = { ".xls" };       //取得網站根目錄路徑         string path = HttpContext.Current.Request.MapPath("~/upload/");      if (myFileUpload.HasFile)       { string fileExtension = System.IO.Path.GetExtension(myFileUpload.FileName).ToLower();        for (int i = 0; i < allowExtensions.Length; i++)           {               if (fileExtension == allowExtensions[i])               {                   fileAllow = true;               }           }           if (fileAllow)           {               try               {                   //隱藏檔到檔案夾                    myFileUpload.SaveAs(path + myFileUpload.FileName);                   lblMes.Text = "檔案匯入成功";                   flag = true;               }               catch (Exception ex)               {                   lblMes.Text += ex.Message;                   flag = false;               }           }           else           {               lblMes.Text = "不允許上傳:" + myFileUpload.PostedFile.FileName + ",只能上傳xls的檔案,請檢查!";               flag = false;           }       }       else       {           lblMes.Text = "請選擇要匯入的excel檔案!";           flag = false;       }       return flag;   }   //該方法實現從Excel中匯出資料到DataSet中,其中filepath為Excel檔案的絕對路徑, sheetname為excel檔案中的表名
public DataSet ExcelDataSource(string filepath, string sheetname)   {       string strConn;       strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath + ";Extended Properties=Excel 8.0;";       OleDbConnection conn = new OleDbConnection(strConn);       OleDbDataAdapter oada = new OleDbDataAdapter("select * from [" + sheetname + "]", strConn);       DataSet ds = new DataSet();       oada.Fill(ds);       conn.Close();       return ds;   }   //獲得Excel中的所有sheetname。   public ArrayList ExcelSheetName(string filepath)   {       ArrayList al = new ArrayList();       string strConn;       strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath + ";Extended Properties=Excel 8.0;";       OleDbConnection conn = new OleDbConnection(strConn);       conn.Open();       DataTable sheetNames = conn.GetOleDbSchemaTable       (System.Data.OleDb.OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });       conn.Close();       foreach (DataRow dr in sheetNames.Rows)       {           al.Add(dr[2]);       }       return al;   } 
也可以在webconfig檔案裡面指定連接字串的參數:
<connectionStrings>    <add name="xls"connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Sample1.xls;Extended Properties=Excel 8.0"/>    <add name="xlsx"connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Sample.xlsx;Extended Properties=Excel 12.0"/>  </connectionStrings>

string connString = ConfigurationManager.ConnectionStrings["xls"].ConnectionString;

注意XP下檔案操作許可權的問題。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.