我在该表单上有一个表单和一个按钮。当用户点击该按钮时,MS-Excel表格从访问数据库生成并保存在特定位置。由于这一代excel需要一段时间才能完成,所以我想要做的是我想显示一个进度条加载,直到生成整个工作表。 显示进度条将帮助用户了解正在生成excel的过程。
我怎么能这样做呢?
这是我的代码:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Try
Dim saveFileDialog1 As New SaveFileDialog
saveFileDialog1.Filter = "Excel File|*.xlsx"
saveFileDialog1.Title = "Save an Excel File"
saveFileDialog1.ShowDialog()
If saveFileDialog1.FileName <> "" Then
saveExcelFile(saveFileDialog1.FileName)
End If
MessageBox.Show("Excel file Saved!")
Catch ex As Exception
End Try
End Sub
Public Sub saveExcelFile(ByVal FileName As String)
Try
Dim xls As Microsoft.Office.Interop.Excel.Application
xls = New Microsoft.Office.Interop.Excel.Application
Dim sheet As Excel.Worksheet
Dim i As Integer
xls.Workbooks.Add()
sheet = xls.ActiveWorkbook.ActiveSheet
Dim row As Integer = 1
Dim col As Integer = 1
For i = 0 To Me.ListView1.Columns.Count - 1
sheet.Cells(1, i + 1) = Me.ListView1.Columns(i).Text
Next
For i = 0 To Me.ListView1.Items.Count - 1
For j = 0 To Me.ListView1.Items(i).SubItems.Count - 1
sheet.Cells(i + 2, j + 1) = Me.ListView1.Items(i).SubItems(j).Text
Next
Next
row += 1
col = 1
' for the header
sheet.Rows(1).Font.Name = "DotumChe"
sheet.Rows(1).Font.size = 15
sheet.Rows(1).HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter
Dim mycol As System.Drawing.Color = System.Drawing.ColorTranslator.FromHtml("#20B2AA")
sheet.Rows(1).Font.color = mycol
' for all the sheet without header
sheet.Range("a2", "z1000").Font.Name = "Arial"
sheet.Range("a2", "z1000").Font.Size = 13
sheet.Range("a2", "z1000").HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter
sheet.Range("A1:X1").EntireColumn.AutoFit()
sheet.Range("A1:X1").EntireRow.AutoFit()
xls.ActiveWorkbook.SaveAs(FileName)
xls.Workbooks.Close()
xls.Quit()
Catch ex As Exception
End Try
End Sub
答案 0 :(得分:1)
我解决了它:) 这就是答案:
Dim x As Integer
x = 100 - CInt((i / Me.ListView1.Items.Count) * 100)
ProgressBar1.Increment(1)
ProgressBar1.Value = CInt((i / Me.ListView1.Items.Count) * 100) + x
Me.Refresh()
这是完整的代码:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim saveFileDialog1 As New SaveFileDialog
saveFileDialog1.Filter = "Excel File|*.xlsx"
saveFileDialog1.Title = "Save an Excel File"
If saveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
If saveFileDialog1.FileName <> "" Then
saveExcelFile(saveFileDialog1.FileName)
End If
End If
MessageBox.Show("Excel file Saved!")
End Sub
Public Sub saveExcelFile(ByVal FileName As String)
Try
Dim xls As New Excel.Application
Dim sheet As Excel.Worksheet
Dim i As Integer
xls.Workbooks.Add()
sheet = xls.ActiveWorkbook.ActiveSheet
Dim row As Integer = 1
Dim col As Integer = 1
For i = 0 To Me.ListView1.Columns.Count - 1
sheet.Cells(1, i + 1) = Me.ListView1.Columns(i).Text
Next
For i = 0 To Me.ListView1.Items.Count - 1
For j = 0 To Me.ListView1.Items(i).SubItems.Count - 1
sheet.Cells(i + 2, j + 1) = Me.ListView1.Items(i).SubItems(j).Text
Next
Dim x As Integer
x = 100 - CInt((i / Me.ListView1.Items.Count) * 100)
ProgressBar1.Increment(1)
ProgressBar1.Value = CInt((i / Me.ListView1.Items.Count) * 100) + x
Me.Refresh()
Next
row += 1
col = 1
' for the header
sheet.Rows(1).Font.Name = "Cooper Black"
sheet.Rows(1).Font.size = 12
sheet.Rows(1).HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter
Dim mycol As System.Drawing.Color = System.Drawing.ColorTranslator.FromHtml("#148cf7")
sheet.Rows(1).Font.color = mycol
' for all the sheet without header
sheet.Range("a2", "z1000").Font.Name = "Arial"
sheet.Range("a2", "z1000").Font.Size = 13
sheet.Range("a2", "z1000").HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter
sheet.Range("A1:X1").EntireColumn.AutoFit()
sheet.Range("A1:X1").EntireRow.AutoFit()
xls.ActiveWorkbook.SaveAs(FileName)
xls.Workbooks.Close()
xls.Quit()
Catch ex As Exception
End Try
End Sub
答案 1 :(得分:0)
在您的应用程序中创建表单。放上进度/温度计条。将最大值设置为Me.ListView1.Columns.Count * 2
,然后:
For i = 0 To Me.ListView1.Columns.Count - 1
sheet.Cells(1, i + 1) = Me.ListView1.Columns(i).Text
'increment progress bar counter by 1
'refresh form
Next
For i = 0 To Me.ListView1.Items.Count - 1
'increment progress bar counter by 1
'refresh form
For j = 0 To Me.ListView1.Items(i).SubItems.Count - 1
sheet.Cells(i + 2, j + 1) = Me.ListView1.Items(i).SubItems(j).Text
Next
Next
然后关闭你的表格。
答案 2 :(得分:0)
首先在表单中添加 ProgressBar控件并设置.Maximum = 100
在循环项目中添加第二行:
ProgressBar1.Maximum = 100
For i = 0 To Me.ListView1.Items.Count - 1
For j = 0 To Me.ListView1.Items(i).SubItems.Count - 1
sheet.Cells(i + 2, j + 1) = Me.ListView1.Items(i).SubItems(j).Text
Next
ProgressBar1.Value = CInt((i / Me.ListView1.Items.Count)* 100)
Next
或类似的东西,我没有尝试但我认为会起作用的。
祝你玩得开心!