Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert .XLS to tab separated .TXT

Tags:

c#

csv

excel

Can I somehow convert Excel .XLS file to txt-tsv (tab-separated-values) file, using C#?

like image 484
user198003 Avatar asked Oct 28 '25 18:10

user198003


1 Answers

Using OleDb can be tricky and can cause issues depending on the version of excel the spreadsheet was created on. For example the above example would work with .xls, but not .xlsx. You would have to change your connection string from "Microsoft.Jet.OLEDB.4.0" to "Microsoft.ACE.OLEDB.12.0" in order to compensate. However, it would still not be universal to all excel sheets. I would use Microsoft.Office.Interop.Excel like follows

Microsoft.Office.Interop.Excel.Application myExcel;
Microsoft.Office.Interop.Excel.Workbook myWorkbook;
Microsoft.Office.Interop.Excel.Worksheet worksheet;

myExcel = new Microsoft.Office.Interop.Excel.Application();
myExcel.Workbooks.Open(inputFileName.xls, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
myWorkbook = myExcel.ActiveWorkbook;
worksheet = (Microsoft.Office.Interop.Excel.Worksheet)myWorkbook.Worksheets[1];
myWorkbook.SaveAs(outputFileName.txt, Microsoft.Office.Interop.Excel.XlFileFormat.xlTextWindows, Missing.Value, Missing.Value, Missing.Value, false, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);

myWorkbook.Close(false, Missing.Value, Missing.Value);
myExcel.Quit();

No looping, no bs. Just copy, paste, and change your filenames. The only problem I have seen is sometimes myExcel.Quit() doesn't seem to work correctly and instances of excel stay open I the background. The way around this is to kill the process manually by your program, but I will save that for a different discussion.

like image 99
JSON Avatar answered Oct 31 '25 08:10

JSON