vb.net将datagrid值表示为excel [英] vb.net expoerting datagrid values to excel
本文介绍了vb.net将datagrid值表示为excel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试将数据网格控件中的值导出为excel。问题出在调试之后,当我单击按钮时,应用程序就会卡住。我也无法关闭表格。我必须单击调试按钮。也没有创建excel文件。请提出解决方案。谢谢。
私有 Sub Button1_Click( ByVal sender 正如系统。对象, ByVal e As System.EventArgs)句柄 Button1.Click
Dim xlApp As Microsoft.Office.Interop.Excel.Application
Dim xlWorkBook 正如 Microsoft.Office.Interop.Excel.Workbook
Dim xlWorkSheet 作为 Microsoft .Office.Interop.Excel.Worksheet
Dim misValue As Object = System.Reflection.Missing.Value
Dim i As 整数
Dim j As 整数
xlApp = 新 Microsoft.Office.Interop.Excel .ApplicationClass
xlWorkBook = xlApp.Workbooks.Add(misValue)
xlWorkSheet = CType (xlWorkBook.Worksheets.Item( sheet1),Microsoft.Office.Interop.Excel.Worksheet)
对于 i = 0 DataGridView1.RowCount - 2
对于 j = 0 DataGridView1.ColumnCount - 1
对于 k 作为 整数 = 1 DataGridView1.Columns.Count
xlWorkSheet.Cells( 1 ,k)= DataGridView1 .Columns(k - 1 )。HeaderText
xlWorkSheet.Cells(i + 2 ,j + 1 )= DataGridView1(j,i).Value.ToString()
下一步
下一步
下一步
xlWorkSheet.SaveAs( D:\ _vbexcel.xlsx)
xlWorkBook .Clo se()
xlApp.Quit()
xlApp = Nothing
xlWorkBook = 没什么
xlWorkSheet = 没什么
MsgBox( 你可以找到文件D:\ _vbexcel.xlsx)
结束 Sub
解决方案
因为我们已经确定你真的只是想加快处理速度,请看这里: http://support.microsoft.com/kb / 306022 [ ^ ]
在该页面上查找使用自动化将数据数组传输到工作表上的某个范围 - 这将为您提供使用数组填充范围的示例。基本上,您将数据加载到一个多维数组中,然后在一行代码中将该数组加载到工作表中 - 工作得更快。
检查这些..
http://www.nullskull.com /faq/1504/export-datagridview-data-in-excel-using-vbnet.aspx [ ^ ]
http://stackoverflow.com/questions/680199/how-to-export-datagridview-to-excel-using-vb-净 [ ^ ]
导出datagridview t o excel vb.net [ ^ ]
i am trying to export values in datagrid control to excel .the problem is after debugging ,when i click the button,the application just gets stuck. and i cannot close the form either. i have to click the debug button. the excel file also is not created. please suggest solutions. thanks.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
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.ApplicationClass
xlWorkBook = xlApp.Workbooks.Add(misValue)
xlWorkSheet = CType(xlWorkBook.Worksheets.Item("sheet1"), Microsoft.Office.Interop.Excel.Worksheet)
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:\vbexcel.xlsx")
xlWorkBook.Close()
xlApp.Quit()
xlApp = Nothing
xlWorkBook = Nothing
xlWorkSheet = Nothing
MsgBox("You can find the file D:\vbexcel.xlsx")
End Sub
解决方案
Since we've established that you're really just looking to speed up your processing, look here: http://support.microsoft.com/kb/306022[^]
Look for "Use Automation to transfer an array of data to a range on a worksheet" on that page--that gives you an example of populating a range with an array. Basically, you load your data into a multi-dimensional array, and then in one line of code load that array to your sheet--works MUCH faster.
Check these..
http://www.nullskull.com/faq/1504/export-datagridview-data-in-excel-using-vbnet.aspx[^]
http://stackoverflow.com/questions/680199/how-to-export-datagridview-to-excel-using-vb-net[^]
Export datagridview to excel vb.net[^]
这篇关于vb.net将datagrid值表示为excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文