none
object reference not set to an instance of an object excel RRS feed

  • Question

  • Hi 

    While reading cells from excel i am getting this error 

    object reference not set to an instance of an object excel

    If i put some value in first cell then it is not showing any error but if first cell is blank then it is showing the error.

    This is my code

     

    string expath = comboBox4.Items[j].ToString();

                                        Microsoft.Office.Interop.Excel.Application xlApp;

                                        Microsoft.Office.Interop.Excel.Workbook xlWorkBook;

                                        Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;

                                        Microsoft.Office.Interop.Excel.Range range;

     

     

                                        xlApp = new Microsoft.Office.Interop.Excel.ApplicationClass();

     

                                        xlWorkBook = xlApp.Workbooks.Open(expath, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);

                                        xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(2);

                                        range = xlWorkSheet.UsedRange;

     

                                        string Art, Hersteller, Typ, Baujahr, Sicherung = "";

                                        try

                                        {

                                            Art = (range.Cells[4, 7] as Microsoft.Office.Interop.Excel.Range).Value2.ToString();

                                            Hersteller = (range.Cells[5, 7] as Microsoft.Office.Interop.Excel.Range).Value2.ToString();

                                            Typ = (range.Cells[6, 7] as Microsoft.Office.Interop.Excel.Range).Value2.ToString();

                                            Baujahr = (range.Cells[7, 7] as Microsoft.Office.Interop.Excel.Range).Value2.ToString();

                                            Sicherung = (range.Cells[13, 5] as Microsoft.Office.Interop.Excel.Range).Value2.ToString();

                                            MessageBox.Show(indx+ " " +value+ " "+ Art + " " + Hersteller + " " + Typ + " " + Baujahr + " " + Sicherung);

                                        }

    Thursday, July 14, 2011 6:36 AM

Answers

  • Ok i got the solution

    Without using range and change some values

     

     Art = ((Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Cells[4, 7]).Value2.ToString();

    • Marked as answer by akki_adhikari Thursday, July 14, 2011 7:19 AM
    Thursday, July 14, 2011 7:19 AM

All replies

  • Ok i got the solution

    Without using range and change some values

     

     Art = ((Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Cells[4, 7]).Value2.ToString();

    • Marked as answer by akki_adhikari Thursday, July 14, 2011 7:19 AM
    Thursday, July 14, 2011 7:19 AM
  • Hello,

    I suggest that you debug the code to find the cause.

    I guess this occurs because Excel cannot convert a file containing the first value empty with such settings. It looks like the delimiter is Tab, so you can try xlApp.Workbooks.Open(expath, 0, true, 6... or xlApp.Workbooks.Open(expath, 0, true, 1... Even better, I suggest that you record a macro while opening such a file and check what parameters Excel uses.


    Regards from Belarus (GMT + 2),

    Andrei Smolin
    Add-in Express Team Leader
    Thursday, July 14, 2011 7:29 AM