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();
}
}
}
}

jay udayPosted Jan 6, 2016, 2:50 PM
What is the alternative to use in Azure WebApp? As ACE 12 Provider is not supported in Azure WebApp for XLSX.
Bobby PPosted Oct 21, 2014, 12:25 PM
I tried using the exact above in a C# Edit Script in a SSIS Package and got an error "System resource exceeded" when executing line da.Fill(dt). Can anyone clue me in as to why???
Prahalad GaggarPosted Nov 27, 2012, 8:43 AM
Can u provide me with the same code for a Web-App to convert .txt to .xls
Jamie GrossPosted Jul 2, 2011, 1:59 PM
How do I get the excel header into the CSV file?
Krista RossPosted Feb 25, 2011, 8:35 AM
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.
cmc cmcPosted Feb 11, 2011, 10:28 AM
una basura tu programa
Earl NuttingPosted Jan 16, 2011, 4:33 PM
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. [email protected]
Alex CPosted Nov 11, 2010, 4:44 PM
Can somebody tell me how to use this code in a form? Executing this code with a button click?
grant shannonPosted Sep 27, 2010, 4:08 PM
Great thanks for this - worked first time. Regards Grant
Anthony VizzaPosted Aug 18, 2010, 5:32 PM
For anyone who wants to know, to work with .xlsx files change the connection string to string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + sourceFile + ";Extended Properties=\"Excel 12.0;\""; Notice that I have removed the HDR=Yes part of the extended properties, this line indicates that the first row is assumed to be headers and will be left out.. if you would like to keep it that way then use string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + sourceFile + ";Extended Properties=\"Excel 12.0;HDR=Yes\"";
QAtester testrPosted May 22, 2010, 7:41 PM
can somebody please tell what need to be changed to convert xlsx to CSV. thanks vd484
QAtester testrPosted May 22, 2010, 7:41 PM
can somebody please tell what need to be changed to convert xlsx to CSV. thanks vd484
Kenneth SundbyPosted Apr 17, 2010, 8:50 AM
Hi, Your code did not work for me until I changed this: string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + sourceFile + ";Extended Properties=\" Excel.0;HDR=Yes;IMEX=1\""; to this: string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + sourceFile + ";Extended Properties=\"Excel 8.0;\""; Thanks for the idea though:)
Maurvi AswarPosted Feb 26, 2010, 2:43 AM
please give me demo window application with forms. Regards, Maurvi
Maurvi AswarPosted Feb 26, 2010, 2:42 AM
can you please give me a demo window application for convert .xls into .csv in asp.net 3.5(C#)?
kota subrahmanyamPosted Mar 26, 2008, 3:20 AM
Hi, I verified with connection string and extended propertie. I a, using office 2003 so I tried with 11.0 object library .. its also not working. And next i reinstalled the driver. But I didnt fine the correct solution. Help me regarding this. ~ Subrahmanyam, CMC Ltd, HYD.