OutOfMemoryException when reading Excel .xlsx file in C# using ExcelLibrary

I'm trying to read an excel file (.xlsx) in Visual studio Windows application using C#. I am using the Google Excel Library from this link ExcelLibrary. I used the following code to read from excel file on button click. I'm using the following code in Visual Studio 2012 Professional,

using ExcelLibrary.SpreadSheet;
using ExcelLibrary.BinaryDrawingFormat;
using ExcelLibrary.BinaryFileFormat;
using ExcelLibrary.CompoundDocumentFormat;

namespace ExcelRead
{
    public partial class ReadForm : Form
    {
        public ReadForm()
        {
            InitializeComponent();
        }

        private void btnRead_Click(object sender, EventArgs e)
        {
            WorkBook inputBook = Workbook.Load("D:\\Files\\input.xlsx");
            List<Worksheet> sheetList = inputBook.Worksheets;
            for (int i = 0; i < sheetList.Count; i++)
            {
                Worksheet sheet = inputBook.Worksheets[i];
                for (int rowIndex = sheet.Cells.FirstRowIndex; rowIndex <= sheet.Cells.LastRowIndex; rowIndex++)
                {
                    Row row = sheet.Cells.GetRow(rowIndex);
                    for (int colIndex = row.FirstColIndex; colIndex <= row.LastColIndex; colIndex++)
                    {
                        Cell cell = row.GetCell(colIndex);
                        Console.WriteLine(cell.ToString());
                    }
                }
            }            
        }
    }
}

But it provides OutOfMemoryException was unhandled exception when i run the code and click the button. It shows the following description in the exception,

An unhandled exception of type 'System.OutOfMemoryException' occurred in mscorlib.dll

I tried using try-catch like below,

try
{
    WorkBook inputBook = Workbook.Load("D:\\Files\\input.xlsx");
    List<Worksheet> sheetList = inputBook.Worksheets;
    for (int i = 0; i < sheetList.Count; i++)
    {
        Worksheet sheet = inputBook.Worksheets[i];
        for (int rowIndex = sheet.Cells.FirstRowIndex; rowIndex <= sheet.Cells.LastRowIndex; rowIndex++)
        {
            Row row = sheet.Cells.GetRow(rowIndex);
            for (int colIndex = row.FirstColIndex; colIndex <= row.LastColIndex; colIndex++)
            {
                Cell cell = row.GetCell(colIndex);
                Console.WriteLine(cell.ToString());
            }
        }
    }
}
catch (Exception err)
{
    Console.WriteLine(err);
}

But again it says the following error,

Adding a 'catch clause' around an active statement will prevent the debug session from continuing while Edit and Continue is enabled

The excel file i am trying to read is only 18 KB so out of memory is not even possible. I am not sure why i get this exception.

Answers


Since you are working with an ".xlsx" file you should use the "Microsoft.Office.Interop.Excel" and "Office" references from GAC.

These should be already installed on your computer assuming that you have the Microsoft Office already installed on your computer.


Need Your Help

is vptr name mangled in c++?

c++ virtual-functions

Even though there are so many articles on how virtual functions are handled in c++, i could not clear one basic doubt. Is vptr variable of derived class overrided by base class variable. Is the ptr