tags:

views:

214

answers:

2

I have a few reports that are exported 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??

A: 

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);
            }
        }
    }
}
Joe Erickson
A: 

This is the code -- view at PasteBin.

suganya
What's the Request/Reponse encoding in your web.config?
Mikael Svenson