Excel to CSV

[box type=”warning”]This code is designed for .NET 4 and uses the optional parameters enhancements. If you want to use it in a previous version of .NET you will need to specify the additional missing parameters.[/box] Using the Excel libraries it’s possible to convert an Excel file to a CSV file. The result of this operation would be the same as if you opened the Excel file and selected “Save As” and then picked as CSV format to save it in, except it’s all automated in your code.

Reference the Microsoft Excel Libraries

[box type=”info”]You will need to have Microsoft Excel installed on your development computer for this to work.[/box]

In Visual Studio you need to goto your ProjectReferences. Right-click and choose Add Reference.

In the window that appears select the COM tab, and pick Microsoft Excel and click ok.

When you do this Visual Studio will find the Interop Assemblies for Office and locate the Excel one and use that instead of a direct reference to the COM.

In your references you should now see Microsoft.Office.Interop.Excel.

ExcelHelper C# Class

This class has a single static method called ConvertExcelToCsv, which takes an Excel file and creates a CSV file. One thing to note is that it does NOT delete the original Excel file, that is up to you if you want to do that.

Since Excel files can have multiple sheets, there is the optional parameter of specifying what sheet to write as the CSV file. If you leave it blank it will default to the first one in the workbook.

using System.IO;
using System.Runtime.InteropServices;
using Microsoft.Office.Interop.Excel;

namespace jarloo
    public static class ExcelHelper
        public static void ConvertExcelToCsv(string source, string destination, int sheetNumber=1)
            if (File.Exists(destination)) File.Delete(destination);

            Application xl = new Application();

                Workbook workbook = xl.Workbooks.Open(source);
                Worksheet ws = (Worksheet) workbook.Sheets[sheetNumber];
                ws.SaveAs(destination, XlFileFormat.xlCSV);

                xl.DisplayAlerts = false;


Example Converting Excel to CSV in C#

All you need to do to convert the file after you’ve added the ExcelHelper class is execute the method like so:

namespace Jarloo
    class Program
        static void Main(string[] args)

Categories:   Code

Tags:  , , , ,


  • Posted: May 31, 2012 07:40


    You are a star. I spent hours looking for an easy solution to this problem, and yours was the only one that worked. Thanks so much..! Your site is forever bookmarked :-)
    • Posted: May 31, 2012 07:46


      Glad you found what you were looking for!
  • Posted: June 8, 2012 04:39


    Great source, thank you very much. Is it possible to modify it to convert all files in defined folder from xls to csv?
    • Posted: June 8, 2012 07:38


      Yes it can you would just need to use the directory class to get a list of files in the directory, then loop through them passing each one into the conversion method.
  • Posted: June 8, 2012 07:41


    Could you point me to the manual, or give some example, so I could try to modify it?
  • Posted: July 25, 2012 10:58


  • Posted: July 27, 2012 00:04


    First of all I found out that csv filed separator depends on .NET version, not only regional settings. Prior to 3 it saves with dot comma, 4 creates only comma separated (even with regional settings with ";" and Local = True). Also, I modified the code a little bit. Now it converts multiple files from folder defined in strInputFolder, puts csv result files to the folder defined in strOutputFolder and deletes the original Excel files. Here is the full code: Module Module1 Sub Main() Dim Excel = CreateObject("Excel.Application") 'Excel.ActiveWorkbook.SaveAs(FileFormat = xlCSV) Dim strXLSFile As String Dim strCSVFile As String 'Change Input and Output folders to relevant location Const strInputFolder As String = "C:\input\" Const strOutputFolder As String = "C:\output\" strXLSFile = Dir(strInputFolder & "*.xls") Do While strXLSFile "" strCSVFile = Left(strXLSFile, InStrRev(strXLSFile, ".")) & "tmp" Excel.Workbooks.Open(strInputFolder & strXLSFile) Excel.Application.DisplayAlerts = False Excel.ActiveWorkbook.SaveAs(strOutputFolder & strCSVFile, FileFormat:=6, local:=False) ' 'FileFormat:=xlCSV) Excel.ActiveWorkbook.Close(True) My.Computer.FileSystem.DeleteFile(strInputFolder & strXLSFile) strXLSFile = Dir() Loop 'End Module End Sub End Module
  • Posted: August 23, 2012 15:16


    Thank you sooo much for this post! I am using Visial Studio 2005, .NET v2. on the 2 lines below, I am getting error: No overload for method 'Open' takes '1' arguments Workbook workbook = xl.Workbooks.Open(source); Worksheet ws = (Worksheet) workbook.Sheets[sheetNumber]; Has anyone had this issue or know why I get this please? Thanks
    • Posted: August 23, 2012 17:53


      Microsoft made it easier to work with Excel in C# with version 4 I believe it was. In newer versions you don't need to supply all the parameters when they are default or null, but you will need to supply them all. Sorry I don't have the right version of .NET etc here.
  • Posted: November 19, 2012 06:08


    i need help for real time data from running excel on remote server .my remote excel run on remote server and update every second.i need this real time data in my machine running excel update at same time when updation in remote excel without dealy. if any one know then plz help me ..
  • Posted: November 20, 2012 00:40


    Rajesh, This is something absolutely different. Code that is posted here is for converting files, not for updating them in real time. In Excel updates in real time are possible with file shared access enabled. In this case file that is opened simultaneously on several machines is updated as soon as one of users saves it. Also you could define ODBC data source in a file and update it from some remote excel target.
  • Posted: November 20, 2012 00:48


    can we make ODBC connection from remote .if yes then give some example of remoite excel data access from remote server
  • Posted: November 20, 2012 01:03


    Rajesh, This is just a standard ODBC setup with excel source. You do not need any samples for this task. Just define ODBC data source Excel (which is standard for Windows ODBC).
    • Posted: November 20, 2012 01:14


      Dear , I'm C#.net programmer and i'm setup odbc connection for remote excel on remote machine. but can you tell me how to update my local computer excel data from this remote excel data through ODBC .
  • Posted: March 20, 2013 16:40


    hi im try change the "," by other caracter when save but icant you now if is posible??
    • Posted: March 20, 2013 18:04

      Kelly Elias

      The delimiter is specified by the format. In the example I use XlFileFormat.xlCSV. You can see the various formats here: http://msdn.microsoft.com/en-ca/library/microsoft.office.interop.excel.xlfileformat(v=office.11).aspx
  • Posted: May 16, 2016 14:25

    Carol Ouellet

    This is nice and simple but I suggest adding the below lines and the end of the finally. Otherwise Excel.exe stays opened in the background. The lines repeat twice, it is not a typo. GC.WaitForPendingFinalizers(); GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect();