Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I create a .csv file by C# code in my project. It has some column saperated with comma.
StringBuilder sb = new StringBuilder();
                //Making columns!
                foreach (ColumnHeader ch in listView_Search.Columns)
                {
                    sb.Append(ch.Text + ",");
                }
                sb.AppendLine();

                //Looping through items and subitems
                foreach (ListViewItem lvi in listView_Search.SelectedItems)
                {
                    foreach (ListViewItem.ListViewSubItem lvs in lvi.SubItems)
                    {
                        if (lvs.Text.Trim() == string.Empty)
                            sb.Append(" ,");
                        else
                            sb.Append(lvs.Text + ",");
                    }
                    sb.AppendLine();
                }

                StreamWriter sw = new StreamWriter(filePath, false, Encoding.UTF8);
                sw.Write(sb);
                sw.Close();



In my system (windows 7, Microsoft Office 2010) my file can open with Excel correctly. every column values are in saperated columns, correctly.
But in other system (windows XP, Microsoft Office 2010) that file opens by Excel but all values are in same column!!!!

how do i resolve this problem?
Posted
Comments
[no name] 31-Dec-14 6:25am    
Check for the format specifier there for MS Excel.
Afzaal Ahmad Zeeshan 31-Dec-14 6:40am    
However the problem is, this problem is a syntax problem. You should try to check for the syntax by validating it.
Zon-cpp 31-Dec-14 6:49am    
my .csv file is created and it is opened by MS excel 2010 in windows 7,
but it can't be opened in windows XP.
my question is, why??!!!
Richard MacCutchan 31-Dec-14 7:04am    
Check exactly how the file is being opened, it may be that the user is doing something different. Without more information it is impossible to guess what the problem may be.
Zon-cpp 31-Dec-14 7:13am    
when I created a .csv file by MS Excel , itself. the file is created correctly.
I checked that file: the columns are Separated with semicolon , instead comma !
how this is possible , in the same version of MS Excel (2010)?!

It's probably down to what character is set as the list seperator on the two machines in question.

[^] Check the bottom of this page for details on how to change it.
 
Share this answer
 
Comments
Zon-cpp 1-Jan-15 0:28am    
thank you.
can I get system setting for seperator character in my code (C#)? then I will create my .csv file using that seperator.
Zon-cpp 1-Jan-15 1:21am    
I found, I can get seperator character with this code:

System.Globalization.CultureInfo.CurrentCulture.TextInfo.ListSeparator

Thank you PeejayAdams ,
If you double-click the file and it automatically opens in Excel, Excel will use a default set of configuration options to parse the text.

If you open Excel first, then go File -> Open, you'll get a wizard window that allows you to configure the parsing options, including the separator characters.
 
Share this answer
 
If possible use another format that does not depends on current locale. It is a well known limitation of CSV format.

You can open any CSV file manually.... but automatic opening will always use system defined separator. The same would apply to date format (separator and order).

One option is to use Open XML SDK to create real Excel file. Although it is free, it is very low level and require a deep understanding of XLSX file format and a lot of testing.

Otherwise, you might use other format like XML (or even HTML), Office Interop (if you are not runniong on a server and are sure that the client has Excel installed) of buy some component that do support Excel files.

If you want to stay with CSV, you should probably use system settings when exporting. It should work has long has the source and target computer if the same (or the system local is the same on both system). Or you can ask the user which separator to uses. In such case, you might put some explaination in the user manual about those issues and maybe some explaination on how to manually import data in Excel.

In reality, CSV files are a bad choice as it is not a well defined format as field separator, date ordering, number and date separator all depends on system settings which changes by country/language.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900