vb.net dataGridView导出EXCEL
按您的方法写一个不需要引用的导出例子,但总不成功,请您指点TryDimAPP_ExcelAsObjectDimExcelAsObjectAPP_Excel=CreateO...
按您的方法写一个不需要引用的导出例子,但总不成功,请您指点
Try
Dim APP_Excel As Object
Dim Excel As Object
APP_Excel = CreateObject("Excel.Application") '创建Excel应用程序
Excel = APP_Excel.workbooks.add(True) '生成新工作簿
Excel.visible = True
Dim n, i, j, row, col As Integer
' Dim excel As Excel.Application = New Excel.Application()
'excel.Application.Workbooks.Add(True)
' excel.Cells.NumberFormatLocal = "@ "
col = 1
'//记录列名
For n = 0 To DataGridView1.ColumnCount - 1
If DataGridView1.Columns(n).Visible Then
Excel.Cells(1, col) = DataGridView1.Columns(n).HeaderText
col = col + 1
End If
Next
'//记录内容
row = 2
' col = 1
For i = 0 To DataGridView1.RowCount - 1
col = 1
For j = 0 To DataGridView1.ColumnCount - 1
'
If DataGridView1.Columns(j).Visible Then
Excel.Cells(i + 2, col) = DataGridView1.Rows(i).Cells(j).Value
col = col + 1
End If
Next
Next
Excel.Visible = True
Catch ex As Exception
Throw ex
End Try 展开
Try
Dim APP_Excel As Object
Dim Excel As Object
APP_Excel = CreateObject("Excel.Application") '创建Excel应用程序
Excel = APP_Excel.workbooks.add(True) '生成新工作簿
Excel.visible = True
Dim n, i, j, row, col As Integer
' Dim excel As Excel.Application = New Excel.Application()
'excel.Application.Workbooks.Add(True)
' excel.Cells.NumberFormatLocal = "@ "
col = 1
'//记录列名
For n = 0 To DataGridView1.ColumnCount - 1
If DataGridView1.Columns(n).Visible Then
Excel.Cells(1, col) = DataGridView1.Columns(n).HeaderText
col = col + 1
End If
Next
'//记录内容
row = 2
' col = 1
For i = 0 To DataGridView1.RowCount - 1
col = 1
For j = 0 To DataGridView1.ColumnCount - 1
'
If DataGridView1.Columns(j).Visible Then
Excel.Cells(i + 2, col) = DataGridView1.Rows(i).Cells(j).Value
col = col + 1
End If
Next
Next
Excel.Visible = True
Catch ex As Exception
Throw ex
End Try 展开
2个回答
展开全部
我有现成的,你直接调用哈,没有时间给你看代码
Public Function daochu(ByVal x As DataGridView) As Boolean '导出到Excel函数
Try
If x.Rows.Count <= 0 Then '判断记录数,如果没有记录就退出
MessageBox.Show("没有记录可以导出", "没有可以导出的项目", MessageBoxButtons.OK, MessageBoxIcon.Information)
Return False
Else '如果有记录就导出到Excel
Dim xx As Object : Dim yy As Object
xx = CreateObject("Excel.Application") '创建Excel对象
yy = xx.workbooks.add()
Dim i As Integer, u As Integer = 0, v As Integer = 0 '定义循环变量,行列变量
For i = 1 To x.Columns.Count '把表头写入Excel
yy.worksheets(1).cells(1, i) = x.Columns(i - 1).HeaderCell.Value
Next
Dim str(x.Rows.Count - 1, x.Columns.Count - 1) '定义一个二维数组
For u = 1 To x.Rows.Count '行循环
For v = 1 To x.Columns.Count '列循环
If x.Item(v - 1, u - 1).Value.GetType.ToString <> "System.Guid" Then
str(u - 1, v - 1) = x.Item(v - 1, u - 1).Value
Else
str(u - 1, v - 1) = x.Item(v - 1, u - 1).Value.ToString
End If
Next
Next
yy.worksheets(1).range("A2").Resize(x.Rows.Count, x.Columns.Count).Value = str '把数组一起写入Excel
yy.worksheets(1).Cells.EntireColumn.AutoFit() '自动调整Excel列
' yy.worksheets(1).name = x.TopLeftHeaderCell.Value.ToString '表标题写入作为Excel工作表名称
xx.visible = True '设置Excel可见
yy = Nothing '销毁组建释放资源
xx = Nothing '销毁组建释放资源
End If
Return True
Catch ex As Exception '错误处理
MessageBox.Show(Err.Description.ToString, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error) '出错提示
Return False
End Try
End Function
Public Function daochu(ByVal x As DataGridView) As Boolean '导出到Excel函数
Try
If x.Rows.Count <= 0 Then '判断记录数,如果没有记录就退出
MessageBox.Show("没有记录可以导出", "没有可以导出的项目", MessageBoxButtons.OK, MessageBoxIcon.Information)
Return False
Else '如果有记录就导出到Excel
Dim xx As Object : Dim yy As Object
xx = CreateObject("Excel.Application") '创建Excel对象
yy = xx.workbooks.add()
Dim i As Integer, u As Integer = 0, v As Integer = 0 '定义循环变量,行列变量
For i = 1 To x.Columns.Count '把表头写入Excel
yy.worksheets(1).cells(1, i) = x.Columns(i - 1).HeaderCell.Value
Next
Dim str(x.Rows.Count - 1, x.Columns.Count - 1) '定义一个二维数组
For u = 1 To x.Rows.Count '行循环
For v = 1 To x.Columns.Count '列循环
If x.Item(v - 1, u - 1).Value.GetType.ToString <> "System.Guid" Then
str(u - 1, v - 1) = x.Item(v - 1, u - 1).Value
Else
str(u - 1, v - 1) = x.Item(v - 1, u - 1).Value.ToString
End If
Next
Next
yy.worksheets(1).range("A2").Resize(x.Rows.Count, x.Columns.Count).Value = str '把数组一起写入Excel
yy.worksheets(1).Cells.EntireColumn.AutoFit() '自动调整Excel列
' yy.worksheets(1).name = x.TopLeftHeaderCell.Value.ToString '表标题写入作为Excel工作表名称
xx.visible = True '设置Excel可见
yy = Nothing '销毁组建释放资源
xx = Nothing '销毁组建释放资源
End If
Return True
Catch ex As Exception '错误处理
MessageBox.Show(Err.Description.ToString, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error) '出错提示
Return False
End Try
End Function
来自:求助得到的回答
展开全部
你的参数没用对,row 没加1,修改后的代码如下(我已成功导出,注意datagridview1的参数。):
'//记录内容
row = 2
' col = 1
For i = 0 To DataGridView1.RowCount - 1
col = 1
For j = 0 To DataGridView1.ColumnCount - 1
If DataGridView1.Columns(j).Visible Then
'xlSheet.Cells(i + 2, col) = DataGridView1.Rows(i).Cells(j).Value
xlSheet.Cells(row, col) = DataGridView1(j, i).Value.ToString
col = col + 1
End If
Next
row = row + 1
Next
xlSheet.Visible = True
'//记录内容
row = 2
' col = 1
For i = 0 To DataGridView1.RowCount - 1
col = 1
For j = 0 To DataGridView1.ColumnCount - 1
If DataGridView1.Columns(j).Visible Then
'xlSheet.Cells(i + 2, col) = DataGridView1.Rows(i).Cells(j).Value
xlSheet.Cells(row, col) = DataGridView1(j, i).Value.ToString
col = col + 1
End If
Next
row = row + 1
Next
xlSheet.Visible = True
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询