Quantcast
Channel: Excel Data Reader - Read Excel files in .NET
Viewing all articles
Browse latest Browse all 448

New Post: Excel sheet to array

$
0
0
Hi,
Me again.

So if I understood well the iDataReader feature I commited this piece of code (the DataSet remained for calculating number of rows and columns):
FileStream stream = File.Open(ExcelSourceFile, FileMode.Open, FileAccess.Read);
            IExcelDataReader excelReader = Path.GetExtension(ExcelSourceFile).ToLower() == ".xls" ? ExcelReaderFactory.CreateBinaryReader(stream) :ExcelReaderFactory.CreateOpenXmlReader(stream);
            
            DataSet ds = new DataSet();
            ds = excelReader.AsDataSet();
            rows = ds.Tables[0].Rows.Count;
            cols = ds.Tables[0].Columns.Count;

            int row=0;
            while (excelReader.Read())
            {
                for (int column = 0; column<=cols; column++)
                {
                    DataFromExcel[row, column] = excelReader[column].ToString();
                } // this is where code breaks with error: System.NullReferenceException
                row++;
            }
            // DataFromExcel[i, j] =
            excelReader.Close();
The error of this code says: System.NullReferenceException. Unfortunately I use VS Express so the debug tool is almost inexisting. At the top of the class there is a declaration:
object[,] DataFromExcel;
What I'm missing?

Best regards,
Mike.

Viewing all articles
Browse latest Browse all 448

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>