2012-11-15 15 views
7

Viene visualizzato un errore System.com_object se sto cercando di leggere la cella EXCEL che è vuota. Il mio codice è:Come leggere un valore di cella vuoto usando EXCEL interop api in C# .net?

public static List<OrderPC> getFilters(string fileCheckout) 
    { 
     List<OrderPC> orderPCs = new List<OrderPC>(); 
     XLDoc sldoc = new XLDoc(); 

     string localPath = @"C:\Temp\PCs.xlsx"; 

     Microsoft.Office.Interop.Excel.Application oXL=null; 
     Microsoft.Office.Interop.Excel.Workbook mWorkBook=null; 
     Microsoft.Office.Interop.Excel.Worksheet mWSheet1=null; 
     Microsoft.Office.Interop.Excel.Range xlRange=null; 
     try 
     { 
      oXL = new Microsoft.Office.Interop.Excel.Application(); 

      mWorkBook = oXL.Workbooks.Open(localPath); 

      mWSheet1 = mWorkBook.Sheets[1]; 

      xlRange = mWSheet1.UsedRange; 

      foreach (Microsoft.Office.Interop.Excel.Hyperlink hl in xlRange.Hyperlinks) 
      { 

       int y = hl.Range.Column; 

       int z = hl.Range.Row; 

       string vFilter = mWSheet1.Cells[z, y + 1].Value2.Trim(); 

       if (vFilter.CompareTo("Weekly") == 0) 
       { 
        String baseUri = "http://xxx.yyy.net?"; 
        int followUpIndex = baseUri.Length; 
        OrderPC orderPc = new OrderPC(); 
        orderPc.ProductClass = hl.TextToDisplay.Trim(); 
        orderPc.HyperLink = hl.Address.Trim().Substring(followUpIndex); 
        orderPc.SpecType = mWSheet1.Cells[z, y - 1].Value2.Trim(); 
        if (mWSheet1.Rows[z].Cells[y + 3] != null || mWSheet1.Rows[z].Cells[y + 3].Value2 != string.Empty) 
         { 
          orderPc.ManufactureDate = mWSheet1.Cells[z, y + 3].Value2.ToString(); //Here is the error** 
         } 
        //Console.WriteLine(orderPc.ProductClass+"----"+orderPc.HyperLink); 

        orderPCs.Add(orderPc); 
       } 
      } 
     } 
     catch (Exception ex) 
     { 
     } 
     finally 
     { 
      GC.Collect(); 
      GC.WaitForPendingFinalizers(); 

      Marshal.FinalReleaseComObject(xlRange); 
      Marshal.FinalReleaseComObject(mWSheet1); 

      mWorkBook.Close(Type.Missing, Type.Missing, Type.Missing); 
      Marshal.FinalReleaseComObject(mWorkBook); 

      oXL.Quit(); 
      Marshal.FinalReleaseComObject(oXL); 
     } 
     return orderPCs; 
    } 

Questo file excel ha 10 colonne e suppongo che sto leggendo una cella valida. L'errore è ** "

{Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: Cannot perform runtime binding on a null reference
at CallSite.Target(Closure , CallSite , Object)
at System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,TRet](CallSite site, T0 arg0)

" ** Non ho alcun indizio fin dalla sua COM. L'aiuto è molto apprezzato.

+0

Puoi convertire questa stringa fino ad oggi: mWSheet1.Cells [ z, y + 3] .Value2.ToString(); e poi provarlo ?! – Sylca

+0

Getta l'errore sopra citato se faccio qualcosa con le celle vuote. – Sulekha

risposta

11

Aggiungere un altro controllo

if (mWSheet1.Cells[z, y + 3].Value2 != null) 

o convertire in stringa utilizzando il seguente codice, perché non sarebbe fallire se Valore2 è nullo

orderPc.ManufactureDate = Convert.ToString(mWSheet1.Cells[z, y + 3].Value2); 
+1

Ha funzionato .. ho anche provato orderPc.ManufactureDate = "" + mWSheet1.Cells [z, y + 3] .Value2; e il suo funzionamento..grazie però .. – Sulekha

+1

Convert.ToString invece di .ToString() ha funzionato per me. Questo in realtà risolverà questo problema per me in molti altri posti da quando non ero a conoscenza di questo. Grazie! –