EPPlus를 사용하여 DataGridview 데이터를 Excel 파일로 전송합니다. 문제는 프로세스가 메모리를 먹고 있다는 것입니다. 사용 된 메모리를 확보하려면 어떻게해야합니까? 외부에서는 수천 줄의 행이 있지만 프로그램에서 사용하는 메모리가 증가하고 내보내기 및 저장 후 Excel로 되돌아 가지 않습니다. 이제 백만 줄을 수출하려고 할 때 나는 추억을 잃는다.DataGridview 데이터를 Excel로 전송
내 코드는 다음과 같습니다.이 프로세스는 백그라운드 작업자에서 실행됩니다. 그것이 using
블록 쌌다 이후로
Using p = New ExcelPackage
Dim sheetnum As Integer = 2
Dim ws As ExcelWorksheet = CreateSheet(p, "report")
For Each dgcol As DataGridViewColumn In dg.Columns
ws.Cells(1, col).Value = dgcol.HeaderText
col += 1
Next
For Each rowx As DataGridViewRow In dg.Rows
For Each colx As DataGridViewColumn In dg.Columns
ws.Cells(row, colx.Index + 1).Value = dg.Rows(rowx.Index).Cells(colx.Index).Value
Next
row += 1
BackgroundWorker1.ReportProgress(CInt(100 * Integer.Parse(rowx.Index + 1)/dg.Rows.Count), CInt(100 * Integer.Parse(rowx.Index + 1)/dg.Rows.Count))
If row = 1048577 Then 'Check if max rows have been reached and create a new sheet
ws = CreateSheet(p, "report" & sheetnum)
sheetnum += 1
row = 2
col = 1
For Each dgcol As DataGridViewColumn In dg.Columns
ws.Cells(1, col).Value = dgcol.HeaderText
col += 1
Next
End If
Next
Dim bin() As Byte = p.GetAsByteArray()
File.WriteAllBytes(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\" & "report.xlsx", bin)
End Using
작업을 마친 후에는 ['ExcelWorksheet'] (https://epplus.codeplex.com/SourceControl/latest#EPPlus/ExcelWorksheet.cs)를 삭제해야합니다. –
@RezaAghaei 제안 해 주셔서 감사합니다. 그러나 작동하지 않았습니다. – crimson589
전체 문제를 해결할 수 있을지 모르겠지만 'ExcelWorksheet' 클래스의'Dispose' 메서드 구현을 살펴 본다면 코드가 많은 리소스를 해제하고 코드 제작자가 더 잘 알 것입니다. 우리는 어떤 자원을 공개해야합니다. –