简体   繁体   English

使用 ExcelLibrary 在 C# 中读取 Excel .xlsx 文件时出现 OutOfMemoryException

[英]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# .我正在尝试使用C#在 Visual Studio Windows 应用程序中读取 excel 文件 (.xlsx)。 I am using the Google Excel Library from this link ExcelLibrary .我正在使用此链接ExcelLibrary 中Google Excel Library I used the following code to read from excel file on button click.我使用以下代码在单击按钮时从 excel 文件中读取。 I'm using the following code in Visual Studio 2012 Professional,我在 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.但是当我运行代码并单击按钮时,它提供了OutOfMemoryException was unhandled异常。 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-catch

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.我试图读取的 excel 文件只有 18 KB,因此内存不足是不可能的。 I am not sure why i get this exception.我不确定为什么会出现此异常。

Since you are working with an ".xlsx" file you should use the "Microsoft.Office.Interop.Excel" and "Office" references from GAC.由于您使用的是“.xlsx”文件,因此您应该使用 GAC 中的“Microsoft.Office.Interop.Excel”和“Office”引用。

These should be already installed on your computer assuming that you have the Microsoft Office already installed on your computer.假设您的计算机上已经安装了 Microsoft Office,这些应该已经安装在您的计算机上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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