Recently, I have been exporting some data about the gridview set to an Excel or Word file.Code. To facilitate future reading and learning.
1. Export Excel:
Public
Void Export ()
{
String filename = "file name ";
System. Web. httpresponse = page. response;
httpresponse. appendheader ("content-disposition",
"attachment; filename =" +
httputility. urlencode (filename,
system. text. encoding. utf8);
httpresponse. contentencoding =
system. text. encoding. getencoding ("gb2312");
httpresponse. contenttype = "application/MS-excel";
system. io. stringwriter Tw = new system. io. stringwriter ();
system. web. UI. htmltextwriter hW = new
system. web. UI. htmltextwriter (TW);
This. gridview_selec.allowpaging = false;
If (viewstate ["ds"]! = NULL)
{
Gridview_selec.datasource = viewstate ["ds"];
Gridview_selec.databind ();
If (gridview_selec.rows.count> 0)
{
If (gridview_selec.headerrow! = NULL)
{
This. gridview_selec.headerrow.cells.removeat (gridview_selec.headerrow.cells.count
-1 );
This. gridview_selec.headerrow.cells.removeat (gridview_selec.headerrow.cells.count
-1 );
For (INT I = 0; I <gridview_selec.rows.count;
I ++)
{
Gridview_selec.rows [I]. cells. removeat (gridview_selec.rows [I]. cells. Count
-1 );
Gridview_selec.rows [I]. cells. removeat (gridview_selec.rows [I]. cells. Count
-1 );
}
}
}
}
Gridview_selec.rendercontrol (HW );
String filepath = page. server. mappath ("..") + "\ temp \" + filename;
System. Io. streamwriter Sw =
System. Io. file. createtext (filepath );
Sw. Write (TW. tostring ());
Sw. Close ();
Downfile (httpresponse, filename, filepath );
Httpresponse. End ();
Gridview_selec.allowpaging = true;
Gridview_selec.datasource = viewstate ["ds"];
Gridview_selec.databind ();
}
Private
Static bool downfile (system. Web. httpresponse response, string
Filename, string fullpath)
{
Try
{
Response. contenttype = "application/octet-stream ";
Response. appendheader ("content-disposition", "attachment; filename ="
+
Httputility. urlencode (filename, system. Text. encoding. utf8) +
"; Charset = gb2312 ");
System. Io. filestream FS = system. Io. file. openread (fullpath );
Long FLEN = FS. length;
Int size = 102400;
Byte [] readdata = new byte [size]; // specify the buffer size
If (size> FLEN) size = convert. toint32 (FLEN );
Long FPOs = 0;
Bool isend = false;
While (! Isend)
{
If (FPOs + size)> FLEN)
{
Size = convert. toint32 (FLEN-FPOs );
Readdata = new byte [size];
Isend = true;
}
FS. Read (readdata, 0, size );
Response. binarywrite (readdata );
FPOs + = size;
}
FS. Close ();
System. Io. file. Delete (fullpath );
Return true;
}
Catch
{
Return false;
}
}
2. Export word:
Modify the following code to use: httpresponse. contenttype =
"Application/MS-excel ";
Changed to: httpresponse. contenttype = "application/MS-word ";
3. Export the TXT plain text format:
Public void exporttxt ()
{
String filename = "Temp ";
Response. Clear ();
Response. Buffer = false;
Response. contentencoding = system. Text. encoding. utf8;
Response. appendheader ("content-disposition", "attachment; filename ="
+ Server. urlencode (filename) + ". txt ");
Response. contenttype = "text/plain ";
This. enableviewstate = false;
String STR = "";
// You Need To iterate a data set and save it to the string variable
If (viewstate ["ds"]! = NULL)
{< br>
dataset DS = (Dataset) viewstate ["ds"];
for (INT m = 0; m M ++)
{< br>
STR + = Ds. tables [0]. columns [M]. columnname + "\ t";
}
for (INT I = 0; I I ++)
{< br>
for (Int J = 0; j J ++)
{< br>
STR + = Ds. tables [0]. rows [I] [J]. tostring () + "\ t";
}< br>
STR + = "\ r \ n ";
}
Response. Write (STR );
Response. End ();
}
Tip: If the exported data has a string starting with 0, you need to make the following settings:
Protected
Void gridview_selec_rowdatabound (Object sender,
Gridviewroweventargs E)
{
If (E. Row. rowtype = datacontrolrowtype. datarow)
{
// Here, The 0th column of gridview1 is the string to be protected
E. Row. cells [0]. Attributes. Add ("style ",
"Vnd. ms-excel.numberformat :@");
}
}
If the page for data export is placed in the master page or nested under other frameworks, You need to override the following method.
Method of rewriting when exporting Excel
Public
Override void verifyrenderinginserverform (Control)
{
}
Note: Some codes are searched online.