10

I want to open an excel file and save it as a csv file. Google search no lucky. I need C sharp code to do it.

Thanks for your nice help.

ardmore
  • 167
  • 2
  • 3
  • 5

3 Answers3

13

If you are willing to use Excel Interop:

        Excel.Application app = new Excel.Application();
        Excel.Workbook wb = app.Workbooks.Open(@"c:\temp\testtable.xlsx");
        wb.SaveAs(@"C:\Temp\output.csv", Excel.XlFileFormat.xlCSVWindows);
        wb.Close(false);
        app.Quit();
        Console.WriteLine("Done!");
John Koerner
  • 37,428
  • 8
  • 84
  • 134
  • 1
    Error:No overload for method 'SaveAs' takes '2' arguments – ardmore Feb 17 '11 at 21:32
  • 1
    Which Excel interop library did you use? Which VS version are you using and framework are you compiling to? Depending on those answers, you may need to pass all of the arguments with the rest being Type.Missing to get it to compile. – John Koerner Feb 17 '11 at 21:43
  • this works quite well; in real code probably worth making sure the converted csv file is unique... using Guid is an efficient way of creating a unique file name for example. – dragonfly02 Mar 18 '15 at 12:20
5
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Interop.Excel;
using System.IO;

namespace TestConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            String fromFile = @"C:\ExlTest\Test.xlsx";
            String toFile = @"C:\ExlTest\csv\Test.csv";

            Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
            Microsoft.Office.Interop.Excel.Workbook wb = app.Workbooks.Open(fromFile, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);

            // this does not throw exception if file doesnt exist
            File.Delete(toFile);

            wb.SaveAs(toFile, Microsoft.Office.Interop.Excel.XlFileFormat.xlCSVWindows, Type.Missing, Type.Missing, false, false, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Microsoft.Office.Interop.Excel.XlSaveConflictResolution.xlLocalSessionChanges, false, Type.Missing, Type.Missing, Type.Missing);

            wb.Close(false,Type.Missing,Type.Missing);

            app.Quit();
        }
    }
}
ChrisF
  • 134,786
  • 31
  • 255
  • 325
Tomv
  • 51
  • 1
  • 1
  • VS2010 Office.Interop.Excel (v12). This also works using .xls files. May have issues with multiple populated sheets, only tested with workbook that had single sheet. – Tomv Jun 17 '14 at 20:28
  • `File.Delete(toFile);` does throw an error when csv file does not exist – user1413 May 29 '20 at 03:23
-3

You could use Visual Studio Tools for Office or ADO.NET to do this.
For more compatibility I suggest you to use the second one: take a look at some tutorials like David Hayden's one to learn how to use it.

To create a CSV file you have just to read the Excel data and write the results to a file using the structure written in Wikipedia.

as-cii
  • 12,819
  • 4
  • 41
  • 43