0

my problem to occur when I try fetch data from MySQL to Excel.

I read more example in Internet, but not working.

I want in event "Form_Closing" it will save all MySQL export to Excel.

I've test create *.csv in localhost/phpMyAdmin to export data to Excel but character not support Unicode. It's broken like:

Character with Unicode is broken

And all cell with header display incorrectly. Thanks.

3
  • please show table or tables character encodings Commented Sep 7, 2015 at 18:45
  • Where is the code you used? What is the encoding used in the table? The default encoding for writing files in .NET is UTF-8. Such problems are always caused by using incorrect codepages with ASCII data. Either you used the wrong encoding or the data was problematic to begin with - eg trying to load ASCII data from the database using the wrong codepage, or storing it using the wrong codepage to begin with. The real solution is to use Unicode in the database as well Commented Sep 9, 2015 at 9:12
  • How did you open the csv file? CSV is not an Excel format, Excel actually imports the data. When you double click on the file, Excel will use default settings and may not detect the correct encoding. If you go through File > Open it does a much better job Commented Sep 9, 2015 at 9:17

1 Answer 1

0

When you create the csv file in C#, you must specify the proper encoding like:

System.IO.StreamWriter streamWriter = new System.IO.StreamWriter(
                         "file.csv", false, System.Text.Encoding.Unicode)
Sign up to request clarification or add additional context in comments.

3 Comments

The default encoding is Unicode - UTF8 specifically.
But your characters doesn't appear to be UTF8. They look like stored on 2 bytes or a different encoding. Search for the proper one.
Not necessarily. Check the OP's image. UTF-8 would have added NULLs that would appear as boxes. It doesn't matter anyway - StreamWriter uses UTF-8 by default. Your answer actually suggests to use UTF-16 - that's what Encoding.Unicode corresponds to. This could only help if Excel detects UTF-16 but not UTF8 when you double click on the file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.