当前位置:编程学习 > C#/ASP.NET >>

vb.net dataGridView导出EXCEL

按您的方法写一个不需要引用的导出例子,但总不成功,请您指点 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
答案:我有现成的,你直接调用哈,没有时间给你看代码
  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 

上一个:请高手帮忙看看vb.net的控件事件顺序问题
下一个:你好 vb.net 中datagridview内数据修改后保存代码怎么写 你是怎么修改的 我也遇到这样的问题 请帮忙谢谢

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,