C#导出csv文件 防止中文乱码的解决方案

来自:http://blog.csdn.net/dannywj1371/article/details/8603701

        #region 导出CSV下载
        string exportFileName = "Export" + DateTime.Now.ToString("yyyyMMddHHmmss");
        System.Web.HttpContext context = System.Web.HttpContext.Current;
        StringBuilder sb = new StringBuilder();
        sb.Append("FirstName,LastName,PhoneNo.,State,TimeZone,ZipCode\n");
        for (int i = 0; i < result.PhoneList.Count; i++)
        {
            sb.Append(result.PhoneList[i].FirstName + "," + result.PhoneList[i].LastName + "," + result.PhoneList[i].Phone + "," + result.PhoneList[i].State + "," + result.PhoneList[i].TimeZone + "," + result.PhoneList[i].ZipCode + "\n");
        }
        StringWriter sw = new StringWriter(sb);
        sw.Close();
        context.Response.Clear();
        /* 
         * Acme 2012-07-04 edit 
         *  
        context.Response.Charset = "gb2312"; 
        context.Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312"); 
        context.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; // "application/octet-stream"; //"application/vnd.ms-excel"; //application/vnd.openxmlformats-officedocument.spreadsheetml.sheet                   
        //context.Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", fileName)); 
        */
        //below is new writing  
        context.Response.Charset = "UTF-8";
        context.Response.ContentEncoding = System.Text.Encoding.UTF8;
        context.Response.HeaderEncoding = System.Text.Encoding.UTF8;
        context.Response.ContentType = "text/csv";
        //主要是下面这一句
        context.Response.BinaryWrite(new byte[] { 0xEF, 0xBB, 0xBF });
        context.Response.Write(sw);
        context.Response.AppendHeader("content-disposition", "attachment; filename=" + HttpUtility.UrlEncode(exportFileName + ".csv", System.Text.Encoding.UTF8).Replace("+", "%20"));
        //context.Response.OutputStream.Write(fileData, 0, fileData.Length);  
        context.Response.Flush();
        context.Response.End();
        #endregion


 

你可以尝试使用以下代码来读取包含中文的CSV文件,并解决乱码问题: ```csharp using System; using System.Collections.Generic; using System.IO; using System.Text; public class CsvReader { private string filePath; private string delimiter; public CsvReader(string filePath, string delimiter = ",") { this.filePath = filePath; this.delimiter = delimiter; } public List<List<string>> ReadCsvFile() { List<List<string>> csvData = new List<List<string>>(); using (var reader = new StreamReader(filePath, Encoding.Default)) { while (!reader.EndOfStream) { var line = reader.ReadLine(); var values = line.Split(delimiter); List<string> row = new List<string>(); foreach (var value in values) { row.Add(value); } csvData.Add(row); } } return csvData; } } public class Program { public static void Main(string[] args) { string filePath = "your_file_path.csv"; CsvReader csvReader = new CsvReader(filePath); List<List<string>> csvData = csvReader.ReadCsvFile(); // 打印CSV文件内容 foreach (var row in csvData) { foreach (var value in row) { Console.Write(value + "\t"); } Console.WriteLine(); } } } ``` 在上述代码中,我们使用了 `StreamReader` 类来读取CSV文件,并使用 `Encoding.Default` 来指定默认的编码。如果你的CSV文件采用其他编码方式,你可以根据实际情况进行调整。 请将 `your_file_path.csv` 替换为你的CSV文件的实际路径。此代码将CSV文件的内容打印到控制台,你可以根据需要进行后续处理。 希望这可以帮助到你!如果你有任何其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值