Skip to content

Instantly share code, notes, and snippets.

@Munawwar
Last active January 14, 2020 12:19
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save Munawwar/924413 to your computer and use it in GitHub Desktop.
Save Munawwar/924413 to your computer and use it in GitHub Desktop.
C# - Excel Data Reader Library - Convert Excel (XLSX or XLS) to CSV
/*
* Dependency : Excel Data Reader from http://exceldatareader.codeplex.com/
* You must add the references to the Dlls (downloaded from the link above) with Visual Studio.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Excel;
using System.Data;
using System.IO;
namespace ExcelDataReaderTest
{
class ExcelDataReader
{
static void Main(string[] args)
{
//Reading from a binary Excel file ('97-2003 format; *.xls)
//IExcelDataReader excelReader2003 = ExcelReaderFactory.CreateBinaryReader(stream);
//Reading from a OpenXml Excel file (2007 format; *.xlsx)
FileStream stream = new FileStream("../../myxlsx/sample.xlsx", FileMode.Open);
IExcelDataReader excelReader2007 = ExcelReaderFactory.CreateOpenXmlReader(stream);
//DataSet - The result of each spreadsheet will be created in the result.Tables
DataSet result = excelReader2007.AsDataSet();
//Data Reader methods
foreach (DataTable table in result.Tables)
{
for (int i = 0; i < table.Rows.Count; i++)
{
for (int j = 0; j < table.Columns.Count; j++)
Console.Write("\"" + table.Rows[i].ItemArray[j] + "\";");
Console.WriteLine();
}
}
//Free resources (IExcelDataReader is IDisposable)
//excelReader2003.Close();
excelReader2007.Close();
Console.Read();
}
}
}
@clintmannar
Copy link

@Munawwar
Copy link
Author

Munawwar commented Apr 9, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment