Click here to Skip to main content
15,886,085 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have a CSV file in which the columns are separated with '\t'. I need to open the file programmatically with proper separation of columns. Please help me to view the file in windows application in runtime. Tried the below code but didn't work for me. It is still not in the required format.

Thanks in advance.

What I have tried:

C#
Excel.Application xlApp;
              object misValue = System.Reflection.Missing.Value;
              xlApp = new Excel.Application();
              xlApp.Workbooks.OpenText(@"c:\polsamples\POLList_201633.csv", Microsoft.Office.Interop.Excel.XlTextParsingType.xlDelimited);
              xlApp.Visible = true;
Posted
Updated 7-Oct-16 11:01am
Comments
Suvendu Shekhar Giri 7-Oct-16 2:03am    
"not in the required format"
what is the required format?
User1454 7-Oct-16 2:09am    
For e.g : This is the required format
Id Student Marks
1 Jake 99
2 Luke 98

But now this is viewed as :
IdStudentMarks
1Jake99
2Luke98

Columns does not get separated with '\t' unless it is done manually (open excel-> DATA->From Text->Finish)
Sinisa Hajnal 7-Oct-16 4:19am    
Check the properties of interop object you use. There should be one defining the separator.
F-ES Sitecore 7-Oct-16 4:51am    
The OpenText method accepts a "Tab" parameter that lets you say it's tab delimited. https://msdn.microsoft.com/en-us/library/office/ff837097.aspx
User1454 7-Oct-16 5:14am    
Tried this as well, but didn't work :(
var ExcelApp = new Excel.Application();
ExcelApp.Workbooks.OpenText( path, Tab:true);
ExcelApp.Visible = true;

I think you are using Interop. Avoid using that.

You can write your own logic using open source projects mentioned over here - C# convert csv to xls (using existing csv file)[^]
 
Share this answer
 
I recently posted an article for a CSV file parser. Right now, it only uses a comma as the separator, but it would be a minor exercise to make the delimiter configurable.

CSV File Parser[^]
 
Share this answer
 
Excel know how to open such files.
By using the macro recording feature, you can record steps to open the file and when record is finished, you can see what excel did to open.
It should not be too complicated to translate in your code.
 
Share this answer
 
Comments
User1454 7-Oct-16 8:14am    
Actually I need to open CSV file in excel from windows application.
For e.g : using 'Process.Start(filename.csv); ' it should be able open but since this \t separated,once it is opened from application, it displays as a single text in each row instead of categorizing into columns.
Just convert your tab to comma
C#
var path = @"c:\polsamples\POLList_201633.csv";
var lines = File.ReadAllLines(path);
var newLines = lines.Select(l => string.Join(",", l.Split('\t')));
File.WriteAllLines(path, newLines);


Microsoft.Office.Interop.Excel.Application xlApp;
object misValue = System.Reflection.Missing.Value;
xlApp = new Microsoft.Office.Interop.Excel.Application();
xlApp.Workbooks.OpenText(path, Microsoft.Office.Interop.Excel.XlTextParsingType.xlFixedWidth);
xlApp.Visible = true;
 
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