vb.net 多线程 访问界面控件
网上查了,都说用委托invoke,就是不知道具体怎么做。麻烦大神了!要执行的函数是daochu()就是把datagridview1导出excelprivatesubdao...
网上查了,都说用委托 invoke,就是不知道具体怎么做。麻烦大神了!
要执行的函数是 daochu() 就是把 datagridview1导出excel
private sub daochu()
SaveFileDialog1.DefaultExt = "xls"
SaveFileDialog1.Filter = "Text documents {*.xls}|*.xls|Text documents {*.xlsx}|*.xlsx|All File {*.*}|*.*"
Dim yon As SaveOptions = SaveFileDialog1.ShowDialog()
If yon = 1 Then
ToolStripProgressBar1.Style = ProgressBarStyle.Blocks
(省略创建exce 对象)
With xlapp.ActiveSheet
.cells(1, 1).value = Label1.Text & " 汇率 " & TextBox1.Text
.range("A1:M1").MergeCells = True
.Range("A1").HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter
.Range("A1").VerticalAlignment = Excel.XlVAlign.xlVAlignCenter
' .range("A1:N1").Borders(XlBordersIndex.xlEdgeBottom).LineStyle = Excel.XlLineStyle.xlLineStyleNone
.range("A1:X1").Borders(Excel.XlBordersIndex.xlEdgeBottom).LineStyle = 1
.range("A2:X2").Borders(Excel.XlBordersIndex.xlEdgeBottom).LineStyle = 1
'.range("A20:N20").Borders(Excel.XlBordersIndex.xlEdgeBottom).LineStyle = 1
.cells(2, 1).value = "编号"
For i = 0 To Datagridview1.RowCount - 1
For j = 0 To Datagridview1.ColumnCount - 1
.cells(3 + i, j + 1).value = Datagridview1.Rows(i).Cells(j).Value
.cells(3 + i, j + 1).Borders(Excel.XlBordersIndex.xlEdgeTop).LineStyle = 1
.cells(3 + i, j + 1).Borders(Excel.XlBordersIndex.xlEdgeBottom).LineStyle = 1
.cells(3 + i, j + 1).Borders(Excel.XlBordersIndex.xlEdgeRight).LineStyle = 1
.cells(3 + i, j + 1).Borders(Excel.XlBordersIndex.xlEdgeLeft).LineStyle = 1
If CStr(.cells(3 + i, 2).value) = "合计" Then .range("A" & 3 + i & ":X" & 3 + i).Interior.Color = 10079487
p_value = (i * (Datagridview1.ColumnCount) + j + 1) / (Datagridview1.RowCount * Datagridview1.ColumnCount)
If p_value <= 100 Then
ToolStripProgressBar1.Value = p_value
End If
Application.DoEvents()
Next
Next
End With
省略保存excel代码
end sub
sub button1_click ()
Dim Thread2 As New System.Threading.Thread(AddressOf daochu)
Thread2.ApartmentState = ApartmentState.STA
Thread2.Start()
end sub 展开
要执行的函数是 daochu() 就是把 datagridview1导出excel
private sub daochu()
SaveFileDialog1.DefaultExt = "xls"
SaveFileDialog1.Filter = "Text documents {*.xls}|*.xls|Text documents {*.xlsx}|*.xlsx|All File {*.*}|*.*"
Dim yon As SaveOptions = SaveFileDialog1.ShowDialog()
If yon = 1 Then
ToolStripProgressBar1.Style = ProgressBarStyle.Blocks
(省略创建exce 对象)
With xlapp.ActiveSheet
.cells(1, 1).value = Label1.Text & " 汇率 " & TextBox1.Text
.range("A1:M1").MergeCells = True
.Range("A1").HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter
.Range("A1").VerticalAlignment = Excel.XlVAlign.xlVAlignCenter
' .range("A1:N1").Borders(XlBordersIndex.xlEdgeBottom).LineStyle = Excel.XlLineStyle.xlLineStyleNone
.range("A1:X1").Borders(Excel.XlBordersIndex.xlEdgeBottom).LineStyle = 1
.range("A2:X2").Borders(Excel.XlBordersIndex.xlEdgeBottom).LineStyle = 1
'.range("A20:N20").Borders(Excel.XlBordersIndex.xlEdgeBottom).LineStyle = 1
.cells(2, 1).value = "编号"
For i = 0 To Datagridview1.RowCount - 1
For j = 0 To Datagridview1.ColumnCount - 1
.cells(3 + i, j + 1).value = Datagridview1.Rows(i).Cells(j).Value
.cells(3 + i, j + 1).Borders(Excel.XlBordersIndex.xlEdgeTop).LineStyle = 1
.cells(3 + i, j + 1).Borders(Excel.XlBordersIndex.xlEdgeBottom).LineStyle = 1
.cells(3 + i, j + 1).Borders(Excel.XlBordersIndex.xlEdgeRight).LineStyle = 1
.cells(3 + i, j + 1).Borders(Excel.XlBordersIndex.xlEdgeLeft).LineStyle = 1
If CStr(.cells(3 + i, 2).value) = "合计" Then .range("A" & 3 + i & ":X" & 3 + i).Interior.Color = 10079487
p_value = (i * (Datagridview1.ColumnCount) + j + 1) / (Datagridview1.RowCount * Datagridview1.ColumnCount)
If p_value <= 100 Then
ToolStripProgressBar1.Value = p_value
End If
Application.DoEvents()
Next
Next
End With
省略保存excel代码
end sub
sub button1_click ()
Dim Thread2 As New System.Threading.Thread(AddressOf daochu)
Thread2.ApartmentState = ApartmentState.STA
Thread2.Start()
end sub 展开
2个回答
展开全部
建议你使用BackGroundWorker控件。将代码写入控件的事件中,使用它的Start方法就可以开始工作了。是与主程序同时进行的。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你的问题是你不明白你为什么要用线程
这个地方用线程,然后再用invoke简直是多此一举
这个地方用线程,然后再用invoke简直是多此一举
更多追问追答
追问
这是导出 excel,数据很多,开始导出后,好几分钟,软件不能动。所以我想用多线程,这样用户可以继续其他操作。
追答
不需要委托,我不知道到你的数据量有多大,不过我觉得做法是
显示savedialogue显示保存位置
datagridview的数据集生成一份拷贝
3. 遍历拷贝的数据集,生成excel
其中第三步封装为方法,放在线程中执行
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询