.netCore中Response.WriteAsync()输出乱码
2021-02-20 04:41
标签:hosting startup tor 图片 function 其他 border contex ima
1.输出中文,导出到excel的代码(System.Web.HttpContext 自己注入) 2.问题: 因为在Core中无法使用 1.先在NuGet中下载 System.Text.Encoding.CodePages包 2.在Startup中或者main方法中,在这儿 我就放在 Startup 中了 3.在上述方法中加上 System.Web.HttpContext.Current.Response.WriteAsync(Encoding.GetEncoding(“GB2312”).ToString()); 4.结果 暂时之能这样了。 求教没有其他更好的方法呢(不使用第三方导出插件,就用输出的这种)? .netCore中Response.WriteAsync()输出乱码 标签:hosting startup tor 图片 function 其他 border contex ima 原文地址:https://www.cnblogs.com/lonelyxmas/p/12922710.html string rt = @"
table border=‘1‘ width=‘100%‘ style=‘color : green‘>
tr height=‘20‘ align=‘center‘ style=‘font-weight : bold ‘ >td>哈哈/td>td>三个字/td>td>浏览数(pv)/td>td>访客数(uv)/td>td>已过数(已经缴费)/td>td>收益/td>/tr>
tr height=‘20‘ align=‘center‘ >td>a/td>td>公开/td>td>0/td>td>1/td>td>0/td>td>¥0.00/td>/tr>
tr height=‘20‘ align=‘center‘ >td>f/td>td>公开/td>td>3/td>td>1/td>td>0/td>td>¥0.00/td>/tr>
tr height=‘20‘ align=‘center‘ >td>g/td>td>公开/td>td>2/td>td>1/td>td>0/td>td>¥0.00/td>/tr>
tr height=‘20‘ align=‘center‘ >td>h/td>td>公开/td>td>45/td>td>1/td>td>0/td>td>¥0.00/td>/tr>
/ table >";
string fileName = System.Web.HttpUtility.UrlPathEncode("视讯.xls");
System.Web.HttpContext.Current.Response.Headers.Add("Content-Type", "application/ms-excel; charset=UTF-8");
System.Web.HttpContext.Current.Response.Headers.Add("Content-Disposition", "attachment;filename=\"" + fileName + "\"");
System.Web.HttpContext.Current.Response.WriteAsync(rt);
百度了很多内容大部分都是针对Core控制台输出中文乱码的解决方案
System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding(“GB2312”);
很多表头都只能使用, System.Web.HttpContext.Current.Response.Headers.Add(),来加上暂时解决方法
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);//注册字体GB2312 来源于:System.Text.Encoding.CodePages
}
string rt = @"
table border=‘1‘ width=‘100%‘ style=‘color : green‘>
tr height=‘20‘ align=‘center‘ style=‘font-weight : bold ‘ >td>哈哈/td>td>三个字/td>td>浏览数(pv)/td>td>访客数(uv)/td>td>已过数(已经缴费)/td>td>收益/td>/tr>
tr height=‘20‘ align=‘center‘ >td>a/td>td>公开/td>td>0/td>td>1/td>td>0/td>td>¥0.00/td>/tr>
tr height=‘20‘ align=‘center‘ >td>f/td>td>公开/td>td>3/td>td>1/td>td>0/td>td>¥0.00/td>/tr>
tr height=‘20‘ align=‘center‘ >td>g/td>td>公开/td>td>2/td>td>1/td>td>0/td>td>¥0.00/td>/tr>
tr height=‘20‘ align=‘center‘ >td>h/td>td>公开/td>td>45/td>td>1/td>td>0/td>td>¥0.00/td>/tr>
/ table >";
string fileName = System.Web.HttpUtility.UrlPathEncode("视讯.xls");
System.Web.HttpContext.Current.Response.Headers.Add("Content-Type", "application/ms-excel; charset=UTF-8");
System.Web.HttpContext.Current.Response.Headers.Add("Content-Disposition", "attachment;filename=\"" + fileName + "\"");
System.Web.HttpContext.Current.Response.WriteAsync(Encoding.GetEncoding("GB2312").ToString());
System.Web.HttpContext.Current.Response.WriteAsync(rt);
System.Text.DBCSCodePageEncoding 显示在excel文件很难看,如更改了列标题的字数也会乱码。
文章标题:.netCore中Response.WriteAsync()输出乱码
文章链接:http://soscw.com/index.php/essay/57840.html