How to fetch data from MySQL into Excel Unicode character in C# - c#

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:
And all cell with header display incorrectly. Thanks.

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)

Related

Keep special characters in File When i save it

I have a file that contains some HTML code. I am trying to load this data into a C# Console app and transfer it into a JSON file to upload somewhere. When loading the file i am losing some of the encoding immediately when bringing the data in.
Example data
<li>Comfort Range: -60°F to 30°F / -50°C to -1°C</li>
Basic read file
//Load the file
String HTML_File = File.ReadAllText(location);
//Output the file to see the text
Console.WriteLine(HTML_File);
Console Output
<li>Comfort Range: -60??F to 30?F / -50?C to -1?C</li>
After i split the data how I need to, I than save the class to a JSON File
File.WriteAllText(OutputPath,JsonConvert.SerializeObject(HTMLDATA));
JSON file Data
<li>Comfort Range: -60�F to 30�F / -50�C to -1�C</li>
How can i go about loading this data and converting it to JSON without losing the encoding? I am still pretty new when it comes to encoding like this.
#JeremyLakeman helped me solve this, thank you sir!! When reading the text into the utility i needed to set the Encoding but not by the default ones.
File.WriteAllText(OutputPath,JsonConvert.SerializeObject(HTMLDATA), Encoding.GetEncoding("iso-8859-1"));
#JeremyLakeman helped me solve this, thank you sir!! When reading the text into the utility i needed to set the Encoding but not by the default ones.
File.WriteAllText(OutputPath,JsonConvert.SerializeObject(HTMLDATA), Encoding.GetEncoding("iso-8859-1"));

StreamWriter writes all data into 1 Column in CSV file

using (StreamWriter sw = File.CreateText("D:\\Data.csv"))
{
foreach (var test in data)
{
sw.WriteLine(test.A.ToString()+','+test.B.ToString());
}
}
test.A and test.B are listed in column A of CSV file.I want them to be in the separate columns.
Try changing your delimiter to semicolon - it will separate your headers into different columns when you open the CSV file via Excel.
I had the same issue: I was able to write to a .txt or .csv with commas as the separators, but in Excel and other apps (python's Pandas) it wasn't being recognized as separate columns properly, my data was all in one column.
I solved this by modifying the Encoding to utf-8:
https://learn.microsoft.com/en-us/dotnet/api/system.text.encoding?view=net-6.0
This is the default for SW but you might have mistakenly changed this elsewhere.

SpreadsheetGear.Factory.GetWorkbook(filename) replacing empty value with 0 in excel file

I have DevEx gridControl with some data from DB.
gridControl.ExportToXlsx(filename, new XlsxExportOptions(TextExportMode.Value));
this code block Works fine and export data to excel. after export, I am opening and closing excel file using SpreadsheetGear like below...
WorkBook = Factory.GetWorkbook(filename);
WorkBook.Save();
WorkBook.Close();
My intention is to perform some task after opening excel file but This opening and closing of excel file replacing all empty values in excel file with 0 (zero).
Can anyone help me to understand why this is happening and how can I fix this.
one more point to add, if I export data with TextExportMode.Text option then SpreadsheetGear is not replacing empty values with 0 but exporting as Text will not solve my purpose.
Thanks in advance.

C# SaveFileDialog CSV (MS-DOS)

I'm using C# WPF and trying to save a file with a CSV (MS-DOS) type, every time it saves it as a normal CSV file.
How I can force it to be (MS-DOS) type?
According to this page the difference is...
If you export as Windows CSV, those fields are encoded using the Windows-1252 code page. DOS encoding usually uses code page 437, which maps characters used in old pre-Windows PCs.
So, you need to specify an encoding when you create your StreamWriter. Maybe something along the lines of (untested)...
using (StreamWriter sw = new StreamWriter(File.Open("filename", FileMode.Create), Encoding.GetEncoding(437)))
{
sw.WriteLine("my text...");
}

Exporting data from database to text file

I have a table in my database called Employeee.
I need to write the database data to a file.
How do I do it in asp.net mvc C#?
i have tried Datatable approach,but it failed.
Do It the same way you would do in a winform, wpf or so.
For example, if your text document has to be in JSON, use :
using(TextWriter writer = new TextWriter(#"yourfilename.json")){
writer.Write(JsonConvert.SerializeObject(db.Employeee));
}
In my example, don't forget to import the Newtownsoft.Json package.
You can also formate everything in XML, CSV...

Categories