• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
老司机快发车
记录自己遇到的一些问题,以便后期查看
博客园    首页    新随笔    联系   管理    订阅  订阅

C# 导出Excel的几种简单方法

1. \t 格式导出

string reStr = "{0}\t{1}\t{2}";
StringWriter sw = new StringWriter();
sw.WriteLine(reStr, "车辆编号", "车牌号", "发动机号");
sw.Close();
string fileName = DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss")+ ".xls";
context.Response.Clear();
context.Response.Charset = "GB2312";
context.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
context.Response.ContentType = "application/ms-excel";
string browser = context.Request.Browser.Browser;
if (browser.Contains("InternetExplorer"))
     context.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName));
else
     context.Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
context.Response.Flush();
context.Response.Write(sw);
context.Response.End();

2. Html格式导出

string reStr = "<tr><td style=\"text-align: center;\">{0}</td><td style=\"text-align: center;\">{1}</td><td style=\"text-align: center;\">{2}</td><td style=\"text-align: center;\">{3}</td>"
StringWriter sw = new StringWriter();
sw.WriteLine(reStr.Replace("td","th"), "车辆编号", "车牌号", "发动机号", "底盘号");
sw.Close();
string fileName = DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss")+ ".xls";
context.Response.Clear();
context.Response.Charset = "GB2312";
context.Response.ContentEncoding = System.Text.Encoding.UTF8;
context.Response.ContentType = "application/ms-excel";
string browser = context.Request.Browser.Browser;
if (browser.Contains("InternetExplorer"))
     context.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName));
else
     context.Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
context.Response.Flush();
context.Response.Write(AddExcelHeader());
context.Response.Write(sw);
context.Response.Write(AddExcelFooter());
context.Response.End();

public string AddExcelHeader() 
{
    System.Text.StringBuilder sb = new System.Text.StringBuilder();
    sb.Append("<html xmlns:v=\"urn:schemas-microsoft-com:vml\" xmlns:o=\"urn:schemas-microsoft-com:office:office\"xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns=\"http://www.w3.org/TR/REC-html40\">");
    sb.Append("<head>");
    sb.Append("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"><meta name=\"ProgId\" content=\"Excel.Sheet\"/><meta name=\"Generator\" content=\"WPS Office ET\"/>");
    sb.Append("<!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name></x:Name><x:WorksheetOptions><x:Selected/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]-->");
    sb.Append("</head>");
    sb.Append("<body>");
    sb.Append("<div style=\"text-align: center;\">");
    sb.Append("<table>");
    return sb.ToString();
}

public string AddExcelFooter() 
{
    System.Text.StringBuilder sb = new System.Text.StringBuilder();
    sb.Append("</table>");
    sb.Append("</div>");
    sb.Append("</body>");
    sb.Append("</html>");
    return sb.ToString();
}

3. Html格式导出时设置Excel单元格格式

  1. 文本:vnd.ms-excel.numberformat:@
  2. 日期:vnd.ms-excel.numberformat:yyyy/mm/dd
  3. 数字:vnd.ms-excel.numberformat:#,##0.00
  4. 货币:vnd.ms-excel.numberformat:¥#,##0.00
  5. 百分比:vnd.ms-excel.numberformat: #0.00%

如:

 <td style=\"vnd.ms-excel.numberformat:@\">122414323543254354353</td>
posted @ 2021-05-06 14:49  老司机快发车  阅读(1569)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3