ARTICLE

How to Convert XLS File into CSV File in C#

Posted by Yuan Wang Articles | How do I February 27, 2008
The article shows you a simple way to convert an XLS file into a CSV file by using a small C# program
Reader Level:
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();

            }

        }

    }
}

Login to add your contents and source code to this article
post comment
     

Can u provide me with the same code for a Web-App to convert .txt to .xls

Posted by Prahalad Gaggar Nov 27, 2012

How do I get the excel header into the CSV file?

Posted by Jamie Gross Jul 02, 2011

Hi! Great bit of code there! But how can I get rid of the double quotes? Everytime I try, I only get a blank target file.

Posted by Krista Ross Feb 25, 2011

una basura tu programa

Posted by cmc cmc Feb 11, 2011

I am not sure how to execute this module. I have downloading the zip file and extract it. What do I do explicitly? I am running Windows 7. earlnutting@yahoo.com

Posted by Earl Nutting Jan 16, 2011
COMMENT USING
PREMIUM SPONSORS
DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and add new content to existing PDF documents from within your applications.
Get Career Advice from Experts
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Get Career Advice from Experts