SIGN UP MEMBER LOGIN:    
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
share this article :
post comment
 

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

Can somebody tell me how to use this code in a form? Executing this code with a button click?

Posted by Alex C Nov 11, 2010
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor