Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CSV, encoding for Excel

Status
Not open for further replies.

jel

Programmer
Feb 17, 2002
349
NL
I want to make a simple csv-file, and open it in Excel.
What I do is:
Code:
string URL = strPath + strFile + ".csv";
FileStream FS = File.Open(URL, FileMode.Create);
Encoding ENC;
ENC = new UnicodeEncoding();
StreamWriter SW = new StreamWriter(FS,ENC);
...
SW.WriteLine ("value1,value2")
Problem:
when I set encoding ENC to Unicode, Excell puts all values in one column. So, my guess is Excell cant't interpret unicode correctly, and I tried other encoding.
Using either UTF8 OR ASCI, Excel puts everything in the right column. However, diacrytical characters like 'è' or 'ë' are replaced with other characters - they were displayed OK using Unicode.
So I've been messing with setting the file-encoding to ASCI, and then trying to convert the values from unicode to asci before writing to file, but nope...
Has anybody solved this problem?



 
Instead to create a CSV text file, create a tab delimited text file with ".xls" extension.
Open it using Excel and you do not care about the accented chars.

Here is how you can view this file by opening it in Excel:

Code:
string viewer ="C:\\Program Files\\MSOffice\\Office10\\excel.exe";
string m_FileToView = "C:\\MyFolder\\mytabdelimitedtextfile.xls"; 

private void m_CmdView_Click(object sender, System.EventArgs e)
{

	System.Diagnostics.Process vProcess=new System.Diagnostics.Process();
	vProcess.EnableRaisingEvents =false;
	vProcess.StartInfo.FileName= viewer;
	vProcess.StartInfo.Arguments= m_FileToView ;
	vProcess.Start();
	
}
-obislavu-
 
I'd love to, but it is not according to specifications I have to meet:
it MUST be csv,
file MUST have extension "csv",
and it MUST be possible to open in excel by double-clicking the file ...
 
Okay, I said to be tab delimited because you can have comma inside of fields. To avoid that you should enclose the fields in double quotes or use vertical tab or horizontal tab.
The above code I posted will work also if you create your file with comma separated fields and put ".csv" extension.
So if your file is looking like:

a1é,b1,292 Amsterdam Street, # 310
x1,y1,z1ç,100 Berlin Ouest, sc 1, #20

when open with Excel everything is Okay.
But I would create the file like:
"a1é","b1","292 Amsterdam Street, # 310"
"x1","y1","z1ç","100 Berlin Ouest, sc 1, #20"
-obislavu-


 
Sorry, I lost you there.
My problem is simply that if I type in Notepad:
"ë","è"
and name the file xxx.csv, Excel will display correct.
However, if I generate the same(?) file using
Code:
StreamWriter SW = new StreamWriter(FS,ENC);
SW.WriteLine ("\"ë\",\"è\"")
I get either 2 questionmarks in different columns (ENC=ASCII), or the text "ë","è" in one column (ENC=Unicode).
I tried to use ASCI and convert the values using:
Code:
Encoding ENCfrom;
Encoding ENCto;
ENCfrom = new UnicodeEncoding ();
ENCto = new ASCIIEncoding ();
byte[] Buff = ENCfrom.GetBytes (strReturnValue);
byte[] ToBuff = Encoding.Convert(ENCfrom, ENCto, Buff);
strReturnValue= ENCto.GetString(ToBuff);
but no...

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top