Last active
January 14, 2020 12:19
-
-
Save Munawwar/924413 to your computer and use it in GitHub Desktop.
C# - Excel Data Reader Library - Convert Excel (XLSX or XLS) to CSV
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A detailed c# excel tutorial
http://csharp.net-informations.com/excel/csharp-excel-tutorial.htm
clint