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.
OutofMemoryExceptionat this lineWorkBook inputBook = Workbook.Load("D:\\Files\\input.xlsx");