开发者

Issue in writing special characters to Excel

开发者 https://www.devze.com 2022-12-15 04:28 出处:网络
I have a few reports that are ex开发者_运维知识库ported to Excel. The problem is whereever there are special characters, it is being replaced by some funny symbols

I have a few reports that are ex开发者_运维知识库ported to Excel. The problem is whereever there are special characters, it is being replaced by some funny symbols

For example, '-'(hyphen) was replaced by –...

Any help to solve the problem??


The most straight forward way is to encode the text file as UTF-8. I ran the following code, opened the resulting hyphen.txt file in Excel 2007 and it worked as expected:

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);
            }
        }
    }
}


This is the code -- view at PasteBin.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号