1,000 件以上のレコードを処理し、excel sheet
. そこで、バックグラウンド ワーカーを使用してヒットすることを考えましたが、それから抜け出し、アクションを実行せずにイベントをbackground worker
トリガーしました。background work completed
以下は私のコードです:
Private Sub btnExport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExport.Click
ProgressBar1.Maximum = 100
ProgressBar1.Step = 1
ProgressBar1.Value = 0
BackgroundWorker1.WorkerReportsProgress = True
BackgroundWorker1.RunWorkerAsync()
End Sub
Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
If cmbAccounts.SelectedIndex = 0 Then
Dim Input1 As String = Directory.GetCurrentDirectory & "\Samples\abc.xlsx"
Dim tdate As String = Me.PresentDate.Value.ToString("yyyy-MM-dd")
Using myConnection As New SqlConnection("Data Source=mydatasource;Initial Catalog=db0XXX;Persist Security Info=True;User ID=sa;Password=abcd"), myCommand As New SqlCommand("GetLog", myConnection), adapter As New SqlDataAdapter(myCommand)
myConnection.Open()
myCommand.CommandType = CommandType.StoredProcedure
myCommand.Parameters.AddWithValue("@AccountID", 123)
myCommand.Parameters.AddWithValue("@Date", tdate)
' Create the DataAdapter
Dim myDataAdapter As New SqlDataAdapter(myCommand)
' Create the DataSet
Dim myDataSet As New DataSet
' Fill the DataSet
myDataAdapter.Fill(myDataSet)
Me.DataGridView1.DataSource = myDataSet.Tables(0)
Me.DataGridView2.DataSource = myDataSet.Tables(1)
Me.DataGridView3.DataSource = myDataSet.Tables(2)
' Close the connection
myConnection.Close()
Dim xlApp As New Excel.Application
Dim xlWorkBook As Excel.Workbook
xlWorkBook = xlApp.Workbooks.Add
xlWorkBook = xlApp.Workbooks.Open(Input1)
xlWorkBook.Sheets(1).activate()
xlApp.Cells.HorizontalAlignment = XlHAlign.xlHAlignCenter
xlApp.DisplayAlerts = False
xlApp.Columns.ColumnWidth = 25
For i = 1 To myDataSet.Tables(0).Rows.Count
For j = 0 To myDataSet.Tables(0).Columns.Count - 1
xlApp.Cells(i + 1, j + 1) = _
myDataSet.Tables(0).Rows(i - 1)(j).ToString()
Next
Next
EndOfFirstTable = myDataSet.Tables(0).Rows.Count + 1
Dim SecondTableFirstRow As Integer = EndOfFirstTable + 1
For i = 1 To myDataSet.Tables(1).Rows.Count
For j = 0 To myDataSet.Tables(1).Columns.Count - 1
xlApp.Cells(i + SecondTableFirstRow, j + 1) = _
myDataSet.Tables(1).Rows(i - 1)(j).ToString()
Next
Next
EndOfSecondTable = myDataSet.Tables(1).Rows.Count + 1
Dim ThirdTableFirstRow As Integer = EndOfSecondTable + 1
For i = 1 To myDataSet.Tables(2).Rows.Count
For j = 0 To myDataSet.Tables(2).Columns.Count - 1
xlApp.Cells(i + ThirdTableFirstRow, j + 1) = _
myDataSet.Tables(2).Rows(i - 1)(j).ToString()
Next
Next
EndOfThirdTable = myDataSet.Tables(2).Rows.Count + 1
If DataGridView1.Rows.Count - 1 + DataGridView2.Rows.Count - 1 + DataGridView3.Rows.Count - 1 = 0 Then
For i = 1 To 2
For j = 0 To myDataSet.Tables(0).Columns.Count - 1
xlApp.Cells(2, j + 1) = "NULL"
Next
Next
End If
xlApp.Columns.AutoFit()
Dim rSearchRange As Range
rSearchRange = xlWorkBook.Sheets(1).UsedRange.Columns(1)
'for example
If xlApp.WorksheetFunction.CountBlank(rSearchRange) Then
rSearchRange.SpecialCells(XlCellType.xlCellTypeBlanks).EntireRow.Delete()
End If
lblChargeEntry.Text = DataGridView1.RowCount - 1 + DataGridView2.RowCount - 1 + DataGridView3.RowCount - 1
value1 = lblChargeEntry.Text
Dim _
Destinationpath As String = Directory.GetCurrentDirectory & "\Output\abc_" & tdate & ".xlsx"
xlApp.ActiveWorkbook.SaveAs(Destinationpath)
'~~> Close the File
xlWorkBook.Close()
'~~> Quit the Excel Application
xlApp.Quit()
End Using
Using myConnection As New SqlConnection("Data Source=mydatasource;Initial Catalog=db0XXX;Persist Security Info=True;User ID=sa;Password=abcd"), myCommand As New SqlCommand("GetLog", myConnection), adapter As New SqlDataAdapter(myCommand)
Dim Input2 As String = Directory.GetCurrentDirectory & "\Samples\bbc.xls"
myConnection.Open()
myCommand.CommandType = CommandType.StoredProcedure
myCommand.Parameters.AddWithValue("@AccountID", 234)
myCommand.Parameters.AddWithValue("@Date", tdate)
' Create the DataAdapter
Dim myDataAdapter As New SqlDataAdapter(myCommand)
' Create the DataSet
Dim myDataSet As New DataSet
' Fill the DataSet
myDataAdapter.Fill(myDataSet)
Me.DataGridView4.DataSource = myDataSet.Tables(0)
' Close the connection
myConnection.Close()
Dim xlApp As New Excel.Application
Dim xlWorkBook As Excel.Workbook
xlWorkBook = xlApp.Workbooks.Add
xlWorkBook = xlApp.Workbooks.Open(Input2)
xlWorkBook.Sheets(1).activate()
xlApp.Cells.HorizontalAlignment = XlHAlign.xlHAlignCenter
xlApp.DisplayAlerts = False
xlApp.Columns.ColumnWidth = 25
Try
Dim EndOfFirstTable As Integer
For i = 1 To myDataSet.Tables(0).Rows.Count
For j = 0 To myDataSet.Tables(0).Columns.Count - 1
xlApp.Cells(i + 1, j + 1) = _
myDataSet.Tables(0).Rows(i - 1)(j).ToString()
Next
Next
EndOfFirstTable = myDataSet.Tables(0).Rows.Count + 1
If DataGridView4.Rows.Count - 1 = 0 Then
For i = 1 To 2
For j = 0 To myDataSet.Tables(0).Columns.Count - 1
xlApp.Cells(2, j + 1) = "NULL"
Next
Next
End If
xlApp.Columns.AutoFit()
Catch
End Try
Dim rSearchRange As Range
rSearchRange = xlWorkBook.Sheets(1).UsedRange.Columns(1)
'for example
If xlApp.WorksheetFunction.CountBlank(rSearchRange) Then
rSearchRange.SpecialCells(XlCellType.xlCellTypeBlanks).EntireRow.Delete()
End If
lblPaymentPosting.Text = DataGridView4.RowCount - 1
value2 = lblPaymentPosting.Text
Dim _
Destinationpath As String = Directory.GetCurrentDirectory & _
"\Output\bbc_" & tdate & ".xls"
xlApp.ActiveWorkbook.SaveAs(Destinationpath)
'~~> Close the File
xlWorkBook.Close()
'~~> Quit the Excel Application
xlApp.Quit()
cmbAccounts.SelectedIndex = 1
End Using
End If
If cmbAccounts.SelectedIndex = 1 Then
Dim Input3 As String = Directory.GetCurrentDirectory & "\Samples\123.xlsx"
Dim tdate As String = Me.PresentDate.Value.ToString("yyyy-MM-dd")
Using myConnection As New SqlConnection("Data Source=mydatasource;Initial Catalog=db0XXX;Persist Security Info=True;User ID=sa;Password=abcd"), myCommand As New SqlCommand("GetLog", myConnection), adapter As New SqlDataAdapter(myCommand)
myConnection.Open()
myCommand.CommandType = CommandType.StoredProcedure
myCommand.Parameters.AddWithValue("@AccountID", 234)
myCommand.Parameters.AddWithValue("@Date", tdate)
' Create the DataAdapter
Dim myDataAdapter As New SqlDataAdapter(myCommand)
' Create the DataSet
Dim myDataSet As New DataSet
' Fill the DataSet
myDataAdapter.Fill(myDataSet)
Me.DataGridView1.DataSource = myDataSet.Tables(0)
Me.DataGridView2.DataSource = myDataSet.Tables(1)
Me.DataGridView3.DataSource = myDataSet.Tables(2)
' Close the connection
myConnection.Close()
Dim xlApp As New Excel.Application
Dim xlWorkBook As Excel.Workbook
xlWorkBook = xlApp.Workbooks.Add
xlWorkBook = xlApp.Workbooks.Open(Input3)
xlWorkBook.Sheets(1).activate()
xlApp.Cells.HorizontalAlignment = XlHAlign.xlHAlignCenter
xlApp.DisplayAlerts = False
xlApp.Columns.ColumnWidth = 25
For i = 1 To myDataSet.Tables(0).Rows.Count
For j = 0 To myDataSet.Tables(0).Columns.Count - 1
xlApp.Cells(i + 1, j + 1) = _
myDataSet.Tables(0).Rows(i - 1)(j).ToString()
Next
Next
EndOfFirstTable = myDataSet.Tables(0).Rows.Count + 1
Dim SecondTableFirstRow As Integer = EndOfFirstTable + 1
For i = 1 To myDataSet.Tables(1).Rows.Count
For j = 0 To myDataSet.Tables(1).Columns.Count - 1
xlApp.Cells(i + SecondTableFirstRow, j + 1) = _
myDataSet.Tables(1).Rows(i - 1)(j).ToString()
Next
Next
EndOfSecondTable = myDataSet.Tables(1).Rows.Count + 1
Dim ThirdTableFirstRow As Integer = EndOfSecondTable + 1
For i = 1 To myDataSet.Tables(2).Rows.Count
For j = 0 To myDataSet.Tables(2).Columns.Count - 1
xlApp.Cells(i + ThirdTableFirstRow, j + 1) = _
myDataSet.Tables(2).Rows(i - 1)(j).ToString()
Next
Next
If DataGridView1.Rows.Count - 1 + DataGridView2.Rows.Count - 1 + DataGridView3.Rows.Count - 1 = 0 Then
For i = 1 To 2
For j = 0 To myDataSet.Tables(0).Columns.Count - 1
xlApp.Cells(2, j + 1) = "NULL"
Next
Next
End If
xlApp.Columns.AutoFit()
Dim rSearchRange As Range
rSearchRange = xlWorkBook.Sheets(1).UsedRange.Columns(1) 'for example
If xlApp.WorksheetFunction.CountBlank(rSearchRange) Then
rSearchRange.SpecialCells(XlCellType.xlCellTypeBlanks).EntireRow.Delete()
End If
lblChargeEntry.Text = DataGridView1.RowCount - 1 + DataGridView2.RowCount - 1 + DataGridView3.RowCount - 1
value3 = lblChargeEntry.Text
Dim Destinationpath As String = Directory.GetCurrentDirectory & "\Output\234_Log_" & tdate & ".xlsx"
xlApp.ActiveWorkbook.SaveAs(Destinationpath )
'~~> Close the File
xlWorkBook.Close()
'~~> Quit the Excel Application
xlApp.Quit()
End Using
For j As Integer = 0 To 99999
Caluculate(j)
backgroundWorker.ReportProgress((j * 100) \ 100000)
Next
End If
End Sub
Private Sub Caluculate(i As Integer)
Dim pow As Double = Math.Pow(i, i)
End Sub
Private Sub BackgroundWorker1_ProgressChanged(sender As Object, e As ProgressChangedEventArgs)
progressBar1.Value = e.ProgressPercentage
End Sub
Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs)
MsgBox("Reports created succesfully!")
End Sub