這是 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