偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

jQuery之導(dǎo)出Wijmo的GridView到Excel

開發(fā) 前端
在相當(dāng)多的情況下,你會在導(dǎo)出時遇到一些錯誤。你可能會收到一條錯誤信息:“RegisterForEventValidation 只能在Render()過程中被調(diào)用;”

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

步驟1 : C1GridView綁定至數(shù)據(jù)源

***步是將C1GridView綁定到數(shù)據(jù)源。為了簡單起見,我們將其綁定到C1Nwind.mdb的Customers表。

image

步驟2 : 導(dǎo)出C1GridView Excel

導(dǎo)出到Excel需要分成兩步。***步是將GridView保存至一個HTML字符串。

Web控件有一個RenderControl()方法可以將服務(wù)器端控件的內(nèi)容輸出到指定的HtmlTextWriter對象。如果啟用了Tracing,該方法還將存儲控件的Trace信息。然后該HtmlTextWriter對象輸出到一個StringWriter 對象。

下面的方法被用來創(chuàng)建一個字符串:

  1. Public Function DataGridToExcel(ByVal dgExport As C1.Web.Wijmo.Controls.C1GridView.C1GridView) As String  
  2.  
  3.     '創(chuàng)建一個stringwriter  
  4.  
  5.     Dim stringWrite As New System.IO.StringWriter()  
  6.  
  7.     '創(chuàng)建一個使用該stringwriter的htmltextwriter  
  8.  
  9.     Dim htmlWrite As New System.Web.UI.HtmlTextWriter(stringWrite)  
  10.  
  11.     Dim dg As C1.Web.Wijmo.Controls.C1GridView.C1GridView  
  12.  
  13.     'just set the input datagrid = to the new dg grid  
  14.  
  15.     dg = dgExport  
  16.  
  17.     '將header的字體加粗  
  18.  
  19.     dg.HeaderStyle.Font.Bold = True  
  20.  
  21.     '如果需要,這里是在組件級別改變顏色/格式  
  22.  
  23.     dg.HeaderStyle.ForeColor = System.Drawing.Color.Black  
  24.  
  25.     dg.RowStyle.ForeColor = System.Drawing.Color.Black  
  26.  
  27.     '綁定修改后的datagrid  
  28.  
  29.     '告訴datagrid將自己呈現(xiàn)到我們提供的htmltextwriter  
  30.  
  31.     dg.AllowSorting = False  
  32.  
  33.     dg.AllowPaging = False  
  34.  
  35.     dg.AllowCustomPaging = False  
  36.  
  37.     '新的代碼  
  38.  
  39.     Dim parent As Control = dg.Parent  
  40.  
  41.     parent.Controls.Remove(dg)  
  42.  
  43.     dg.RenderControl(htmlWrite)  
  44.  
  45.     '新的代碼  
  46.  
  47.     parent.Controls.Add(dg)  
  48.  
  49.     '輸出HTML  
  50.  
  51.     Return stringWrite.ToString()  
  52.  
  53. End Function 

image

下一步,我們將在一個Button Click事件中調(diào)用這個DownloadToExcel 方法從保存的字符串創(chuàng)建一個excel文件。

 
 
  1. Public Sub DownloadToExcel(ByVal content As String, ByVal response As HttpResponse)  
  2.  
  3. '清理 response.object  
  4.  
  5. response.Clear()  
  6.  
  7. response.Buffer = True  
  8.  
  9. response.Charset = "" 
  10.  
  11. '設(shè)置響應(yīng)的MIME類型為excel  
  12.  
  13. response.ContentType = "application/vnd.ms-excel" 
  14.  
  15. response.ContentEncoding = New System.Text.UTF8Encoding()  
  16.  
  17. response.Write(content)  
  18.  
  19. response.End()  
  20.  
  21. End Sub 

 image

實現(xiàn)時的問題

在相當(dāng)多的情況下,你會在導(dǎo)出時遇到一些錯誤。你可能會收到一條錯誤信息:“RegisterForEventValidation 只能在Render()過程中被調(diào)用;”。在這種情況下,請嘗試以下方法

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()過程中被調(diào)用;”異常,可以關(guān)閉Page.EnableEventValidation 或者將RenderControl方法調(diào)用放置在一個try-catch塊中。

此外,如果gridview包含一個復(fù)選框或者一個模板列,你會收到上面的錯誤。目前已發(fā)現(xiàn)微軟發(fā)布的GridView會發(fā)生同樣的錯誤。由于C1GridView繼承自微軟發(fā)布的GridView,所以它是C1GridView的已知設(shè)計問題。

下載示例 

Wijmo下載,請進(jìn)入Studio for ASP.NET Wijmo 2012 v1正式發(fā)布(2012.03.22更新)!

原文鏈接:http://www.cnblogs.com/powertoolsteam/archive/2012/04/25/2469717.html

責(zé)任編輯:張偉 來源: 葡萄城控件技術(shù)團(tuán)隊的博客
相關(guān)推薦

2011-05-31 15:56:03

Android Gridview

2022-12-29 08:49:40

SpringBootExcel

2020-09-22 09:41:09

前端

2025-05-06 01:00:00

Excel高性能內(nèi)存

2025-05-19 01:00:00

2009-07-27 15:58:25

添加GridView

2020-04-21 10:45:47

PythonWordExcel

2011-09-08 15:29:50

Android Wid界面GridView

2012-04-09 10:34:21

jQuery

2024-09-12 17:23:02

2025-02-12 07:27:52

MySQL8分頁查詢excel

2023-02-25 10:04:21

JavaExcel導(dǎo)出功能

2023-02-03 08:21:30

excelMySQL

2025-05-12 00:00:00

2009-12-04 16:49:38

PHP導(dǎo)出Excel亂

2009-09-07 14:41:48

GridView展開與

2021-03-11 08:24:48

Javapoi數(shù)據(jù)脫敏

2010-07-21 09:18:44

SQL Server

2021-04-20 09:32:15

Excel框架工具

2024-07-30 15:56:42

點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號