Here, we use the method of converting an Excel file from the server and then downloading it using the file address.
For how to generate an Excel file, see [original]. Net to create an Excel file (insert data, modify the format, and generate a chart ).
First try the Response. WriteFile method:
Copy codeThe Code is as follows:
FileInfo fi = new FileInfo (excelFile); // excelFile is the address of the file on the server.
HttpResponse contextResponse = HttpContext. Current. Response;
ContextResponse. Clear ();
ContextResponse. Buffer = true;
ContextResponse. Charset = "GB2312"; // set the data type to Chinese to prevent garbled characters.
ContextResponse. AppendHeader ("Content-Disposition", String. Format ("attachment; filename = {0}", excelName); // defines the output file and file name
ContextResponse. AppendHeader ("Content-Length", fi. Length. ToString ());
ContextResponse. ContentEncoding = Encoding. Default;
ContextResponse. ContentType = "application/ms-excel"; // set the output file type to excel.
ContextResponse. WriteFile (fi. FullName );
ContextResponse. Flush ();
ContextResponse. End ();
The Excel file in the first line is the address of the Excel file on the server, for example, "C: \ Website \ Excel \ xx.xlsx ".
This method is also commonly provided on the Internet, but in actual operations, there is a problem of inintention:
In Chrome
Everything works. the Excel file is directly downloaded to the default download folder of Chrome.
In Firefox
Because the FlashGot plug-in is installed, the application download tool is selected first:
The display is normal. If you select "save file", the Excel file will also be saved to the default folder. However, if you try a third-party download tool, such as thunder, the following window will appear:
Note that in the URL column, ViewState information is added after the actual address of the page, and the saved name is not the name of the Excel file, but the name of the page.
After clicking confirm, the downloaded file is converted into a local file (the file will first be changed to a. ZIP file and then to an actual file)
Under IE7
The "save" dialog box will pop up first. The file is normal. Similarly, when you click "save", the "Download" dialog box will pop up. Unlike Firefox, there is no ViewState information at the end of the URL.
Click OK to download the page file:
If you click Cancel in the Download Dialog Box of thunder, IE will be used for download. The file here is correct again:
It is suspected that thunder re-Requests the download Based on the URL in the download dialog box. It has nothing to do with the page initiating the request, and IE does not transmit the ViewState information to thunder, the downloaded file is not an Excel page.
Later, I tried the multipart download method, which is actually invalid, because thunder does not care about the download mechanism you provided to it, and when I call thunder in Firefox, because the Viewstate of multipart download does not contain the complete information of the Excel file, the downloaded Viewstate is also incomplete.
Finally, we can only use the oldest solution: Response. Redirect () to switch to the actual file address.
Copy codeThe Code is as follows:
FileInfo fi = new FileInfo (excelFile );
HttpResponse contextResponse = HttpContext. Current. Response;
ContextResponse. Redirect (string. Format ("~ /Template/{0} ", excelName), false );
In this way, the test is normal in three browsers, because the actual file address is requested, and the actual file address is displayed in thunder. Download will not cause problems. However, this is equivalent to notifying the client of the actual address of the file, which is not private. But fortunately, this does not require good privacy, and the file will be deleted after a certain period of time, so it is not a big problem.
The above is the result of my first consideration. It seems that I am still a little lazy ......
Afterwards, since every time thunder actually re-Requests the URL, we should input a URL for thunder to generate an Excel file.
That is, when you click the "generate Excel" button, go to another Export page and complete the steps of generating an Excel file and downloading an Excel file in the Page_Load Method on this page.
Copy codeThe Code is as follows:
String fileName = Request. QueryString ["FileName"];
String exportName = Request. QueryString ["Export"];
If (fileName! = Null)
{
ExportManger. CreateExcel (fileName); // create an Excel file on the server first.
Response. Redirect (String. Format ("{0 }? Export = {1} ", Request. Path. ToString (), fileName); // redirect to this page, but the Query parameter is changed to Export.
}
Else if (exportName! = Null)
{
ExportManger. ExportExcel (exportName); // download the Excel file.
}
The page jumps twice. The first is to generate an Excel file and the second is to download an Excel file.
Two jumps are made because thunder captures the final URL. If the URL is generated and downloaded together, Thunder will regenerate an Excel file during the download. The code for downloading the Excel file ExportManger. ExportExcel (exportName) uses the Response. Write method introduced at the beginning of this article. You can also use the multipart download method:
Copy codeThe Code is as follows:
If (fi. Length> 0)
{
FileStream sr = new FileStream (fi. FullName, System. IO. FileMode. Open, System. IO. FileAccess. Read, System. IO. FileShare. Read );
Int size = 1024; // set the length of each read.
For (int I = 0; I <fi. Length/size + 1; I ++)
{
Byte [] buffer = new byte [size];
Int length = sr. Read (buffer, 0, size );
ContextResponse. OutputStream. Write (buffer, 0, length );
}
Sr. Close ();
}
Else
{
ContextResponse. WriteFile (fi. FullName );
}
Here, only one Excel file is generated and retained on the server. You can use the "Export" parameter to download the same file each time you download the file. If you only need a one-time file, and each download needs to be regenerated, you only need to download and generate the Export page. Then, the Response. Write method at the beginning is changed:
Copy codeThe Code is as follows:
ContextResponse. Flush ();
Fi. Delete ();
ContextResponse. End ();
That is, after each response is cleared, the file is deleted first and then the response is ended. In this way, the downloading tool cannot be used, the privacy of the Server File address is protected, and the multipart write method can be used to write large files, in addition, the generated files can be deleted as needed without occupying server space.