Skip to content

Code for read Excel file in C#

Hari B

Asked Jul 29, 2014, 9:27 PM EDT1 answer
Share
LinkedInX
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

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    Hi Hareesh,
    below code will help you.

    Java
            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 Microsoft.Office.Interop.Excel.Range Range = null;

    C#
     public MyExcel()
            {
                if (this.APP == null)
                {
                    this.APP = new Microsoft.Office.Interop.Excel.Application();
                    this.APP.DisplayAlerts = false;
    
                    this.APP.WorkbookBeforeClose += APP_WorkbookBeforeClose;
                }
            }
    C#
     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;
                }
            }
    C#
     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;
                }
            }
    public void Close()
            {
                if (this.APP != null)
                {
                    if (this.WB != null)
                    {
                        if (this.WS != null)
                            Marshal.ReleaseComObject(this.WS);
                        this.WB.Close(false, Type.Missing, Type.Missing);
                        Marshal.ReleaseComObject(this.WB);
                    }
                    this.APP.Quit();
                    Marshal.ReleaseComObject(this.APP);
                }
            }

    if this helps you kindly mark as answer
    Thanks,
    Kailash
    0