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;
}
}