将特殊字符写入Excel时出现问题

时间:2009-12-23 13:55:50

标签: c# excel

我有一些导出到Excel的报告。问题是,如果有特殊字符,它将被一些有趣的符号取代

例如,' - '(连字符)被替换为'...

解决问题的任何帮助??

2 个答案:

答案 0 :(得分:0)

最直接的方法是将文本文件编码为UTF-8。我运行了以下代码,在Excel 2007中打开了生成的hyphen.txt文件,它按预期工作:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var hyphen = "\u2010\r\n";
            var encoding = Encoding.UTF8;
            var bytes = encoding.GetBytes(hyphen);
            using (var stream = new System.IO.FileStream(@"c:\tmp\hyphen.txt", System.IO.FileMode.Create, System.IO.FileAccess.ReadWrite))
            {
                stream.Write(bytes, 0, bytes.Length);
            }
        }
    }
}

答案 1 :(得分:0)

这是代码 - view at PasteBin

相关问题