Recommended export to excel files

Source: Internet
Author: User
In this formula, the content or result set to be exported is submitted to a B. ASPX page through a form on a page (such as a.htm.
On the ASPX page, an Excel file with a unique file name is generated and automatically output to the foreground. Saved by the front-end.
1.
A.htm page, you only need to submit the form to B. aspx. As for what you want to submit, put the hidden control in the form.
You can even dynamically change the value of the hidden control (this is useful ).
Keywords:
<Form method = "Post" Action = "B. aspx" target = "_ blank">
<Input type = "hidden" name = "textsql" value = "content to be submitted" id = "textsql"> </input>
</Form>

2.
Key issues in the background of the ASPX pageCode:


Using system. IO;
Using system. text;

// Receive the form value
Private void page_load (Object sender, system. eventargs E)
{

String deststring = request. Form ["textsql"]. tostring ();
Exporttofile (deststring );

}


// Save as an Excel file
Private void exporttofile (string esql)
{
// New A GUID
String filename = guid. newguid (). tostring () + ". xls"; // guid generated unique file name
String filetype = "application/MS-excel ";
Try
{
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 = filetype;
Stringbuilder ckpw = new stringbuilder (esql );
System. Io. stringwriter Tw = new system. Io. stringwriter (ckpw );
String filepath = server. mappath ("..") + "\" + filename;
System. Io. streamwriter Sw = system. Io. file. createtext (filepath );
Sw. Write (TW. tostring ());
Sw. Close ();

Downfile (httpresponse, filename, filepath );
Httpresponse. End ();
}
Catch (exception ee)
{
Throw EE;
}
}


// Output to the foreground
Private 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; // 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.