Home » excel » c# – How to open CSV file in Excel with correct columns

c# – How to open CSV file in Excel with correct columns

Posted by: admin May 14, 2020 Leave a comment

Questions:

I try to open a CSV file with Excel using the Microsoft.Office.Interop.Excel libary.
And it works fine but all the text is in one column with the delimiter “;”.
Here an example:

Id;Name;Zeit
1;Name1;21.05.2019 09:21:04
3;Name2;21.05.2019 09:21:04

This is the code I used to open the CSV in Excel:

object missing = Type.Missing;
Excel.Application ex = new Excel.Application();
Excel.Workbook wbs = ex.Workbooks.Open(@"c:\users\langenwa\desktop\File.csv", 0, false, Excel.XlFileFormat.xlCSV, "", "", false, Excel.XlPlatform.xlWindows, ";", true, false, 0, true, false, false);
Excel.Worksheet mSheet = (Excel.Worksheet)wbs.Worksheets[1];
ex.Visible = true;

Thanks for any help and sorry for my bad English.

How to&Answers:

The csv file works fine in my excel, likely your system has a different default seperator. You can override this.

try to add this at the top of the csv file:

sep=;
  • note that this only works when opening csv in excel

See these answers for more details: https://superuser.com/questions/606272/how-to-get-excel-to-interpret-the-comma-as-a-default-delimiter-in-csv-files