我不知道为什么我在SharpDevelop中为我的C#程序获得System.OutOfMemoryException
。我的程序打开一个Excel工作表并处理工作表中的一些数据以检查重复项。
以下是完整的异常错误消息:
System.Runtime.InteropServices.COMException: See inner exception(s) for details. ---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.OutOfMemoryException: Not enough storage is available to complete this operation. (Exception from HRESULT: 0x8007000E (E_OUTOFMEMORY))
at static Object NetOffice.Invoker.PropertyGet(NetOffice.COMObject comObject, System.String name, System.Object[] paramsArray)
at Object NetOffice.ExcelApi.Range.get_Value()
at System.Void excelApp.Program.markDuplicates() in c:\Users\HP\Documents\SharpDevelop Projects\excelApp\excelApp\Program.cs:line 80
at static System.Void excelApp.Program.Main(System.String[] args) in c:\Users\HP\Documents\SharpDevelop Projects\excelApp\excelApp\Program.cs:line 41
这是我的完整计划:
它指向markDuplicates()
方法中的以下行:
第80行:
Object[,] valuesArray = (Object[,])tableRange.Value;
我不知道为什么我会得到这个例外。
我正在使用.NET Framework 4.5.1。
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Input.Manipulations;
using NetOffice.ExcelApi;
using NetOffice.ExcelApi.Enums;
using Excel = NetOffice.ExcelApi.Application;
namespace excelApp
{
class Program
{
Excel excelApplication;
Workbook workbook;
Worksheet sheet;
HashSet<int> mpanHashCodeList;
[STAThreadAttribute]
public static void Main(string[] args)
{
Program p = new Program();
p.openWorkSheet(@"C:\Users\HP\Desktop\Book1.xlsx", 2);
p.markDuplicates();
Console.ReadKey(true);
}
private void openWorkSheet(string path, int worksheet)
{
excelApplication = new Excel
{
Visible = true,
ScreenUpdating = true
};
try
{
workbook = excelApplication.Workbooks.Open(path);
sheet = (Worksheet)workbook.Worksheets[worksheet];
}
catch
{
Console.WriteLine("File does not exist");
}
}
private void markDuplicates()
{
Range range = sheet.Cells[2,2];
Range rngLastCell = range.get_End(XlDirection.xlToRight)
.get_End(XlDirection.xlDown);
// holds the range of cells in the worksheet
Range tableRange = sheet.Range(range, rngLastCell);
// holds all the values of the range of cells in the worksheet
Object[,] valuesArray = (Object[,])tableRange.Value;
mpanHashCodeList = new HashSet<int>();
int count = 0;
for(var i = 1; i <= valuesArray.GetUpperBound(0); i++)
{
// create a new string for each row
var rowIdBuilder = new StringBuilder(10);
for(var j = 1; j <= valuesArray.GetUpperBound(1); j++)
{
switch(j)
{
case 1:
rowIdBuilder.Append(valuesArray[i,j].ToString());
break;
case 3:
rowIdBuilder.Append(valuesArray[i,j].ToString());
break;
case 4:
rowIdBuilder.Append(valuesArray[i,j].ToString());
break;
case 6:
rowIdBuilder.Append(valuesArray[i,j].ToString());
break;
}
}
Console.WriteLine(rowIdBuilder.ToString());
int hashcode = rowIdBuilder.ToString().GetHashCode();
if(mpanHashCodeList.Contains(hashcode))
{
count++;
mpanHashCodeList.Remove(hashcode);
}
else
{
mpanHashCodeList.Add(hashcode);
}
}
Console.WriteLine(count + " duplicates found");
}
}
}
答案 0 :(得分:0)
本机代码返回错误代码0x8007000E(E_OUTOFMEMORY)非常常见,然后将其映射到托管代码中的OutOfMemory异常。例如,当本机堆分配失败或句柄分配失败时,这是常见的。
在Visual Studio中使用混合模式调试最好诊断出此类问题。专业人士通常会使用像WinDbg / cdb这样的工具来正确解析这些正确的符号。
简而言之:没有简单的答案。预计会有肮脏的工作。