NPOI C#: best way from Workbook to byte[]

532 Views Asked by At

We have done an utility method to return a byte[] from an IWorkBook (NPOI).

public static byte[] WorkbookToByteArray(IWorkbook workbook, bool flush = false)
{
    using (var ms = new MemoryStream())
    {
        workbook.Write(ms);
        if (flush) ms.Flush();
        return ms.ToArray();
    }
}

Now we have 2 questions:

  1. Can we remove the Flush line for the MemoryStream? There is an using, so perhaps...
  2. How can we free RAM at the end? At the moment, seems that the GC wait to clean the workbook from memory.

EDIT: More information about RAM situation (the example here is with a very small Excel, about 259KB - to save time, normally we have huge generation of Excel - like 25-30MB or over). snapshot1

snapshot2

0

There are 0 best solutions below