dotnetcore / NPOI

A .NET library for reading and writing Microsoft Office binary and OOXML file formats.
Apache License 2.0
1.93k stars 413 forks source link

Memory leak issue #166

Open limengning opened 4 years ago

limengning commented 4 years ago

Use NPOI in dotnet core web api to create excel file. The memory usage of the website is always rising.

phucnh1993 commented 4 years ago

image I run on K8S and use NPOI read file Excel from a Stream

My Code: var checkFile = true; ISheet sheet; IFont font; using (MemoryStream ms = new MemoryStream()) { var data = Convert.FromBase64String(file.FileBase); await ms.WriteAsync(data, 0, data.Length); ms.Position = 0; var temp = Path.GetExtension(file.FileName); if (Path.GetExtension(file.FileName).Equals(".xls")) { var book = new HSSFWorkbook(ms); font = book.CreateFont(); sheet = book.GetSheet("Sheet1"); } else { var book = new XSSFWorkbook(ms); font = book.CreateFont(); sheet = book.GetSheet("Sheet1"); } int rowIndex = 1; IRow rowData; font.Color = HSSFColor.Red.Index; var listThread = new List(); List hashCodes = new List(); MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider(); while ((rowData = sheet.GetRow(rowIndex)) != null) { rowIndex++; string dataDup = string.Format("{0}{1}{2}{3}", rowData.GetCell(numberCell).ToString(), rowData.GetCell(numberCell).ToString(), rowData.GetCell(numberCell).ToString(), rowData.GetCell(numberCell).ToString()); colNum = null; colStr = null; colLen = null; rowData = null; } ms.Flush(); ms.Close(); }

phucnh1993 commented 4 years ago

hình ảnh Tôi chạy trên K8S và sử dụng NPOI đọc tệp Excel từ Luồng

Mã của tôi: var checkFile = true; Bảng ISheet; Phông chữ IFont; bằng cách sử dụng (MemoryStream ms = new MemoryStream ()) { var data = Convert.FromBase64String (file.FileBase); đang chờ ms.WriteAsync (dữ liệu, 0, dữ liệu. Độ dài); ms.P Vị trí = 0; var temp = Path.GetExtension (file.FileName); if (Path.GetExtension (file.FileName) .Equals (". xls")) { var book = new HSSFWorkbook (ms); font = book.CreateFont (); sheet = book.GetSheet ("Sheet1"); } khác { var book = new XSSFWorkbook (ms); font = book.CreateFont (); sheet = book.GetSheet ("Sheet1"); } int row Index = 1; IRow hàngData; font.Color = HSSFColor.Red. Index; var listThread = Danh sách mới (); Danh sách hashCodes = Danh sách mới (); MD5CryptoServiceProvider md5 = MD5CryptoServiceProvider (); while (rowData = sheet.GetRow (row Index))! = null) { rowIndex ++; chuỗi dataDup = string.Format ("{0} {1} {2} {3}", rowData.GetCell (numberCell) .ToString (), rowData.GetCell (numberCell) .ToString (), rowData.GetCell (numberCell) .ToString (), rowData.GetCell (numberCell) .ToString ()); colNum = null; colStr = null; colLen = null; hàngData = null; } ms.Flush (); ms. Đóng (); }

I user DotNetCore 3.1