Link to home
Start Free TrialLog in
Avatar of RadhaKrishnaKiJaya
RadhaKrishnaKiJaya

asked on

Special character is not displaying correctly

Hi Experts,

I have this code. When I export the data to CSV file, it should display "Approuvé Date".

But it is not encoding the "é " character properly and displaying "Approuvé Date".

Please help me what should I do to fix this problem. It happens for French, German and Italian language.


Thanks in advance!

using Orchard.Localization;
 public Localizer T { get; set; } var propertiesToInclude = new Dictionary<string, string> {                {"ApproveDate", T("ApproveDate").Text} };

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Chinmay Patel
Chinmay Patel
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of RadhaKrishnaKiJaya
RadhaKrishnaKiJaya

ASKER

Thank you for trying to help me!

This is the code that sends the data to CSV file.
private void PushCsv(StringBuilder builder, string filename) {
         _workContextAccessor.GetContext().HttpContext.Response.Clear();
         _workContextAccessor.GetContext().HttpContext.Response.AddHeader("content-disposition",
                                                          string.Format("attachment; filename={0}", filename));
         _workContextAccessor.GetContext().HttpContext.Response.ContentType = "application/ms-excel";
         _workContextAccessor.GetContext().HttpContext.Response.Write(builder.ToString());
         _workContextAccessor.GetContext().HttpContext.Response.End();
      }

Open in new window

This is not an excel file. I think the content type is incorrect, it should be:
_workContextAccessor.GetContext().HttpContext.Response.ContentType = "text/csv";

Open in new window

Note:
1) Another content type that you could try is  "application/csv"
2) If more help needed please provide what version of.NET and .NETMVC you re using and how your public method calls your code snippet

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi All,
Thank you for trying to help me!
Miguel Oz , I tried all the solution you provided. But nothing worked. The .net version I am using is 4.8.3761.0
The public method called is this.
public void ExportAsDelimitedFile<T>(string filename, IEnumerable<T> items) {
         var builder = new StringBuilder();
         foreach (var item in items) {
            builder.AppendLine(item.ToString());
         }
         PushCsv(builder, filename);
      }

Open in new window

Jonathan Dahan , Can you please tell me in my code above how I can add the encoding info(UTF-8 encoding)

Thank you all so much for your time!
I added this and it is working fine now!
_workContextAccessor.GetContext().HttpContext.Response.ContentEncoding = Encoding.GetEncoding("Windows-1252");

Open in new window

windows-1252 defined at Microsoft's site.

Here's a quote from DBpedia:

Windows-1252 or CP-1252 (code page 1252) is a single-byte character encoding of the Latin alphabet, used by default in the legacy components of Microsoft Windows for English and many European languages including Spanish, French, and German. Depending on the country, use can be much higher than the global average, e.g. for Germany (including ISO-8859-1) at 6.3%.