NPOI icon indicating copy to clipboard operation
NPOI copied to clipboard

OutOfMemoryException in large files

Open LeonardoX77 opened this issue 6 years ago • 2 comments

I'm trying to save big amounts of data (between 60000 and 200000 records) but I get an OutOfMemoryException sometimes in MemoryPackagePartOutputStream.Write(byte[] b) method, some other in XSSFRichTextString.UtfDecode(String value) when I'm near 191000 records.

System.OutOfMemoryException: 'Exception of type 'System.OutOfMemoryException' was thrown.'

I tried to save ISheet every 30000 records, something like this, but it always saves the first 30000 rows. How can I do it?

        string filename = $"c:\\temp\\{tTask.Name}.xlsx";

        using (ApplicationDbContext db = new ApplicationDbContext())
        {
            IWorkbook workbook = new XSSFWorkbook();
            var sql = "Select * From View_TradingResults";
            List<Model> tTasks = db.Database.SqlQuery<Model>(sql).ToList();

            if (tTasks.Count > 0)
            {
                if (File.Exists(filename)) File.Delete(filename);
                FileStream out1 = new FileStream(filename, FileMode.Create, FileAccess.ReadWrite);

                string[] colNames = new string[] { "TradingTaskId", "Name", "TestId" };
                ISheet sheet = workbook.CreateSheet("Sheet1");

                foreach (var dataRow in tTasks.Take(50000))
                {
                    int idCol = 0;
                    IRow row = sheet.CreateRow(rowNumber++);

                    foreach (var colName in colNames)
                    {
                        var cell = row.CreateCell(idCol++);
                        cell.SetCellValue(dataRow[colName].ToString());
                    }
                }
                workbook.Write(out1);

                //get next amount of data, repeat previous loop with next records...
                foreach (var dataRow in tTasks.Skip(50000).Take(50000))
                {
                    IRow row = sheet.CreateRow(rowNumber++);
                    ....
                }
                workbook.Write(out1);
                out1.Close();
            }
        }

LeonardoX77 avatar Feb 19 '19 19:02 LeonardoX77

same issue for me.

aajmot avatar Feb 21 '20 11:02 aajmot

In my case was related iis express 32 bit, i solved launching iis64 bit

Il ven 21 feb 2020, 12:54 Ajmot Sk [email protected] ha scritto:

same issue for me.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/dotnetcore/NPOI/issues/111?email_source=notifications&email_token=AOQ6EVSZ4344NY4II6MXIPLRD66HLA5CNFSM4GYO25YKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEMSO4OY#issuecomment-589622843, or unsubscribe https://github.com/notifications/unsubscribe-auth/AOQ6EVSYH2FRU2JZY5K34XTRD66HLANCNFSM4GYO25YA .

Oipe85 avatar Feb 21 '20 11:02 Oipe85