Export an Excel data file using Fileresult

Source: Internet
Author: User

Using the way HTML is stitched into table table, return fileresult output a binary file.

The first type: using Filecontentresult

Create a Filecontentresult object by using the file contents, content type, file name//parameters: FileContents: Binary contents of the response//      ContentType: Content Type (MIME type)//      Filedownloadname: The file name displayed in the browser download window////Returns the result: the file Content object. Protected internal virtual Filecontentresult file (byte[] FileContents, String ContentType, string filedownloadname);

Need to convert the contents of a file into a byte array byte[]

byte [] filecontents = Encoding.Default.GetBytes (sbhtml.tostring ());

The second type: using Filestreamresult

Other parameters describe the same filecontentresult//parameter: FileStream: The stream of the response//return result: File stream object. protected internal virtual Filestreamresult file (stream FileStream, String ContentType, string filedownloadname);

Need to convert the contents of a file into a stream

var fileStream = new MemoryStream (filecontents);

The third type: using Filepathresult

Other parameters describe the same filecontentresult//parameter: FileName: The file path of the response//return result: File stream object. protected internal virtual Filepathresult file (string FileName, String ContentType, string filedownloadname);
The Excel file must first be available on the server and will be returned by the Server.MapPath get path.

For details, see the code:

Exportexcel Code PublicFileresult Exportexcel () {varsbhtml =NewStringBuilder (); Sbhtml.append ("<table border= ' 1 ' cellspacing= ' 0 ' cellpadding= ' 0 ' >"); Sbhtml.append ("<tr>"); varLsttitle =Newlist<string> {"numbering","name","Age","creation Time" }; foreach(varIteminchlsttitle) {Sbhtml.appendformat ("<td style= ' font-size:14px;text-align:center;background-color: #DCE0E2; font-weight:bold; ' Height= ' >{0 }</td>", item); } sbhtml.append ("</tr>");  for(inti =0; I < +; i++) {sbhtml.append ("<tr>"); Sbhtml.appendformat ("<td style= ' font-size:12px;height:20px; ' >{0}</td>", i); Sbhtml.appendformat ("<td style= ' font-size:12px;height:20px; ' > Cock Wire {0} # </td>", i); Sbhtml.appendformat ("<td style= ' font-size:12px;height:20px; ' >{0}</td>",NewRandom (). Next ( -, -) +i); Sbhtml.appendformat ("<td style= ' font-size:12px;height:20px; ' >{0}</td>", DateTime.Now); Sbhtml.append ("</tr>"); } sbhtml.append ("</table>"); //The first type: Using Filecontentresult            byte[] FileContents =Encoding.Default.GetBytes (sbhtml.tostring ()); returnFile (FileContents,"Application/ms-excel","Filecontents.xls"); //The second type: Using Filestreamresult            varFileStream =NewMemoryStream (filecontents); returnFile (FileStream,"Application/ms-excel","Filestream.xls"); //The third type: Using Filepathresult//the Excel file must first be available on the server and will be returned by the Server.MapPath get path.            varFileName = Server.MapPath ("~/files/filename.xls"); returnFile (FileName,"Application/ms-excel","Filename.xls"); }

Export an Excel data file using Fileresult

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.