JQuery UI component set with better Wijmo: export the Wijmo's GridView to Excel

Source: Internet
Author: User

Wijmo GridViewWidgetYou cannot export an Excel file. This blog introduces a simple method to save the Wijmo GridView control to Excel. You can use the same method on C1 WebUI GridView.

 

Procedure1:SetC1GridViewBind to Data Source

The first step is to bind C1GridView to the data source. For simplicity, we bind it to the c1mers table of C1Nwind. mdb.

650) this. width = 650; "style =" border-right-0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px "title =" image "border =" 0 "alt =" image "src =" http://www.bkjia.com/uploads/allimg/131228/132T42W1-0.png "width =" 896 "height =" 330 "/>

 

Procedure2:ExportC1GridViewToExcel

Two steps are required to export data to Excel. The first step is to save the GridView to an HTML string.

The Web control hasRenderControl ()Method to output the content of the server-side control to the specifiedHtmlTextWriterObject. If Tracing is enabled, this method also stores the Trace information of the control. ThenHtmlTextWriterOutput object toStringWriterObject.

The following method is used to create a string:

 

 
 
  1. Public Function DataGridToExcel (ByVal dgExport As C1.Web. Wijmo. Controls. C1GridView. C1GridView) As String
  2.  
  3. 'Create a stringwriter
  4.  
  5. Dim stringWrite As New System. IO. StringWriter ()
  6.  
  7. 'Create an htmltextwriter that uses the stringwriter
  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. 'Bold the header font
  18.  
  19. Dg. HeaderStyle. Font. Bold = True
  20.  
  21. 'If needed, the color/format is changed at the component level.
  22.  
  23. Dg. HeaderStyle. ForeColor = System. Drawing. Color. Black
  24.  
  25. Dg. RowStyle. ForeColor = System. Drawing. Color. Black
  26.  
  27. 'Bind the modified datagrid
  28.  
  29. 'Tells the datagrid to present itself to the htmltextwriter we provide.
  30.  
  31. Dg. AllowSorting = False
  32.  
  33. Dg. AllowPaging = False
  34.  
  35. Dg. AllowCustomPaging = False
  36.  
  37. 'New code
  38.  
  39. Dim parent As Control = dg. Parent
  40.  
  41. Parent. Controls. Remove (dg)
  42.  
  43. Dg. RenderControl (htmlWrite)
  44.  
  45. 'New code
  46.  
  47. Parent. Controls. Add (dg)
  48.  
  49. 'Output HTML
  50.  
  51. Return stringWrite. ToString ()
  52.  
  53. End Function

 

 

650) this. width = 650; "style =" border-right-0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px "title =" image "border =" 0 "alt =" image "src =" http://www.bkjia.com/uploads/allimg/131228/132T45021-1.png "width =" 897 "height =" 157 "/>

 

Next, we will callDownloadToExcelMethod To create an excel file from the saved string.

 

 
 
  1. Public Sub DownloadToExcel (ByVal content As String, ByVal response As HttpResponse)
  2.  
  3. 'Clear response. object
  4.  
  5. Response. Clear ()
  6.  
  7. Response. Buffer = True
  8.  
  9. Response. Charset = ""
  10.  
  11. 'Set the response MIME type to 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

 

650) this. width = 650; "style =" border-right-0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px "title =" image "border =" 0 "alt =" image "src =" http://www.bkjia.com/uploads/allimg/131228/132T42L6-2.png "width =" 604 "height =" 404 "/>

 

Implementation problems

In many cases, you may encounter some errors during export. You may receive an error message: "RegisterForEventValidation can only be called during Render ;". In this case, try the following methods:


1. You can reload the VerifyRenderingInServerForm method as described in the following article:

Http://connect.microsoft.com/VisualStudio/feedback/details/118285/rendercontrol-doesnt-work-for-gridview

Public Overrides Sub VerifyRenderingInServerForm (control As Control)

End Sub

 

2. To avoid the error that "RegisterForEventValidation can only be called during Render ();", disable Page. EnableEventValidation or place the RenderControl method call in a try-catch Block.

In addition, if the gridview contains a check box or a template column, you will receive the above error. Currently, the same error occurs in Microsoft's GridView. Because C1GridView inherits from Microsoft's GridView, it is a known design issue of C1GridView.

 

Download example

 

Download Wijmo. Please goStudio for ASP. NET Wijmo 2012 v1 officially released 2012.03.22 )!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.