Reader Level:
ARTICLE

How to Convert XLS File into CSV File in C#

On February 27, 2008 Articles | How do I
The article shows you a simple way to convert an XLS file into a CSV file by using a small C# program
  • 0
  • 0
  • 121844
Download Files:
 

Introduction

The C# program is small and simple as follows. You can test it by giving an input Excel XLS file (source.xls). The program will convert the XLS file into a CSV file named "target.csv".

The program was compiled by VS2005.

using System;

using System.IO;

using System.Data;

using System.Data.OleDb;

using System.Collections.Generic;

using System.Text;

 

namespace XlsToCsv

{

    class Program

    {

        static void Main(string[] args)

        {

            string sourceFile, worksheetName, targetFile;

            sourceFile = "source.xls"; worksheetName = "sheet1"; targetFile = "target.csv"; 

            convertExcelToCSV(sourceFile, worksheetName, targetFile);

        }
 

        static void convertExcelToCSV(string sourceFile, string worksheetName, string targetFile)

        {

            string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + sourceFile + ";Extended Properties=\" Excel.0;HDR=Yes;IMEX=1\""; 

            OleDbConnection conn = null;

            StreamWriter wrtr = null;

            OleDbCommand cmd = null;

            OleDbDataAdapter da = null; 

            try

            {

                conn = new OleDbConnection(strConn);

                conn.Open();

 

                cmd = new OleDbCommand("SELECT * FROM [" + worksheetName + "$]", conn);

                cmd.CommandType = CommandType.Text;

                wrtr = new StreamWriter(targetFile);

 

                da = new OleDbDataAdapter(cmd);

                DataTable dt = new DataTable();

                da.Fill(dt);

 

                for (int x = 0; x < dt.Rows.Count; x++)

                {

                    string rowString = "";

                    for (int y = 0; y < dt.Columns.Count; y++)

                    {

                        rowString += "\"" + dt.Rows[x][y].ToString() + "\",";

                    }

                    wrtr.WriteLine(rowString);

                }

                Console.WriteLine();

                Console.WriteLine("Done! Your " + sourceFile + " has been converted into " + targetFile + ".");

                Console.WriteLine();

            }

            catch (Exception exc)

            {

                Console.WriteLine(exc.ToString());

                Console.ReadLine();

            }

            finally

            {

                if (conn.State == ConnectionState.Open)

                conn.Close();

                conn.Dispose();

                cmd.Dispose();

                da.Dispose();

                wrtr.Close();

                wrtr.Dispose();

            }

        }

    }
}

Yuan Wang

Yuan is working in an IT firm of Northern VA. He has over 10 years experience in mathematical modeling, parallel computing, and programming. He is currently working with Java, C++ and .NET mostly fo...

  • 489rank
  • 267K readers
COMMENT USING

Trending up