ASP.NET中讀取excel內容並顯示

來源:互聯網
上載者:User

標籤:http   檔案   資料   os   art   問題   

   項目中經常會用到把excel的檔案內容匯入到資料庫中的,剛剛花了點時間,做了個例子,基本上能實現匯入Excel後顯示的功能吧,匯入的excel檔案得是xls,即是2003的. 
    代碼思路如下:要讀取的excel檔案必得得是在本地硬碟,所以一般來說都是讓遠端使用者選擇自己硬碟上的Excel檔案,然後把使用者選擇的檔案上傳到本機伺服器上,再在本機伺服器上進行操作.我把介面後置代碼重要部分貼出來,大家自己慢慢看吧,都有注釋了. 

C#代碼  
  1. // 上傳按鈕  
  2.     protected void 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.     private bool 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, new object[] { null, null, null, "TABLE" });  
  93.         conn.Close();  
  94.         foreach (DataRow dr in sheetNames.Rows)  
  95.         {  
  96.             al.Add(dr[2]);  
  97.         }  
  98.         return al;  
  99.     }  


要注意的是我們要一開始就在網站根目錄下建立upload檔案夾,而且要把他的使用權限設定為可讀可寫的?這個許可權的問題搞得頭大,不知道到底應該怎麼搞的,XP系統下建立立的檔案夾好像都是唯讀,我右鍵屬性把唯讀去掉,結果再次查看的時候還是唯讀,不過好像發現對程式沒有什麼意思,上傳完excel檔案後還是可以讀取查看的. 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.