接着上面的来,继续导出Execl 的功能

使用FileResult 方式直接可以生产Execl ,这样我们将会写大量处理后台的代码,个人感觉不好,只是展示出来,提供参考

第一步:编辑控制器

 public FileResult ExportFile()
{
var list = GetList(); var sbHtml = new StringBuilder();
string title = "order";
//标题
sbHtml.Append("<table border='1' cellspacing='0' cellpadding='0'>");
sbHtml.Append("<tr>");
sbHtml.AppendFormat("<td style='font-size: 14px;text-align:center; font-weight:bold;' height='30' colspan='3'>{0}</td>", title);
sbHtml.Append("</tr>");
sbHtml.Append("</table>");
sbHtml.Append("<table border='1' cellspacing='0' cellpadding='0' style='font-size: 12px;'>");
var lstTitle = new List<string> { "Year", "Week", "Date" }; //编写表头
sbHtml.Append("<tr>");
foreach (var item in lstTitle)
{
sbHtml.AppendFormat("<td style='text-align:center;background-color: #DCE0E2; font-weight:bold;' height='25'>{0}</td>", item);
}
sbHtml.Append("</tr>");
int i = ; var strDataType = string.Empty;
var strQuestion = string.Empty; foreach (var item in list)
{ sbHtml.Append("</tr>");
sbHtml.AppendFormat("<td align='center'>{0}</td><td align='center'>{1}</td><td align='center'>{2}</td>", item.Id, item.Name,item.CreateTime.ToString("yyyy-MM-dd hh:mm:ss") );
sbHtml.Append("</tr>");
i++; }
sbHtml.Append("</table>"); byte[] fileContents = Encoding.Default.GetBytes(sbHtml.ToString()); string fileName = title + ".xls"; if (Request.Browser.Browser == "IE")
{
Response.Charset = "GB2312";
Response.ContentEncoding = Encoding.GetEncoding("GB2312");
Response.AddHeader("Content-Disposition", string.Format("attachment; filename=\"{0}\"", HttpUtility.UrlEncode(fileName, Encoding.UTF8)));
}
return File(fileContents, "application/ms-excel", fileName);
}

第二步:前端调用

  function ExportFile() {

        window.open("/Home/ExportFile");

    }

OK ,这样就好 控制器里面的代码也是挺简单,自己看看吧

最新文章

  1. Bootstrap&lt;基础十七&gt;导航栏
  2. D3树状图异步按需加载数据
  3. block使用小结、在arc中使用block、如何防止循环引用
  4. xcode Git
  5. Linux init进程详解
  6. javascript防止SQL注入
  7. 如何实现一个c/s模式的flv视频点播系统
  8. leetcode problem 41 -- First Missing Positive
  9. DSPack各种使用方法
  10. nginx 编译选项
  11. (转).NET平台开源JSON库LitJSON的使用方法
  12. (转)Javascript 面向对象编程(一):封装(作者:阮一峰)
  13. elasticsearch-head的安装
  14. vue 之 .sync 修饰符
  15. 5.06-re
  16. MinFilter(MaxFilter)快速算法C++实现
  17. IDEA复制某个类的包名路径
  18. 物联网架构成长之路(10)-Nginx负载均衡
  19. 使用maven构建一个web项目
  20. c#FTP应用---FileZilla Server

热门文章

  1. Centos7 自定义systemctl服务脚本
  2. STM32-增量式旋转编码器测量
  3. Python3 操作系统与路径 模块(os / os.path / pathlib)
  4. 《Implementing QuantLib》译后记
  5. 部署一个flask服务记录
  6. Python爬虫之XML
  7. Linux系统编程:线程控制
  8. Elastic-Job源码分析之JobScheduler类分析
  9. 命令行下 初识 redis 入门教程
  10. mysql去除重复记录案例