-1

I am getting below exception in my application at line:

Workbook workbook = new Workbook(fstream);

Exception:

Exception of type 'System.OutOfMemoryException' was thrown.

C# Code:

string tempPath = @"C:\File\01_TDv01.xlsx";
foreach (string templateFile in Directory.GetFiles(tempPath))
{
    using (FileStream fstream = new FileStream(templateFile, FileMode.Open))
    {                    
         Workbook workbook = new Workbook(fstream);     //getting exception here
         Worksheet worksheet = workbook.Worksheets[0];
         ArrayList List = new ArrayList();

    //other code
    }
}
catch (Exception ex)
{

}

I am processing the Excel file of size 38,436 KB; with 10K rows in it.

Web Config Settings:

<add key="maxFileSizeLimit" value="2147483647" />

<httpRuntime targetFramework="4.5"  maxQueryStringLength="52768" enable="true" maxRequestLength="2147483647"/>

<requestLimits maxQueryString="52768" maxAllowedContentLength="4294967295" />

What is wrong in my code and how can I resolve this exception?

I can't make lot of changes in code as it is already live.

halfer
  • 19,824
  • 17
  • 99
  • 186
timz_123
  • 435
  • 1
  • 9
  • 47
  • 1
    https://docs.aspose.com/display/cellsnet/Aspose.Cells+FAQs#Aspose.CellsFAQs-HowtoFixtheSystem.OutOfMemoryExceptionwhileLoadingLargeSpreadsheets%3F – user6144226 Jul 04 '17 at 06:53
  • have you check on which iteration of loop exception has been thrown? – Nisarg Desai Jul 04 '17 at 08:20
  • @NisargDesai: on 1st iteration itself the exception is thrown. – timz_123 Jul 04 '17 at 08:50
  • what you want to do with this excel file? by c#. – Nisarg Desai Jul 04 '17 at 08:52
  • I would like to suggest if you want to read/write or modify data of excel than to hold entire stream of file in one object will be bad idea. you can use OLEDB for performing operation on excel worksheet. it will be more easy to processing as well as you can do all the stuff with sql. and you no need to held memory for file. https://www.codeproject.com/Tips/705470/Read-and-Write-Excel-Documents-Using-OLEDB – Nisarg Desai Jul 04 '17 at 08:58
  • @NisargDesai: I have to read the data of excel, and save it into DB. I am using Aspose.Cells for operations on excel worksheet. I can not change much into application as it is already went live. Kindly suggest. I have also referred this link for the exception I am facing: https://docs.aspose.com/display/cellsnet/Aspose.Cells+FAQs#Aspose.CellsFAQs-HowtoFixtheSystem.OutOfMemoryExceptionwhileLoadingLargeSpreadsheets%3F – timz_123 Jul 04 '17 at 09:02
  • well in case of this you can only go with the example as they provided using parameterized constructor of workbook class. pass LoadOptions object with setting MemorySetting property of that to MemorySetting.MemoryPreference; and pass into workbook constructor. in short copy and replace their code if it could not solve than there is bug in their class library. – Nisarg Desai Jul 04 '17 at 09:12
  • @NisargDesai: Thank you for the help. As per this line "There are fair chances that the Workbook constructor may throw System.OutOfMemoryException while loading large spreadsheets" What is the default size of excel sheet or excel workbook to be read/operated using Aspose.Cells in .Net ? – timz_123 Jul 05 '17 at 02:24
  • @NisargDesai: Also, I am getting this exception sometimes only not always, please let me know why ? – timz_123 Jul 05 '17 at 04:13
  • @user3196511 sorry for late rely this memory exceptions only get thrown when processors are taking heavy load and there are not that much available memory in RAM so if your OS is currently running too many concurrent process and among them if particular process is too large than system will throw exception as OS not able to maintain execution pointers in ram memories process queue as there is no more memories. hope that helps. – Nisarg Desai Jul 08 '17 at 06:51

1 Answers1

1

@user3196511

Could you provide us your template file "01_TDv01.xlsx". We need to evaluate your issue using your template file. Please also make sure you have sufficient memory (e.g 10 times or more memory of the size of the file) available for the big process as you are loading a large file We also recommend you to kindly try our latest version/fix: Aspose.Cells for .NET v17.6.x (if you are not already using it). Moreover, you should try to set MemoryPreference attribute on while loading your template file, see the following sample code for your reference: e.g Sample code:

        //Specify the LoadOptions            
        LoadOptions opt = new LoadOptions();
        //Set the memory preferences
        opt.MemorySetting = MemorySetting.MemoryPreference;

        using (FileStream fstream = new FileStream(templateFile, FileMode.Open))
{                    
     Workbook workbook = new Workbook(fstream, opt);     //getting exception here
     Worksheet worksheet = workbook.Worksheets[0];


//other code
}

I am working as Support developer/ Evangelist at Aspose.

Amjad Sahi
  • 1,813
  • 1
  • 10
  • 15
  • Thank you so much for the explanation. In my web application, I am using Aspose.Cells for .NET, Version = 7.0.4.0 Also, above exception is thrown sometimes not always. Application is deployed on Server which is 64 bit and its Installed memory (RAM) is 64 GB Excel File is sitting in server's C drive (204 GB free of 279 GB). Excel File has 100K rows and 225 columns. sorry to say but due to security issues, I cant share the excel. – timz_123 Jul 06 '17 at 04:31
  • Could you try using our latest version: Aspose.Cells for .NET v17.6 (you may download it from Downloads module in Aspose site )? You may use it with v17.6.x in a separate simple application with the above suggested code if it makes any difference. – Amjad Sahi Jul 06 '17 at 09:17