新聞中心
Wijmo GridView 控件不提供導出Excel文件的方法。本篇博客介紹一種將Wijmo的GridView控件保存到Excel的簡單方法。你可以使用同樣的方法在C1 WebUI GridView上。

創(chuàng)新互聯(lián)公司成立于2013年,是專業(yè)互聯(lián)網(wǎng)技術服務公司,擁有項目做網(wǎng)站、網(wǎng)站建設網(wǎng)站策劃,項目實施與項目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元晉州做網(wǎng)站,已為上家服務,為晉州各地企業(yè)和個人服務,聯(lián)系電話:18980820575
步驟1 : 將C1GridView綁定至數(shù)據(jù)源
***步是將C1GridView綁定到數(shù)據(jù)源。為了簡單起見,我們將其綁定到C1Nwind.mdb的Customers表。
步驟2 : 導出C1GridView 至Excel
導出到Excel需要分成兩步。***步是將GridView保存至一個HTML字符串。
Web控件有一個RenderControl()方法可以將服務器端控件的內(nèi)容輸出到指定的HtmlTextWriter對象。如果啟用了Tracing,該方法還將存儲控件的Trace信息。然后該HtmlTextWriter對象輸出到一個StringWriter 對象。
下面的方法被用來創(chuàng)建一個字符串:
- Public Function DataGridToExcel(ByVal dgExport As C1.Web.Wijmo.Controls.C1GridView.C1GridView) As String
- '創(chuàng)建一個stringwriter
- Dim stringWrite As New System.IO.StringWriter()
- '創(chuàng)建一個使用該stringwriter的htmltextwriter
- Dim htmlWrite As New System.Web.UI.HtmlTextWriter(stringWrite)
- Dim dg As C1.Web.Wijmo.Controls.C1GridView.C1GridView
- 'just set the input datagrid = to the new dg grid
- dg = dgExport
- '將header的字體加粗
- dg.HeaderStyle.Font.Bold = True
- '如果需要,這里是在組件級別改變顏色/格式
- dg.HeaderStyle.ForeColor = System.Drawing.Color.Black
- dg.RowStyle.ForeColor = System.Drawing.Color.Black
- '綁定修改后的datagrid
- '告訴datagrid將自己呈現(xiàn)到我們提供的htmltextwriter
- dg.AllowSorting = False
- dg.AllowPaging = False
- dg.AllowCustomPaging = False
- '新的代碼
- Dim parent As Control = dg.Parent
- parent.Controls.Remove(dg)
- dg.RenderControl(htmlWrite)
- '新的代碼
- parent.Controls.Add(dg)
- '輸出HTML
- Return stringWrite.ToString()
- End Function
下一步,我們將在一個Button Click事件中調用這個DownloadToExcel 方法從保存的字符串創(chuàng)建一個excel文件。
- Public Sub DownloadToExcel(ByVal content As String, ByVal response As HttpResponse)
- '清理 response.object
- response.Clear()
- response.Buffer = True
- response.Charset = ""
- '設置響應的MIME類型為excel
- response.ContentType = "application/vnd.ms-excel"
- response.ContentEncoding = New System.Text.UTF8Encoding()
- response.Write(content)
- response.End()
- End Sub
實現(xiàn)時的問題
在相當多的情況下,你會在導出時遇到一些錯誤。你可能會收到一條錯誤信息:“RegisterForEventValidation 只能在Render()過程中被調用;”。在這種情況下,請嘗試以下方法
1. 你可以向下面的文章描述的那樣,重載VerifyRenderingInServerForm 方法:
http://connect.microsoft.com/VisualStudio/feedback/details/118285/rendercontrol-doesnt-work-for-gridview
Public Overrides Sub VerifyRenderingInServerForm(control As Control)
End Sub
2. 為了避免收到“RegisterForEventValidation 只能在Render()過程中被調用;”異常,可以關閉Page.EnableEventValidation 或者將RenderControl方法調用放置在一個try-catch塊中。
此外,如果gridview包含一個復選框或者一個模板列,你會收到上面的錯誤。目前已發(fā)現(xiàn)微軟發(fā)布的GridView會發(fā)生同樣的錯誤。由于C1GridView繼承自微軟發(fā)布的GridView,所以它是C1GridView的已知設計問題。
下載示例
Wijmo下載,請進入Studio for ASP.NET Wijmo 2012 v1正式發(fā)布(2012.03.22更新)!
原文鏈接:http://www.cnblogs.com/powertoolsteam/archive/2012/04/25/2469717.html
責任編輯:張偉
來源: 葡萄城控件技術團隊的博客 jQuery
新聞名稱:jQuery之導出Wijmo的GridView到Excel
鏈接分享:http://m.fisionsoft.com.cn/article/dpiicji.html


咨詢
建站咨詢
