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