• 使用FileResult导出Excel数据文件


    用的是Html拼接成Table表格的方式,返回 FileResult 输出一个二进制的文件.

    第一种:使用FileContentResult

    // 通过使用文件内容,内容类型,文件名称创建一个FileContentResult对象
    // 参数:fileContents:响应的二进制文件内容
    //      contentType:内容类型(MIME类型)
    //      fileDownloadName:显示在浏览器下载窗口的文件名称//
    // 返回结果:文件内容对象.
    protected internal virtual FileContentResult File(byte[] fileContents, string contentType, string fileDownloadName);

    需要将文件内容转化成字节数组byte[]

    byte[] fileContents = Encoding.Default.GetBytes(sbHtml.ToString());

    第二种:使用FileStreamResult

    // 其他参数描述同FileContentResult
    // 参数:fileStream:响应的流
    // 返回结果:文件流对象.
    protected internal virtual FileStreamResult File(Stream fileStream, string contentType, string fileDownloadName);
    

     需要将文件内容转化成流

    var fileStream = new MemoryStream(fileContents);
    

    第三种:使用FilePathResult

    // 其他参数描述同FileContentResult
    // 参数:fileName:响应的文件路径
    // 返回结果:文件流对象.
    protected internal virtual FilePathResult File(string fileName, string contentType, string fileDownloadName);
    
    服务器上首先必须要有这个Excel文件,然会通过Server.MapPath获取路径返回.

    具体详情请看代码:

    ExportExcel Code 
            public FileResult ExportExcel()
            {
                var sbHtml = new StringBuilder();
                sbHtml.Append("<table border='1' cellspacing='0' cellpadding='0'>");
                sbHtml.Append("<tr>");
                var lstTitle = new List<string> { "编号", "姓名", "年龄", "创建时间" };
                foreach (var item in lstTitle)
                {
                    sbHtml.AppendFormat("<td style='font-size: 14px;text-align:center;background-color: #DCE0E2; font-weight:bold;' height='25'>{0}</td>", item);
                }
                sbHtml.Append("</tr>");
    
                for (int i = 0; i < 1000; 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;'>屌丝{0}号</td>", i);
                    sbHtml.AppendFormat("<td style='font-size: 12px;height:20px;'>{0}</td>", new Random().Next(20, 30) + i);
                    sbHtml.AppendFormat("<td style='font-size: 12px;height:20px;'>{0}</td>", DateTime.Now);
                    sbHtml.Append("</tr>");
                }
                sbHtml.Append("</table>");
    
                //第一种:使用FileContentResult
                byte[] fileContents = Encoding.Default.GetBytes(sbHtml.ToString());
                return File(fileContents, "application/ms-excel", "fileContents.xls");
    
                //第二种:使用FileStreamResult
                var fileStream = new MemoryStream(fileContents);
                return File(fileStream, "application/ms-excel", "fileStream.xls");
    
                //第三种:使用FilePathResult
                //服务器上首先必须要有这个Excel文件,然会通过Server.MapPath获取路径返回.
                var fileName = Server.MapPath("~/Files/fileName.xls");
                return File(fileName, "application/ms-excel", "fileName.xls");
            }
  • 相关阅读:
    OpenCV下载
    ffmpeg.转换示例_处理流程
    调试.pdb&源码
    OpenCV.图片透明
    ffmpeg.用vs2017编译(20200102)
    ffmpeg.视频转码
    ffmpeg.串流_资料(包含一些vlc)
    Qt563x86vs2015.编译错误(TypeError: Property 'asciify' of object Core::Internal::UtilsJsExtension(0x????????) is not a function)
    前端性能优化规则
    前端性能优化 —— reflow(回流)和repaint(重绘)
  • 原文地址:https://www.cnblogs.com/sky-net/p/4700555.html
Copyright © 2020-2023  润新知