简体   繁体   English

进度条,用于将sql大量导出到excel进程

[英]progress bar for large export of sql to excel process

I need to create a progress bar for my export process. 我需要为导出过程创建一个进度条。

I already have a code for exporting. 我已经有用于导出的代码。 However, the larger the data to be exported the longer the system hangs and won't provide information to user that it is doing something. 但是,要导出的数据越大,系统挂起的时间就越长,并且不会向用户提供它正在做某事的信息。 what i needed is to create a progress bar to at least inform the user that a process is being completed. 我需要创建一个进度条,以至少通知用户该过程已完成。

I don't know where to put it and what to put. 我不知道在哪里放什么。

I am using VS 2013 Ultimate and Excel 2013.. here is my code for export. 我正在使用VS 2013 Ultimate和Excel2013。这是我的导出代码。

Try
    Dim xlApp As Microsoft.Office.Interop.Excel.Application
    Dim xlWorkBook As Microsoft.Office.Interop.Excel.Workbook
    Dim xlWorkSheet As Microsoft.Office.Interop.Excel.Worksheet
    Dim misValue As Object = System.Reflection.Missing.Value
    Dim i As Integer
    Dim j As Integer

    xlApp = New Microsoft.Office.Interop.Excel.Application
    xlWorkBook = xlApp.Workbooks.Add(misValue)
    xlWorkSheet = xlWorkBook.Sheets("sheet1")

    For i = 0 To DataGridView1.RowCount - 2
        For j = 0 To DataGridView1.ColumnCount - 1
            For k As Integer = 1 To DataGridView1.Columns.Count
                xlWorkSheet.Cells(1, k) = DataGridView1.Columns(k - 1).HeaderText
                xlWorkSheet.Cells(i + 2, j + 1) = DataGridView1(j, i).Value.ToString()
            Next
        Next
    Next

    xlWorkSheet.SaveAs("D:\NGPdata.xlsx")
    xlWorkBook.Close()
    xlApp.Quit()

    releaseObject(xlApp)
    releaseObject(xlWorkBook)
    releaseObject(xlWorkSheet)
Catch ex As Exception
    MsgBox(ex.Message & " here")
End Try
Try
    Dim res As MsgBoxResult
    res = MsgBox("Process completed, Would you like to open file?", MsgBoxStyle.YesNo)
    If (res = MsgBoxResult.Yes) Then
        Process.Start("D:\NGPdata.xlsx")
    End If
Catch ex As Exception
    MsgBox(ex.Message)
End Try

as well as the code it required. 以及所需的代码。

Private Sub releaseObject(ByVal obj As Object)
    Try
        System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
        obj = Nothing
    Catch ex As Exception
        obj = Nothing
    Finally
        GC.Collect()
    End Try
End Sub
  1. Insert a ProgressBar in the Form. 在表单中插入一个ProgressBar。
  2. Modify properties of ProgressBar 修改ProgressBar的属性

     value = 0 Maximun = 100 

And your code should be: 您的代码应为:

    ...
    Dim Increment as Single
    Dim IntMax, currentIndex as Long
    currentIndex = 0
    intMax = (DataGridView1.RowCount - 2) * (DataGridView1.ColumnCount - 1) * DataGridView1.ColumnCount

    For i = 0 To DataGridView1.RowCount - 2
        For j = 0 To DataGridView1.ColumnCount - 1
            For k As Integer = 1 To DataGridView1.Columns.Count
                xlWorkSheet.Cells(1, k) = DataGridView1.Columns(k - 1).HeaderText
                xlWorkSheet.Cells(i + 2, j + 1) = DataGridView1(j, i).Value.ToString()

                Increment= Int(currentIndex / intMax)
                ProgressBar1.Increment(Increment)
                currentIndex += 1
            Next
        Next
    Next

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM