zoukankan      html  css  js  c++  java
  • 将table导出为Excel的标准无乱码写法

    导出为Excel有很多种写法,对于一些复杂的格式,笔者喜欢在后台先拼成一个<table>,再使用Response输出。

    如果数据中包含中文或者一些特殊字符,可很多不规范的写法都会导致页面乱码,这里就把一种(笔者认为)最标准的格式带给大家:

    Page p = HttpContext.Current.Handler as Page;
    p.Response.Clear();
    p.Response.Buffer = true;
    p.Response.Charset = "UTF-8";
    p.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(filename, Encoding.UTF8) + ".xls");
    p.Response.ContentEncoding = Encoding.UTF8;
    p.Response.ContentType = "application/vnd.ms-excel";
    p.EnableViewState = false;
    p.Response.Write("<html><head><meta http-equiv='Content-Type' content='application/vnd.ms-excel; charset=utf-8' /></head>" + text + "</html>");
    p.Response.End();


    以上代码需注意两点:

    1)Charset/AppendHeader/ContentEncoding都必须统一使用UTF8

    2)text里面如果只有<table>,必须在前后加上<html></html>.为进一步规范,建议用以上代码格式编写

    3)为防止出现“4340611210891797”变为“4.340611210e+017”,请加入不作自动转换的样式<table style='vnd.ms-excel.numberformat:@'>……</table>

    好了,现在应该是无论如何你怎么导出Excel,也没有乱码字符了。

  • 相关阅读:
    kmp学习笔记(模板)
    最小表示法 (模板)
    Codeforces 1339C
    Codeforces 1339D
    Codeforces 1244C
    Codeforces 1262D2
    Codeforces 1330D
    Problem M. Mediocre String Problem
    Codeforces 1326D2
    selenium读取数据文件
  • 原文地址:https://www.cnblogs.com/kandyvip/p/3890714.html
Copyright © 2011-2022 走看看