0

So, im trying to make an Excel documents parser, and everything goes fine, until it hits empty cell in Excel. Then it throws an exception *"Microsoft.CSharp.RuntimeBinder.RuntimeBinderException" occured in System.Core.dll"

namespace ExcelParser {
    class Program
    {
        static void Main(string[] args)
        {
            Excel.Application excelApp = new Excel.Application();
            excelApp.Visible = true;

            string _sourceFile = "F:\\Bullshit\\book1.xlsm";

            excelApp.Workbooks.Open(_sourceFile);

            int row = 1;
            Excel.Worksheet currentSheet = (Excel.Worksheet)excelApp.Workbooks[1].Worksheets[1];
            Console.WriteLine("Initializing");
            while (currentSheet.get_Range("A" + row).Value2 != null)
            {
                List<string> tempList = new List<string>();
                for (char column = 'A'; column < 'J'; column++)
                {
                    Console.Write(column + row.ToString());
                    Excel.Range cell = currentSheet.get_Range(column + row.ToString());
                    Console.WriteLine(cell.Value2.ToString() != "" ? cell.Value2.ToString() : "null!"); // the problem line
                }
                row++;
            }
            Console.ReadKey();
        }
    }
}
Zsw
  • 3,920
  • 4
  • 29
  • 43
SLy_huh
  • 145
  • 2
  • 16

3 Answers3

0

You cannot cast ToString method of an object if you don't have an object. It'll return null, but null is not he same as "" try using this line:

Console.WriteLine(("" + cell.Value2).ToString() != "" ? cell.Value2.ToString() : "null!");

Marek Židek
  • 809
  • 1
  • 15
  • 31
  • Nope, still got the same exception. It seems to be, that even empty cells aren't equal null. So maybe it has some specific data type, or something? Even tho it should return string – SLy_huh Aug 19 '15 at 13:29
  • Well it is an object, so it can be null. I suggest trying to add if statement to check whether `cell.Value2 == null`. If that doesn't help, you could try using `try` scope and `catch(RuntimeBinderException)` and write there what you wanna do with empty cells. If that doesn't help, I'm sorry :/ – Marek Židek Aug 19 '15 at 13:44
  • I also suggest to Click on View Details on the exception and provide it to your question as the default error message just usually tells only the exception type – Marek Židek Aug 19 '15 at 13:55
  • `"" + cell.Value2.ToString()` is still calling ToString on the null Value2. I guess you meant `("" + cell.Value2).ToString()` (in which case you don't need that ToString) – Hans Kesting Aug 20 '15 at 07:26
0

A quick and dirty way to get rid of the exception is just

try
{
   Console.WriteLine(cell.Value2.ToString() != "" ? cell.Value2.ToString(): "null!");
}
catch(exception e)
{
  Console.WriteLine(e.Argument);
}
Josh Davis
  • 90
  • 8
0

I guess that cell.Value2 is null for an empty cell. In that case you can't call .ToString() on it.

You can however check for it:

Console.WriteLine(cell.Value2 != null ? cell.Value2.ToString() : "null!");
Hans Kesting
  • 38,117
  • 9
  • 79
  • 111