troubleshooting Question

C#: How to deal with empty cells while reading excel spreadsheet

Avatar of trevor1940
trevor1940 asked on
.NET ProgrammingC#
10 Comments1 Solution86 ViewsLast Modified:
Hi I'm trying to read an excel spreadsheet I thought I cracked it until hitting empty cells

Ho do I stop this from erroring?

Error

Error 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)
   at CallSite.Target(Closure , CallSite , Object )
   at .Program.Main(String[] args) in \Program.cs:line 109

Open in new window



            string WorkBook = @"C:\Temp\MyFile.xls";
            if (File.Exists(WorkBook))
            {
                Excel.Application xlApp = new Excel.Application();



                Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(WorkBook);
                Excel._Worksheet xlWorksheet = xlWorkbook.Sheets[3];
                Excel.Range xlRange = xlWorksheet.UsedRange;

                int rowCount = xlRange.Rows.Count;
                int colCount = xlRange.Columns.Count;
                int PrefCount = 1;
                //iterate over the rows and columns and print to the console as it appears in the file
                //excel is not zero based!!
                for (int i = 3; i <= rowCount; i++)
                {
//. ...
                    string StrDescription = "";
                    try
                    {
                        if (xlRange.Cells[i, 2].Value2.ToString() != null || xlRange.Cells !=null)
                        {
                        StrDescription = xlRange.Cells[i, 2].Value2.ToString();
                    StrDescription = Regex.Replace(StrDescription, @"\r\n?|\n", " ");

                        }
                        else
                        {
                            Console.WriteLine("Description is Null");
                        }
  
                    Console.WriteLine("1 Description: {0}", StrDescription);
                }
                    catch (Exception e)
                {
                    Console.WriteLine("Error {0}", e);
                    continue;
                }

Open in new window

ASKER CERTIFIED SOLUTION
Kyle Abrahams
Director of Information Technology

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 10 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 10 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros