2012-01-10 49 views
1

在Web應用程序[asp.net]中,我試圖獲取gridview中的excel數據。它工作正常,但gridview中的表格格式與excel不同。我的意思是表格的格式不同於excel到gridivew。我正在放置屏幕截圖請找到它們首先是Excel格式:enter image description here表格格式從Excel到Gridview?在Asp.Net中?

第二個是Gridview Screent鏡頭,它在.aspx頁面。

enter image description here

請幫我在格式化表的設計。謝謝。

+0

如果您爲Gridview顯示標記並描述您想要的更改,它會對我們有所幫助。現在,我們必須通過比較屏幕截圖來了解自己的情況。 – 2012-01-12 12:09:51

+0

你的格式是什麼意思?只是外觀?這是asp.net應用程序你不能用CSS做到這一點? – bAN 2012-01-12 12:10:39

+0

我認爲你從電網控制問得太多。你要求從網格控制來實現excel - 但是excel是一個多年的開發程序,網格控制只是asp.net的一個基本幫助控制。我認爲你有很多工作要做。 – Aristos 2012-01-12 15:04:13

回答

0

嘗試從這個鏈接:http://www.dotnetcurry.com/ShowArticle.aspx?ID=138
在一個學習Linq的視頻中,我看到了這個標籤,但沒有完全記住這一點。但我測試了這一點,你也可以用你的linq代碼製作表格。
玩得開心!

0

試試這個代碼導入Excel到的GridView:

using System.Data; 
public partial class _Default : System.Web.UI.Page 
{ 
    DataTable dt = new DataTable(); 
    protected void Page_Load(object sender, EventArgs e) 
    { 

    } 
    protected void Button1_Click(object sender, EventArgs e) 
    { 
     //Check file is available in File upload Control 
     if (FileUpload1.HasFile) 
     { 
      //Store file name in the string variable 
      string filename = FileUpload1.FileName; 
      //Save file upload file in to server path for temporary 
      FileUpload1.SaveAs(Server.MapPath(filename)); 
      //Export excel data into Gridview using below method 
      ExportToGrid(Server.MapPath(filename));    
     }   
    } 
    void ExportToGrid(String path) 
    { 
     OleDbConnection MyConnection = null; 
     DataSet DtSet = null; 
     OleDbDataAdapter MyCommand = null; 

     //Connection for MS Excel 2003 .xls format 
     MyConnection = new OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0; Data Source='" + path + "';Extended Properties=Excel 8.0;"); 

     //Connection for .xslx 2007 format 
     // MyConnection = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + path + "';Extended Properties=Excel 12.0;");    

     //Select your Excel file 
     MyCommand = new System.Data.OleDb.OleDbDataAdapter("select * from [Sheet1$]", MyConnection); 
     DtSet = new System.Data.DataSet(); 
     //Bind all excel data in to data set 
     MyCommand.Fill(DtSet, "[Sheet1$]"); 
     dt = DtSet.Tables[0]; 
     MyConnection.Close(); 
     //Check datatable have records 
     if (dt.Rows.Count > 0) 
     { 
      GridView1.DataSource = dt; 
      GridView1.DataBind(); 
     } 
     //Delete temporary Excel file from the Server path 
     if(System.IO.File.Exists(path)) 
     { 
      System.IO.File.Delete(path); 
     } 
    } 
} 
0

一個主要可見問題的是你的Excel電子表格有子表(例如,貨物數量)。在「純」 HTML,這可以使用COLSPAN or ROWSPAN來完成,但與GridView控件,它不是那麼容易,除非你創建子GridView的,就像這樣:

<asp:GridView runat="server" ...> 
    <Columns> 
    <asp:BoundField HeaderText="MyHeader" DataField="MyField1"... /> 
    ... 
    <asp:TemplateField HeaderText="MySubGridHeader"> 
     <asp:GridView runat="server" ...> // need to databind this 
     <Columns> 
      <asp:BoundField HeaderText="MySubHeader1" DataField="MyField2"... /> 
      <asp:BoundField HeaderText="MySubHeader2" DataField="MyField3"... /> 
     </Columns> 
     </asp:GridView> 
    </asp:TemplateField> 
    </Columns> 
</asp:GridView> 

否則我建議你去爲它可以顯示商業軟件包先進的網格,例如這個例子:SPREAD(注意:我不是附屬的,我沒有測試過)。