2

I would like to save xls or xlsx files to a csv extension pipe separated. The following code I use, the XlFileFormat is only possible to save it as an csv comma separated as you can see on the Microsoft info page.

using Microsoft.Office.Interop.Excel;
var excel = new Application();
var wbk = excel.Workbooks.Open(sourceFileLocation, 0, true, 5, "", "", true, XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
wbk.SaveAs(targetFileLocation, XlFileFormat.xlCSVMac, AccessMode: XlSaveAsAccessMode.xlExclusive);

I could save it with a comma sperated and open the file again and change all commas in a pipe, but this is an ugly way. Is there another easy way to save the document to a pipe separated CSV?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Freddy
  • 960
  • 1
  • 20
  • 46

1 Answers1

2

I suggest you combine Convert Excel Range to ADO.NET DataSet or DataTable, etc

and

Convert DataTable to CSV stream

to read the data into a ADO.Net data table and save it in CSV format with the flexibility you need.

Community
  • 1
  • 1
Tarik
  • 10,810
  • 2
  • 26
  • 40
  • Isn't DataSets and DataTables an old technology? I still hear my teacher telling me to not use these anymore. But yeah I would also not know another way... – Freddy Apr 08 '14 at 07:16
  • 1
    I would advise you to accept such claims without questioning them. See http://stackoverflow.com/questions/2698151/entity-framework-vs-linq-to-sql-vs-ado-net-with-stored-procedures – Tarik Apr 08 '14 at 07:29
  • I meant, I would advise you **not** accept such claims without questioning them :-) – Tarik Apr 08 '14 at 12:08