Hello All
I am the Beginner in C#. i want code for read Excel file in C#. in Excel file read based on the key Vale(key value means one column). Please Help me.
Thanks
Code for read Excel file in C#
1 Answer
Know the answer? Post it. Somebody with the same question will find it here.
Former memberPosted Jul 30, 2014, 12:41 AM EDT
below code will help you.
public class MyExcel { public Microsoft.Office.Interop.Excel.Application APP = null; public Microsoft.Office.Interop.Excel.Workbook WB = null; public Microsoft.Office.Interop.Excel.Worksheet WS = null;public MyExcel() { if (this.APP == null) { this.APP = new Microsoft.Office.Interop.Excel.Application(); this.APP.DisplayAlerts = false; this.APP.WorkbookBeforeClose += APP_WorkbookBeforeClose; }public Microsoft.Office.Interop.Excel.Workbook Open(string Location) { try { this.WB = this.APP.Workbooks.Open(Location, 0, false, 5, "", "", false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false); this.WS = this.WS = (Microsoft.Office.Interop.Excel.Worksheet)WB.Sheets[1]; this.APP.Visible = true; // this.WS.Change += APP_SheetChangeEvent; // this.APP.SheetChange += Event_ChangeEvent; return this.WB; } catch (Exception ex) { throw ex; }public object GetCellValue(string ColumnName) { try { if (this.WS != null) { Thread.Sleep(50); Microsoft.Office.Interop.Excel.Range RG = this.WS.get_Range(ColumnName, ColumnName); // string add = RG.Address[1, 1, Microsoft.Office.Interop.Excel.XlReferenceStyle.xlA1]; return RG.Value; } else return null; } catch (Exception ex) { MessageBox.Show(ex.Message); throw ex; }if this helps you kindly mark as answer
Thanks,
Kailash