Code
Private void button#click (object sender, System. EventArgs e)
{
// Export Excel
Response. Clear ();
Response. Buffer = true;
Response. AppendHeader ("Content-Disposition", "attachment?filename=.xls"); // defines the output file and file name
Response. ContentType = "application/vnd. ms-excel ";
Response. Charset = "GB2312 ";
Response. ContentEncoding = System. Text. Encoding. GetEncoding ("GB2312 ");
This. EnableViewState = false;
StringWriter sw = new StringWriter ();
HtmlTextWriter htw = new HtmlTextWriter (sw );
// This. SpanProjInfo. RenderControl (htw );
Response. Write (sw. ToString ());
Response. End ();
//---------------------------
System. Web. HttpResponse httpResponse = Page. Response;
HttpResponse. appendHeader ("Content-Disposition", "attachment; filename =" + HttpUtility. urlEncode (this. hid_reportname.Value, System. text. encoding. UTF8) + DateTime. now. toString ("yyyyMMdd") + ". xls ");
HttpResponse. ContentEncoding = System. Text. Encoding. GetEncoding ("GB2312 ");
HttpResponse. ContentType = "application/vnd. ms-excel ";
System. IO. StringWriter tw = new System. IO. StringWriter ();
System. Web. UI. HtmlTextWriter hw = new System. Web. UI. HtmlTextWriter (tw );
This. SpanProjInfo. RenderControl (hw );
String filePath2 = Request. PhysicalApplicationPath + "Download \ xx.xls ";
System. IO. StreamWriter sw = System. IO. File. CreateText (filePath2 );
Sw. Write (tw. ToString ());
Sw. Close ();
DownFile (httpResponse, "ttt", filePath2 );
HttpResponse. End ();
}
Private void btnExcel_ServerClick (object sender, System. EventArgs e)
{
// Export Excel
Response. Clear ();
Response. Buffer = true;
Response. AppendHeader ("Content-Disposition", "attachment; filename =" + this. hid_reportname.Value); // defines the output file and file name
Response. ContentType = "application/vnd. ms-excel ";
Response. Charset = "GB2312 ";
Response. ContentEncoding = System. Text. Encoding. GetEncoding ("GB2312 ");
This. EnableViewState = false;
StringWriter sw = new StringWriter ();
HtmlTextWriter htw = new HtmlTextWriter (sw );
// This. SpanProjInfo. RenderControl (htw );
Response. Write (SpanProjInfo. InnerHtml. ToString ());
Response. End ();
}
Public 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) + DateTime. Now. ToString ("yyyyMMdd") + ". xls; charset = GB2312 ");
System. IO. FileStream fs = System. IO. File. OpenRead (fullPath );
Long fLen = fs. Length;
Int size = 102400; // download data at the same time every k
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); // Read a compressed Block
Response. BinaryWrite (readData );
FPos + = size;
}
Fs. Close ();
System. IO. File. Delete (fullPath );
Return true;
}
Catch
{
Return false;
}
}