简体   繁体   中英

Reading an dynamic Excel File with C# error

I am trying to read an excel file with C# and display every cell in the sheet inside a messagebox using the messagebox.Show() method. Problem is that my excel file has 5 rows and 3 columns. here is my excel sheet: http://postimg.org/image/xts9n1kif .

It displays everything until "roof" and after that stops leaving behind "light" and "iron", but if i fill the stuff3 column it reads everything perfectly fine. As is my case, the file may change. It may have more columns or rows in it, and some of them may be empty.

Any idea why it is not working?

Here is my code:

using System;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;

namespace ReadFromExcell
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.Load += new System.EventHandler(this.Form1_Load);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Excel.Application xlApp = new Excel.Application();
            Excel.Workbook xlWorkbook = xlApp.Workbooks.Open("C:\\Users\\User1\\Desktop\\ItemDB.xlsx");
            Excel._Worksheet xlWorksheet = xlWorkbook.Sheets[1];
            Excel.Range xlRange = xlWorksheet.UsedRange;

            int rowCount = xlRange.Rows.Count;
            int colCount = xlRange.Columns.Count;


            for (int i = 1; i <= rowCount; i++)
            {
                for (int j = 1; j <= colCount; j++)
                {
                    MessageBox.Show(xlRange.Cells[i, j].Value2.ToString());
                }
            }
            //Close the excel file after reading it.
            xlWorkbook.Close();
        }



    }
}

Most likely what happens in your code is that when the loop hits cell C4 (which is empty) xlRange.Cells[i, j].Value2 becomes null .

Trying to invoke the ToString() method (or any other method, for that matter) on a null reference will cause a NullReferenceException .

Change your code inside your loop from...

MessageBox.Show(xlRange.Cells[i, j].Value2.ToString());

...to something like that:

MessageBox.Show( (xlRange.Cells[i, j].Value2 ?? "<no value>").ToString() );

The ?? operator is handy in this situation. If the expression on the left side of ?? (that is xlRange.Cells[i, j].Value2 ) results in a null value, the ?? operator returns the value on the right-hand side of ?? instead.


(The cell C4 could contain a number of white-space characters, in which case the cell's value would not be null . But a cell with just a number of white-space characters is a rather unusual and rare occurrence.)

Now Try this..

        for (int i = 0; i <= rowCount-1; i++)
        {
            for (int j = 0; j <= xlRange.Rows[0].Columns.Count-1; j++)
            {
                MessageBox.Show(xlRange.Cells[i, j].Value2.ToString());
            }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM