Friday, July 16, 2010

get excel data by passing file stream


//pass file stream and get excel data

//referance link:-http://exceldatareader.codeplex.com/

static void GetAllExcelValues(Stream sourceFileStream)

{

//FileStream sourceFileStream = File.Open(filePath, FileMode.Open, FileAccess.Read);

//1. Reading from a binary Excel file ('97-2003 format; *.xls)

IExcelDataReader excelReader = ExcelReaderFactory.CreateBinaryReader(sourceFileStream);

//...

//2. Reading from a OpenXml Excel file (2007 format; *.xlsx)

//IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(sourceFileStream);

//...

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

var tableCount = 0;

while (tableCount <>

{

var Sheets = result.Tables[tableCount];

int rowCount = 1;

foreach (DataRow Eachrows in Sheets.Rows)

{

foreach (var CellValue in Eachrows.ItemArray)

{

string CharCount = string.Empty;

var data = CellValue != null ? CellValue : string.Empty;

}

rowCount++;

}

tableCount++;

}

excelReader.Close();

}

No comments: