Document directory
- I. How to export an Excel table
- Ii. debugging of error messages
ASP. NET, while viewing data tables, most users want to export data table information as Excel and save it.
This document describes how to export Excel tables and how to debug error messages during debugging.
I. How to export an Excel table
There are two ways to export execl in Asp.net: one is to store the exported file under a folder on the server, and then output the file address to the browser; one is to directly write the file output stream to the browser. In response output, T-separated Data. When execl is exported, n is equivalent to line feed.
Some netizens wrote: "C # export data to excel summary" http://blog.csdn.net/bat800/archive/2007/07/17/1694537.aspx
The content is already detailed. This article makes some corrections to it. You can directly copy the code for testing.
A simple method is described here. The Code is as follows:
Public void dgtoexcel (system. web. UI. control CTL) <br/>{< br/> httpcontext. current. response. appendheader ("content-disposition", "attachment?filename=excel.xls"); <br/> httpcontext. current. response. charset = "UTF-8"; <br/> httpcontext. current. response. contentencoding = system. text. encoding. default; <br/> httpcontext. current. response. contenttype = "application/MS-excel"; <br/> CTL. page. enableviewstate = false; <br/> system. io. stringwriter Tw = new system. io. stringwriter (); <br/> system. web. UI. htmltextwriter hW = new system. web. UI. htmltextwriter (TW); <br/> CTL. rendercontrol (HW); <br/> httpcontext. current. response. write (TW. tostring (); <br/> httpcontext. current. response. end (); <br/>}
You can follow the dgtoexcel (this. gridview1); operation.
However, although the above method implements the export function, it also imports all the output information in HTML such as buttons and paging boxes. What we usually want to export is data on the DataGrid control, and if your DataGrid uses pagination, it exports the information of the current page, that is, it exports the information displayed in the DataGrid. Instead of all the information of your select statement. The following method helps you solve this problem:
Public void createexcel (Dataset ds, string filename) <br/>{< br/> httpresponse resp; <br/> resp = page. response; <br/> resp. contentencoding = system. text. encoding. getencoding ("GBK"); <br/> resp. appendheader ("content-disposition", "attachment; filename =" + filename); <br/> string colheaders = "", ls_item = ""; </P> <p> // define the table object and row object, and use dataset to initialize the value. <br/> system. data. datatable dt = Ds. tables [0]; <br/> datarow [] myrow = DT. select (); // It can be similar to DT. select ("ID> 10") to filter data <br/> int I = 0; <br/> int Cl = DT. columns. count; </P> <p> // obtain the titles of each column in the data table. Separate the headers with T and add a carriage return character after the last column title. <br/> for (I = 0; I <CL; I ++) <br/>{< br/> if (I = (Cl-1) // The last column, add/n <br/> {<br/> colheaders + = DT. columns [I]. caption. tostring () + "/N"; <br/>}< br/> else <br/> {<br/> colheaders + = DT. columns [I]. caption. tostring () + "/t"; <br/>}</P> <p >}< br/> resp. write (colheaders ); <br/> // write the obtained data information to the HTTP output stream </P> <p> // process data row by row <br/> foreach (datarow row in myrow) <br/>{< br/> // write data from the current row to the HTTP output stream, and leave ls_item empty for downstream data <br/> for (I = 0; I <CL; I ++) <br/>{< br/> if (I = (Cl-1) // The last column, add/n <br/>{< br/> ls_item + = row [I]. tostring () + "/N"; <br/>}< br/> else <br/> {<br/> ls_item + = row [I]. tostring () + "/t"; <br/>}</P> <p >}< br/> resp. write (ls_item); <br/> ls_item = ""; </P> <p >}< br/> resp. end (); <br/>}
You can follow createexcel (DS, "ex.xls.
Ii. debugging of error messages
Note: The control gridview1 of the gridview must be placed in the form tag with runat = server]
Solution:
(1) confirm that your gridview control is in form, and runat = "server" in Form"
(2) rewrite the following methods in the background code:
Public override void verifyrenderinginserverform (Control) <br/>{</P> <p>}
Prompt [You can only call registerforeventvalidation when executing render]
Solution:
(1) modify web. config (not recommended) <pages enableeventvalidation = "false"> </pages>
(2) modify the Page code:
<% @ Page Language = "C #" enableeventvalidation = "false" autoeventwireup = "true" </P> <p> codefile = "exportgridview. aspx. CS "inherits =" exportgridview "%> <br/>