2017-02-22 1 views
-1

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 
+0

작업을 마친 후에는 ['ExcelWorksheet'] (https://epplus.codeplex.com/SourceControl/latest#EPPlus/ExcelWorksheet.cs)를 삭제해야합니다. –

+0

@RezaAghaei 제안 해 주셔서 감사합니다. 그러나 작동하지 않았습니다. – crimson589

+0

전체 문제를 해결할 수 있을지 모르겠지만 'ExcelWorksheet' 클래스의'Dispose' 메서드 구현을 살펴 본다면 코드가 많은 리소스를 해제하고 코드 제작자가 더 잘 알 것입니다. 우리는 어떤 자원을 공개해야합니다. –

답변

0

상기 ExcelPackage 목적은 적절하게 배치된다. 당신은 또한 댓글에 ExcelWorksheet 객체를 처리했으나 제대로 작동하지 않는다고 말했습니까?

그러나, 나는 당신이 시도는합니다 (End Using 전) 코드의 마지막 줄에 파일에 쓸 바이트 배열

을 폐기하지 않는 것이라고 생각합니다. 희망이 도움이됩니다.

+0

바이트에 대해 '.dispose'가 없습니다 – crimson589

+0

다음을 읽어보십시오. http://stackoverflow.com/questions/13033684/how-do-i-properly-dispose-the-byte-array-used-in-the-class - 기본적으로 가비지 수집기를 호출하고 수집하도록 요청할 수 있습니다. –

관련 문제