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.

Community
  • 1
  • 1
  • please show table or tables character encodings – Drew Sep 07 '15 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 – Panagiotis Kanavos Sep 09 '15 at 09: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 – Panagiotis Kanavos Sep 09 '15 at 09:17

1 Answers1

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)
alex.pulver
  • 2,107
  • 2
  • 31
  • 31
  • The default encoding *is* Unicode - UTF8 specifically. – Panagiotis Kanavos Sep 09 '15 at 09:09
  • 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. – alex.pulver Sep 09 '15 at 09:13
  • 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 – Panagiotis Kanavos Sep 09 '15 at 09:17