zoukankan      html  css  js  c++  java
  • 导出CSV

    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");
    }

    js的代码:
    window.location.href = "Export"; 

    后台代码:
    1
    2
    3
    4
    public FileResult Export()
    {
        return File(Server.MapPath("/Data/Export.xls"), "application/ms-excel""Export.xls");
    }

     

  • 相关阅读:
    实现Maven自动下载源代码包并关联
    Maven3入门篇
    小典故:为什么数组的索引总是从0开始,而不是1?
    C语言算法探究之(一):算法的准确性
    C语言算法探究之(二):算法的准确性
    Visual Studio对无用引用(unused using)的处理方法
    C# CRC8的实现(原创)
    C#4.0:新功能和展望
    C#控件重绘学习(一)
    双加号(++)在C#中的用法解释
  • 原文地址:https://www.cnblogs.com/zhangxiaolei521/p/5286160.html
Copyright © 2011-2022 走看看