IMG-LOGO

How to read excel workbook using open source .Net Koogra?

andy - 10 Mar, 2015 6927 Views 0 Comment

Learn how easy to read excel file using open source .Net Koogra. This library can only used to read excel, biff and xlsx files.

Firstly download the library dll file and copy the file into the bin folder.

Secondly, use the using import method and place this at the top of your code.

using Net.SourceForge.Koogra;

This is the example of routine to read the excel file. Make sure the path of your excel file is correctly defined.

private void ReadExcel(){
	string fullPath = Server.MapPath(Globals.ApplicationPath + "/sample.xls");
	xlsObject = ExcelOpenSpreadsheet(fullPath, 0);
	for (int i = 0; i < xlsObject.Count(); i++){
		object[] row = xlsObject[(int)i];
		if (row[0] != null && i >= 0){
			Response.Write("Value is for column 0 on row " + i.ToString() + " is " + row[0].ToString());
		}
	}
}
	
private object[][] ExcelOpenSpreadsheet(string thisFileName, int worksheetID){
	object[][] values = null;
	try{
		xlFile = Net.SourceForge.Koogra.WorkbookFactory.GetExcelBIFFReader(thisFileName);
		sheet = xlFile.Worksheets.GetWorksheetByIndex(worksheetID);
		values = new object[sheet.LastRow + 1][];
		uint r = 0;
		uint c = 0;

		for (r = sheet.FirstRow; r <= sheet.LastRow; r++){
			values[r] = new object[8];
			Net.SourceForge.Koogra.IRow row = sheet.Rows.GetRow(r);
			if (row != null)
			{
				for (c = 0; c < 8; c++)
				{
					if (row.GetCell(c) != null)
					{
						values[r][c] = row.GetCell(c).Value;
					}
				}
			}
		}
	}catch (Exception ex){
		Response.Write(ex.Message);
	}
	return values;
}

Comments

There are no comments available.

Write Comment
0 characters entered. Maximum characters allowed are 1000 characters.

Related Articles

How to remove html tags from string in c#?

Sometimes you need to remove HTML tags from string to ensure there are no dangerous or malicious scripts especially when you want to store the string or data text into the database.