如何将数据从datagridview导出到excel [英] How to export data from datagridview to excel
问题描述
你好
请帮助
如何将数据从datagridview导出到excel
当我点击button1然后打开excel但空白数据导出datagridview
当我点击button2然后出现错误显示
请帮助
http://www.mediafire.com/file/vcm9sg3o8qzpp5t/Capture.PNG
http://www.mediafire。 com / file / 6r5xgztxzqhbz1z / Button + 2 + error.png
我尝试过:
<私有子Button1_Click(ByVal sender As System.Object,ByVal e As System.EventArgs)处理Button1.Click
Dim app_xls As Object
Dim lig_cpt, col_cpt As Integer
app_xls = CreateObject(excel.application)
app_xls.workbooks.add()
app_xls.visible = True
'kkkkkkk
尝试
对于col_cpt = 0到DataGridView1.ColumnCount - 4
app_xls.activesheet.cell( 1,col_cpt,+ 1)。value = DataGridView1.Columns(col_cpt).HeaderText
Next
For lig_cpt = 0 To DataGridView1.Rows.Count - 1
for col_cpt = 0 To DataGridView1。 ColumnCount - 4
如果是IsNumeric(DataGridView1.Item(col_cpt,lig_cpt).Value)那么
app_xls.activesheet.cells(lig_cpt + 2,col_cpt + 1).value = CDbl(DataGridView1 .Item(col_cpt,lig_cpt).Value)
Else
app_xls.activesheet.cells(lig_cpt + 2,col_cpt + 1).value = DataGridView1.Item(col_cpt,lig_cpt).Value
结束如果
下一个
下一个
Catch ex As Exception
结束尝试
结束子
========================================= =============
Private Sub Button2_Click(ByVal sender As System.Object,ByVal e As System .EventArgs)处理B. utton2.Click
Dim ExcelApp As Object,ExcelBook As Object
Dim ExcelSheet As Object
Dim i As Integer
Dim j As Integer
'create object excel
ExcelApp = CreateObject(Excel.Application)
ExcelBook = ExcelApp.WorkBooks.Add
ExcelSheet = ExcelBook.WorkSheets(1)
使用ExcelSheet
For i = 1 To Me.DataGridView1.RowCount
.cells(i,1)= Me.DataGridView1.Rows(i - 1).Cells(id)。value
for j = 1 To DataGridView1.Columns.Count - 1
.cells(i,j + 1)= DataGridView1.Rows(i - 1).Cells(j).Value
Next
Next
结束时
ExcelApp.Visible = True
'
ExcelSheet = Nothing
ExcelBook = Nothing
ExcelApp = Nothing
End Sub
到设置任何单元格的值使用此语法:
app_xls.activesheet.Range(Range).Value = Value
其中范围
包含一个字符串,其中包含所需单元格的坐标 - 例如:
app_xls.activesheet .Range( A1)。值=值
将值放入第一行A列中的单元格。
查看此代码行:app_xls.activesheet.cell ( 1 ,col_cpt , +1).value = DataGridView1.Columns(col_cpt).HeaderText
在col_cpt后面你输入了一个额外的逗号,这里完全错了 - 我想你不想把它放在那里。
如果删除它,看看你的代码在做什么。 ..
hello
please help
How to export data from datagridview to excel
when i click button1 then open excel but blank data export from datagridview
when i click button2 then a error show
please help
http://www.mediafire.com/file/vcm9sg3o8qzpp5t/Capture.PNG
http://www.mediafire.com/file/6r5xgztxzqhbz1z/Button+2+error.png
What I have tried:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim app_xls As Object Dim lig_cpt, col_cpt As Integer app_xls = CreateObject("excel.application") app_xls.workbooks.add() app_xls.visible = True 'kkkkkkk Try For col_cpt = 0 To DataGridView1.ColumnCount - 4 app_xls.activesheet.cell(1, col_cpt, +1).value = DataGridView1.Columns(col_cpt).HeaderText Next For lig_cpt = 0 To DataGridView1.Rows.Count - 1 For col_cpt = 0 To DataGridView1.ColumnCount - 4 If IsNumeric(DataGridView1.Item(col_cpt, lig_cpt).Value) Then app_xls.activesheet.cells(lig_cpt + 2, col_cpt + 1).value = CDbl(DataGridView1.Item(col_cpt, lig_cpt).Value) Else app_xls.activesheet.cells(lig_cpt + 2, col_cpt + 1).value = DataGridView1.Item(col_cpt, lig_cpt).Value End If Next Next Catch ex As Exception End Try End Sub
======================================================
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Dim ExcelApp As Object, ExcelBook As Object Dim ExcelSheet As Object Dim i As Integer Dim j As Integer 'create object of excel ExcelApp = CreateObject("Excel.Application") ExcelBook = ExcelApp.WorkBooks.Add ExcelSheet = ExcelBook.WorkSheets(1) With ExcelSheet For i = 1 To Me.DataGridView1.RowCount .cells(i, 1) = Me.DataGridView1.Rows(i - 1).Cells("id").value For j = 1 To DataGridView1.Columns.Count - 1 .cells(i, j + 1) = DataGridView1.Rows(i - 1).Cells(j).Value Next Next End With ExcelApp.Visible = True ' ExcelSheet = Nothing ExcelBook = Nothing ExcelApp = Nothing End Sub
To set the value of any cell use this syntax:
app_xls.activesheet.Range(Range).Value = Value
whereRange
contains a string with the coordinates of the desired cell - e.g.:
app_xls.activesheet.Range("A1").Value = Value
to put a value to the cell in column A of the first row.
Take a look to this codeline :app_xls.activesheet.cell(1, col_cpt, +1).value = DataGridView1.Columns(col_cpt).HeaderText
Behind col_cpt you have entered an additional comma which is complete wrong here - I suppose you didn't wanted to place it there.
See what you code is doing if you remove it ...
这篇关于如何将数据从datagridview导出到excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!