2009-09-08 86 views
0

我想將數據網格導出到excel文件。它正確地轉換asp:BoundColumn,但無法轉換具有linkbutton或imagebutton的模板列。這些單元格被轉換爲空單元格。將網格導出爲ex​​cel時出現問題

這裏是出口的控件到Excel中。代碼,

Public Shared Sub Export(ByVal Mime As String, ByVal Filename As String, ByVal Response As HttpResponse, ByVal Control As Control) 

    Dim stringWrite As New System.IO.StringWriter() 
    Dim htmlWrite As New System.Web.UI.HtmlTextWriter(stringWrite) 
    Response.Clear() 
    Response.Buffer = True 
    Response.Charset = String.Empty 
    Response.ContentType = Mime 
    Response.AddHeader("Content-Disposition", "filename=" & Filename) 
    Control.RenderControl(htmlWrite) 
    Response.Write("<!DOCTYPE html PUBLIC ""-//W3C//DTD XHTML 1.0 Transitional//EN"" ""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd""><html xmlns=""http://www.w3.org/1999/xhtml""><head id=""Head1"" runat=""server""><meta http-equiv=""content-type"" content=""text-html; charset=utf-8""></head><body>" & stringWrite.ToString & "</body></html>") 
    Response.End() 
End Sub 

問題出在哪裏可以?有任何想法嗎? 在此先感謝。

+0

你需要的ImageButton在Excel中呢? – 2009-09-08 06:58:31

+0

其實我的問題不是imagebutton的可見性。不顯示它是可以的,但我想知道原因。但對於linkbutton是我需要它,至少是它的文本。 – 2009-09-08 07:03:35

回答

1

基本上,問題是在服務器端表單之外呈現非文字控件。

這裏有幾篇文章專注於使用GridView做這件事,但也應該與DataGrid一起工作。主要看的是它們在渲染網格之前調用的函數,該網格通過網格中的控件並將任何LinkBut​​ton等轉換爲文字。

第1條:A simpler, more "hard-coded" way, specifically for LinkButtons and Dropdownlists(目光投向下方爲DisableControls功能)

第2條:A more robust and flexible method to deal with different types of controls