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