asp.net导出Excel显示中文乱码的解决方法
protected void btnexcel_click(object sender, eventargs e)
{
list<buflow.model.orderhistory> orderlist = dal.getorderlist2("");
string filename = "3g流量网龄升级计划用户表";
string name1 = filename;
string bname = common.myrequest.getbrowsername().tolower();
if (bname.contains("firefox"))
{
}
else if (bname.contains("ie"))
{
filename = httputility.urlencode(filename, system.text.encoding.utf8);
}
else
{
filename = httputility.urlencode(filename, system.text.encoding.utf8);
}
httpresponse resp = system.web.httpcontext.current.response;
resp.charset = "utf-8";
resp.clear();
resp.appendheader("content-disposition", "attachment;filename=" + filename + ".xls");
resp.contentencoding = system.text.encoding.utf8;
resp.contenttype = "application/ms-excel";
string style ="<meta http-equiv=\"content-type\" content=\"application/ms-excel; charset=utf-8\"/>"+ "<style> .table{ font: 9pt tahoma, verdana; color: #000000; text-align:center; background-color:#8ecbe8; }.table td{text-align:center;height:21px;background-color:#eff6ff;}.table th{ font: 9pt tahoma, verdana; color: #000000; font-weight: bold; background-color: #8ecbea; height:25px; text-align:center; padding-left:10px;}</style>";
resp.write(style);
//resp.write(exporttable(list));
resp.write("<table class='table'><tr><th>" +"手机"+ "</th><th>" + "渠道" + "</th><th>" +"时间" + "</th></tr>");
//dbvideolist = (list<subshiyongmodel>)session["dbvideolist"];
foreach (buflow.model.orderhistory model in orderlist)
{
resp.write("<tr><td>" + model.phone + "</td>");
resp.write("<td>" + model.qudao + "</td>");
resp.write("<td>" + model.tm + "</td>");
resp.write("</tr>");
}
resp.write("<table>");
resp.flush();
resp.end();
}
需要注意的是编码的问题,在输出的时候,最好加上以下语句:
<meta http-equiv="content-type" content="application/ms-excel; charset=gb2312"/>
上一篇: Python实现扩展内置类型的方法分析