2012-02-09 69 views
1

在Web應用程序中,我試圖從Excel中的數據,並綁定到GridView控件,它工作正常,但在Excel中包含whcih是一種在細胞只有單一的價值和remaing細胞該行是空則顯示默認字母等[F,G,H ..]像下面在GridView header.The的頂部是實際的excel表 enter image description hereExcel數據到gridview?在Asp.net中?

下面

在excel表格式 enter image description here

這是我的代碼在頁面加載甚至

 System.Data.DataTable dt = null; 

      string excelFile = Server.MapPath("./performances.xls"); 
      DataSet ds = new DataSet(); 
      // Connection String. 
      String connString = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + excelFile + ";Extended Properties=Excel 8.0;"; 
      // Create connection. 
      oledbConn = new OleDbConnection(connString); 
      // Opens connection with the database. 
      oledbConn.Open(); 
      // Get the data table containing the schema guid, and also sheet names. 
      dt = oledbConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null); 
      if (dt == null) 
      { 
       return; 
      } 
      String[] excelSheets = new String[dt.Rows.Count]; 
      int i = 0; 
      // Add the sheet name to the string array. 
      // And respective data will be put into dataset table 
      foreach (DataRow row in dt.Rows) 
      { 
       excelSheets[i] = row["TABLE_NAME"].ToString(); 
       OleDbCommand cmd = new OleDbCommand("SELECT * FROM [" + excelSheets[i] + "]", oledbConn); 
       OleDbDataAdapter oleda = new OleDbDataAdapter(); 
       oleda.SelectCommand = cmd; 
       oleda.Fill(ds, "TABLE"); 
       i++; 
      } 
      // Bind the data to the GridView 
      grdMygrd .DataSource = ds.Tables[0].DefaultView; 
      grdMygrd.DataBind(); 
      Session["Table"] = ds.Tables[0]; 
+0

請幫幫我,解決這個問題可以 – 2012-02-09 11:02:17

+0

粘貼一些代碼,因爲它是一個有點難以明白你用什麼方法,使約束力。你是否將它綁定爲數據集? – Igarioshka 2012-02-09 17:08:06

+0

你肯定lgarioshka,我會把代碼 – 2012-02-10 03:42:41

回答

0

我已經重複了你的錯誤,並且在我看來這是excel格式的問題。 Excel文檔中的標題是簡單的合併單元格,OleDB數據提供程序將這些單元格視爲單獨的空值,並且由合併單元格組成的標題的值位於底部單元格中。可能的解決方案是在excel中嘗試並壓扁標題,並手動在頁面上繪製它們。

會的身影,因爲OLEDB數據提供商看到Excel文件作爲數據庫,以及那些實際上並不具有子報頭。

相關問題