2013-05-21 28 views
0

當網站查看ASPX,我有一個表中的細胞數的文字,如:ASP表到Excel

tab.Rows[1].Cells[1].innerHtml = "Booked :" 

(在很多行和單元格,但在每個小區的不同文本)

現在我只想點擊一個按鈕,表格中的數據將被下載到Excel文件中。

表ID:標籤

protected void Page_Load(object sender, EventArgs e) 
{ 
    if (!IsPostBack) 
    { 
     //Table tbl = new Table(); 
     TableRow tr = new TableRow(); 
     TableCell tcel = new TableCell(); 
     tcel.Text = "id"; 
     tr.Cells.Add(tcel); 

     TableCell tcel1 = new TableCell(); 
     tcel1.Text = "id1"; 
     tr.Cells.Add(tcel1); 

     tab.Rows.Add(tr); 
    } 
} 
protected void Button1_Click(object sender, EventArgs e) 
{ 
    string filename = "ExportExcel.xls"; 
    System.IO.StringWriter tw = new System.IO.StringWriter(); 
    System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw); 

    //DataGrid dgGrid = new DataGrid(); 
    //dgGrid.DataSource = tbl; 
    //dgGrid.DataBind(); 

    //Get the HTML for the control.    
    tab.RenderControl(hw); 
    //Write the HTML back to the browser. 
    //Response.ContentType = application/vnd.ms-excel; 
    Response.ContentType = "application/vnd.ms-excel"; 
    Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename + ""); 
    this.EnableViewState = false; 
    Response.Write(tw.ToString()); 
    Response.End(); 
} 

改性watraplion答案,但依然沒有回答.. 錯誤的:

DataTable dt = dt; //Use of unassigned local variable 'dt' 

回答

1

試試這個:

ASPX設計視圖:

<body> 
    <form id="form1" runat="server"> 
     <div> 
      <asp:Table ID="Table1" runat="server"> 
      </asp:Table> 
     </div> 
     <div> 
      <asp:Button ID="btnExport" onclick="btnExport_Click" Text="Export" runat="server"> 
     </div> 
    </form> 
</body> 

個aspx.cs(代碼後面)

protected void Page_Load(object sender, EventArgs e) 
    { 
     TableRow tr = new TableRow(); 
     TableCell tcel = new TableCell(); 
     tcel.Text = "id"; 
     tr.Cells.Add(tcel); 

     TableCell tcel1 = new TableCell(); 
     tcel1.Text = "id1"; 
     tr.Cells.Add(tcel1); 

     Table1.Rows.Add(tr); 
    } 

    protected void btn_Click(object sender, EventArgs e) 
    { 
     string filename = "ExportExcel.xls"; 
     System.IO.StringWriter tw = new System.IO.StringWriter(); 
     System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw); 

     //Get the HTML for the control.    
     Table1.RenderControl(hw); 
     //Write the HTML back to the browser. 
     //Response.ContentType = application/vnd.ms-excel; 
     Response.ContentType = "application/vnd.ms-excel"; 
     Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename + ""); 
     this.EnableViewState = false; 
     Response.Write(tw.ToString()); 
     Response.End(); 
    } 
+0

對不起,我覺得沒必要改變這一點, 但這裏有錯誤DataGrid dgGrid = new DataGrid(); – CigarDon

+0

它要求我解決使用System.Windows.Forms.DataGrid或System.Web.UI.WebControls.DataGrid ... – CigarDon

+0

您是否動態創建代碼背後的HTML表或使用在aspx頁面?? – watraplion

0

首先說,這是你正在試圖做的是不導出到Excel,但您發送HTML有錯誤標題來誘騙瀏覽器打開該內容與Excel 。

這個解決方案有很多問題,excel自己會警告用戶內容不同於擴展名,因爲你發送html,你的響應頭文件說這是一個excel文件。我敢打賭,客戶端上的某些反惡意軟件或類似服務器上的某些反惡意軟件會阻止此響應,因爲服務的內容不是頭中聲明的內容,而是已知的惡意軟件行爲。

這是遠遠更好,更容易使用專用擅長庫,例如EPPlus,看看這裏的一個導出數據表到真實的Excel文件ASP.NET ashx的處理程序:

https://stackoverflow.com/a/9569827/351383