Click here to Skip to main content
15,867,330 members
Articles / Web Development / ASP.NET
Alternative
Tip/Trick

Loading Excel data into a GridView

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
7 Oct 2011CPOL 17.2K   1   1
FileStream stream = File.Open(filePath, FileMode.Open, FileAccess.Read);//1. Reading from a binary Excel file ('97-2003 format; *.xls)IExcelDataReader excelReader = ExcelReaderFactory.CreateBinaryReader(stream);//...//2. Reading from a OpenXml Excel file (2007 format;...
C#
FileStream stream = File.Open(filePath, FileMode.Open, FileAccess.Read);

//1. Reading from a binary Excel file ('97-2003 format; *.xls)
IExcelDataReader excelReader = ExcelReaderFactory.CreateBinaryReader(stream);
//...
//2. Reading from a OpenXml Excel file (2007 format; *.xlsx)
IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
//...
//3. DataSet - The result of each spreadsheet will be created in the result.Tables
DataSet result = excelReader.AsDataSet();
//...
//4. DataSet - Create column names from first row
excelReader.IsFirstRowAsColumnNames = true;
DataSet result = excelReader.AsDataSet();

//5. Data Reader methods
while (excelReader.Read())
{
	//excelReader.GetInt32(0);
}

//6. Free resources (IExcelDataReader is IDisposable)
excelReader.Close();

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Pakistan Pakistan
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Generalits copy paste of "Alternative 1" link content.. Pin
kiran dangar6-Oct-11 21:08
kiran dangar6-Oct-11 21:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.