Asp.net xls file download function instance

Source: Internet
Author: User

Asp tutorial. net xls file download function example
Provides two asp.net tutorial c files to download the instance code. If you directly connect to the xls file, it will be opened in the browser. Now we use. net to download the instance.
*/

Private void page_load (object sender, system. eventargs e)
{
If (! Page. ispostback)
{
String filepath = "d: www.bKjia.c0m.xls ";
// Filepath = "d: test.xls ";
System. io. filestream fs = system. io. file. openread (filepath );
Byte [] filedata = new byte [fs. length];
Fs. read (filedata, 0, (int) (fs. length ));
Fs. close ();

Response. addheader ("content-type", "application/vnd. ms-excel ");

String saveasfilename = "yoursaveasfilename ";
Saveasfilename = encode (saveasfilename );
// Saveasfilename = httputility. urldecode (saveasfilename, system. text. encoding. getencoding ("shift-jis "));
Response. addheader ("content-disposition", "inline; filename =" + saveasfilename );

Response. addheader ("content-length", filedata. length. tostring ());
Response. binarywrite (filedata );
Response. end ();
}
}

// Method 2

Public static void downloadfile (string filepath)
{
String str = httpcontext. current. request. servervariables ["appl_physical_path"];
String path = str + "www. bKjia. c0m" + filepath;
Fileinfo file = new fileinfo (path );
Httpcontext. current. response. contentencoding = system. text. encoding. getencoding ("UTF-8"); // solves Chinese garbled characters
Httpcontext. current. response. addheader ("content-disposition", "attachment; filename =" + httpcontext. current. server. urlencode (file. name); // solves Chinese file name garbled characters
Httpcontext. current. response. addheader ("content-length", file. length. tostring ());
Httpcontext. current. response. contenttype = "appliction/octet-stream ";
Httpcontext. current. response. writefile (file. fullname );
Httpcontext. current. response. end ();
}

 

Related Article

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.