(Original) excel Export

Source: Internet
Author: User
Tags urlencode
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;
}
}

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.