这是 working..and我把处理代码移动到finally块,现在它每次都失败。为什么我在使用ASP .NET Excel Interop时出现内存不足错误?
我有一个测试电子表格,有4条记录,6列长。这里是我使用的代码。这是IIS 5(我的电脑)和IIS 6(Web服务器)上的ASP .Net 3.5。
它在catch之前的线上爆炸:“values =(object [,])range.Value2;”并出现以下错误:
11/2/2009 8:47:43 AM :: Not enough storage is available to complete this operation. (Exception from HRESULT: 0x8007000E (E_OUTOFMEMORY))
任何想法?建议?我从codeproject中获得了大部分代码,所以我不知道这是否是使用Excel的正确方法。感谢您的任何帮助,您可以提供。
这里是我的代码:
Excel.ApplicationClass app = null; Excel.Workbook book = null; Excel.Worksheet sheet = null; Excel.Range range = null; object[,] values = null; try { // Configure Excel app = new Excel.ApplicationClass(); app.Visible = false; app.ScreenUpdating = false; app.DisplayAlerts = false; // Open a new instance of excel with the uploaded file book = app.Workbooks.Open(path); // Get first worksheet in book sheet = (Excel.Worksheet)book.Worksheets[1]; // Start with first cell on second row range = sheet.get_Range("A2", Missing.Value); // Get all cells to the right range = range.get_End(Excel.XlDirection.xlToRight); // Get all cells downwards range = range.get_End(Excel.XlDirection.xlDown); // Get address of bottom rightmost cell string downAddress = range.get_Address(false, false, Excel.XlReferenceStyle.xlA1, Type.Missing, Type.Missing); // Get complete range of data range = sheet.get_Range("A2", downAddress); // get 2d array of all data values = (object[,])range.Value2; } catch (Exception e) { LoggingService.log(e.Message); } finally { // Clean up range = null; sheet = null; if (book != null) book.Close(false, Missing.Value, Missing.Value); book = null; if (app != null) app.Quit(); app = null; } return values;
从ASP.NET处理Excel需要麻烦。 COM组件不是设计用于多线程环境中的。 – RichardOD 2009-11-02 18:58:38
或任何服务器环境。 Office interop是steriods上的办公宏指令 - 它们*可以*用于可编程访问办公文档,但它不是一种非常可靠的方法。 – David 2009-11-02 19:01:39
够公平的。这可能需要另一个问题,但是如何让用户上传.xls文档从中获取值呢?我不认为这会很难把它变成一个平面文件... – IronicMuffin 2009-11-02 19:03:34