ASP. NET MVC uses Fileresult to export Excel data files

Source: Internet
Author: User

A few days ago a MVC3.0 project did an Excel export feature, which is recorded today. The simplest method is taken. Using the way HTML is stitched into table table, return fileresult output a binary file.

The first type: using Filecontentresult

        //        //Summary://Create a Filecontentresult object by using the file content, content type, file name//        //Parameters://filecontents://the contents of the binary file in response//        //ContentType://content Type (MIME type)//        //Filedownloadname://the file name that appears in the browser download window//        //return Result://The file content object.        protected Internal VirtualFilecontentresult File (byte[] filecontents,stringContentType,stringFiledownloadname);

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 parameter description same as Filecontentresult        //Parameters://FileStream://the flow of the response//         //return Result://The file stream object.        protected Internal VirtualFilestreamresult File (Stream fileStream,stringContentType,stringFiledownloadname);

Need to convert the contents of a file into a stream

        var New MemoryStream (filecontents);

The third type: using Filepathresult

        //Other parameter description same as Filecontentresult        //Parameters://FileName://file path of the response//        //return Result://The file stream object.       protected Internal VirtualFilepathresult File (stringFileName,stringContentType,stringFiledownloadname);

The Excel file must first be available on the server and will be returned by the Server.MapPath get path.

Please see the code for details.

Exportexcel Code
1          Publicfileresult exportexcel ()2         {3             varsbhtml =NewStringBuilder ();4Sbhtml.append ("<table border= ' 1 ' cellspacing= ' 0 ' cellpadding= ' 0 ' >");5Sbhtml.append ("<tr>");6             varLsttitle =Newlist<string> {"numbering","name","Age","creation Time" };7             foreach(varIteminchlsttitle)8             {9Sbhtml.appendformat ("<td style= ' font-size:14px;text-align:center;background-color: #DCE0E2; font-weight:bold; ' Height= ' >{0 }</td>", item);Ten             } OneSbhtml.append ("</tr>"); A  -              for(inti =0; I < +; i++) -             { theSbhtml.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>"); +             } ASbhtml.append ("</table>"); at  -             //The first type: Using Filecontentresult -             byte[] FileContents =Encoding.Default.GetBytes (sbhtml.tostring ()); -             returnFile (FileContents,"Application/ms-excel","Filecontents.xls"); -  -             //The second type: Using Filestreamresult in             varFileStream =NewMemoryStream (filecontents); -             returnFile (FileStream,"Application/ms-excel","Filestream.xls"); to  +             //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. the             varFileName = Server.MapPath ("~/files/filename.xls"); *             returnFile (FileName,"Application/ms-excel","Filename.xls"); $}

ASP. NET MVC uses Fileresult to export Excel data files

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.