asp.net – 为什么我的StreamWriter响应输出在Excel中产生垃圾重
发布时间:2020-12-16 06:52:18 所属栏目:asp.Net 来源:网络整理
导读:我正在使用 another Stack Overflow question中的技术将CSV文件写入响应输出以供用户打开/保存.该文件在记事本中看起来很好,但是当我在Excel中打开它时,重音字符是垃圾.我认为这与字符编码有关,所以我尝试手动将其设置为UTF-8(StreamWriter的默认值).这是代
我正在使用
another Stack Overflow question中的技术将CSV文件写入响应输出以供用户打开/保存.该文件在记事本中看起来很好,但是当我在Excel中打开它时,重音字符是垃圾.我认为这与字符编码有关,所以我尝试手动将其设置为UTF-8(StreamWriter的默认值).这是代码:
// This fills a list to enumerate - each record is one CSV line List<FullRegistrationInfo> fullUsers = GetFullUserRegistrations(); context.Response.Clear(); context.Response.AddHeader("content-disposition","attachment; filename=registros.csv"); context.Response.ContentType = "text/csv"; context.Response.Charset = "utf-8"; using (StreamWriter writer = new StreamWriter(context.Response.OutputStream)) { for (int i = 0; i < fullUsers.Count(); i++) { // Get the record to process FullRegistrationInfo record = fullUsers[i]; // If it's the first record then write header if (i == 0) writer.WriteLine(Encoding.UTF8.GetString( Encoding.UTF8.GetPreamble()) + "User,First Name,Surname"); writer.WriteLine(record.User + "," + record.FirstName + "," + record.Surname); } } context.Response.End(); 有关正确编码文件需要做什么的任何想法,以便Excel可以查看重音字符? 解决方法
您可能必须将一个名为
Byte-order Mark的UTF-8指示符写入输出的开头,以通知Excel有关UTF-8的信息.愚蠢的Excel.
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-core – 什么是request5的模拟.在ASP.NET 5中的属性
- asp.net:部分类和继承
- asp.net – 如何使用jQuery ajax避免快速结果的“闪烁”进度
- asp.net-mvc – 版本弃用Facebook Graph API v2.2
- 准备ASP.Net网站进行渗透测试
- 在ASP.NET web.config全球标记中设置日期格式?
- asp.net-mvc – 将Linq2Sql对象传递给Views有什么问题?
- asp.net-mvc – 与程序包管理器控制台中的实体框架相关的项
- asp.net-mvc – 如何从AuthorizationContext获取MVC Action
- asp.net – 会话变量保存在哪里?
推荐文章
站长推荐
- asp.net-mvc – 每当我在MVC3中请求新页面时,都会
- 最好的TinyMce编辑器图像管理器/文件上传为Asp.n
- 安装程序 – 在MVC4 RC中缺少单页应用程序模板
- 如何在asp.net C#中的SQL Server中以YY格式获取年
- .net – VS2017 15.3解决方案文件中的新GlobalSe
- asp.net-mvc-3 – ASP.NET MVC3 WebGrid – 自定
- asp.net-mvc – Asp.net mvc 4部署错误
- asp.net-mvc – 在ASP.NET MVC DisplayFor Html
- asp.net-mvc-4 – ASP.NET MVC 4通过ActionLink传
- asp.net-mvc – 为MVC3 DateTime设置DataFormatS
热点阅读