CSV Separator char


I had to deal with some csv export issues on a customer machine in Asia, until I realized, that when saving a XlsFile with

XlsFile csv = new XlsFile(1, true);
csv.Save("Export.csv", TFileFormats.Text, ',');

the ',' separator char is ignored and the semicolon ';' for a German setting is used. First I thought FlexCel is using the Windows system settings char for list separation (which Excel is using), but it seems to me FlexCel is always using the ';' char as csv separator char. Is there any misunderstanding on my side to make this work with a different separator char?

Best regards

There is something weird going on here. FlexCel doesn't do any replacement of the character you specify: If you say a "," it will use a "," no matter what your locale is. You might be creating files for a customer in a different country, so it makes no sense that we automatically use your machine format settings.

If you wanted to use them, you could write

var listSeparatorStr = CultureInfo.CurrentCulture.TextInfo.ListSeparator;
 if (listSeparatorStr == null || listSeparatorStr.Length != 1)   throw new Exception("Unsupported list separator."); var listSeparator = listSeparatorStr[0]; 

xls.Save("Export.csv", TFileFormats.Text, listSeparator); 

But since you aren't, I don't know what could be going on. I've rechecked the code, and whatever you pass to the delimiter in xls.Save is directly saved to the file (no smart changing of formats behind the scene). I also tried this code:

       static char ListSeparator()
            var listSeparatorStr = CultureInfo.CurrentCulture.TextInfo.ListSeparator;
            if (listSeparatorStr == null || listSeparatorStr.Length != 1)
                throw new Exception("Unsupported list separator.");
            return listSeparatorStr[0];

       void main()
           var xlso = new XlsFile(@"r:\test.xlsx", true);
            xlso.Save("r:\\Export.comma.machineculture.csv", TFileFormats.Text, ',');
            xlso.Save("r:\\Export.semicolon.machineculture.csv", TFileFormats.Text, ';');
            xlso.Save("r:\\Export.listsep.machineculture.csv", TFileFormats.Text, ListSeparator());

            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("de-DE");
            xlso.Save("r:\\Export.comma.de-DEculture.csv", TFileFormats.Text, ',');
            xlso.Save("r:\\Export.semicolon.de-DEculture.csv", TFileFormats.Text, ';');
            xlso.Save("r:\\Export.listsep.de-DEculture.csv", TFileFormats.Text, ListSeparator());

            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("hi-IN");
            xlso.Save("r:\\Export.comma.hi-INculture.csv", TFileFormats.Text, ',');
            xlso.Save("r:\\Export.semicolon.hi-INculture.csv", TFileFormats.Text, ';');
            xlso.Save("r:\\Export.listsep.hi-INculture.csv", TFileFormats.Text, ListSeparator());

And all the files were created in their correct format. Specifically, Export.comma.hi-INculture.csv, Export.comma.de-DEculture.csv and Export.comma.machineculture.csv all use commas.

Can you reproduce your problem with the test code here? Or do you have an idea of how I could reproduce it?

Ps: You might have already seen it, but whenever someone asks for CSV, I recommend reading Understanding CSV files. | FlexCel Studio for the .NET Framework documentation


thank you for your efforts. I think I made things more complicate than they need to be - the solution to my problem was pretty simple (and silly) - I made too much code changes in too short time until I finally realized that I overwrite the filled report with my report template and with the wrong separator. I apologize!

Anyway, I still have some problems with named ranges and an error message, but I will write this into a separate thread.

Best regards