3

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.

8
  • I'm concerned about those loops. Which line is it getting to and causing the error? Commented Sep 20, 2016 at 19:15
  • @KSib im getting OutofMemoryException at this line WorkBook inputBook = Workbook.Load("D:\\Files\\input.xlsx"); Commented Sep 20, 2016 at 19:22
  • What version of Office is the file created with? According to the documentation the xlsx format isn't even supported (yet): Currently .xls (BIFF8) format is implemented. In future .xlsx (Excel 2007) may also be supported. See code.google.com/archive/p/excellibrary Commented Sep 20, 2016 at 19:25
  • @DangerZone yes i tried to open a simple file with one row also and it wont work shows same error. Commented Sep 20, 2016 at 19:28
  • That's odd. I'd also be concerned about the date of the last commit of that repository (2013) as it's not being supported anymore. Might just be a bug for too many rows, too many formulas, etc. Have you tried it with a more up-to-date library such as EPPlus? epplus.codeplex.com Commented Sep 20, 2016 at 19:29

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.