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

}

generate a list of alphabet using csharp

//generate a list of alphabet using csharp

//this recurcive function will return you

//a string with position of passed int

//say if pass 0 will return A ,1-B,2-C,.....,26-AA,27-AB,....,701-ZZ,702-AAA,703-AAB,...

static string CharacterIncrement(int colCount)

{

int TempCount = 0;

string returnCharCount = string.Empty;

if (colCount <>

{

TempCount = colCount;

char CharCount = Convert.ToChar((Convert.ToInt32('A') + TempCount));

returnCharCount += CharCount;

return returnCharCount;

}

else

{

var rev = 0;

while (colCount >= 26)

{

colCount = colCount - 26;

rev++;

}

returnCharCount += CharacterIncrement(rev-1);

returnCharCount += CharacterIncrement(colCount);

return returnCharCount;

}

}

//--------this loop call this function---------//

int i = 0;

while (i <>

{

string CharCount = string.Empty;

CharCount = CharacterIncrement(i);

i++;

}