Datagrid 数据导出到excel 最新的速度最快的

         当然了,你必须首先确保装上了Excel,因为需要首先在 项目->添加引用中选择 com 标签后,添加 Microsoft   Excel  11.0 object library.

Public Function Datagrid2Excel(ByVal Dtg As DataGrid)
        If Dtg.VisibleRowCount > 0 Then
            Try

                Dim datav As New DataView
                If TypeOf Dtg.DataSource Is DataView Then
                    datav = CType(Dtg.DataSource, DataView)
                ElseIf TypeOf Dtg.DataSource Is DataSet Then
                    datav = CType(Dtg.DataSource, DataSet).Tables(0).DefaultView
                ElseIf TypeOf Dtg.DataSource Is DataTable Then
                    datav = CType(Dtg.DataSource, DataTable).DefaultView
                End If
                Dim i, j As Integer
                Dim rows As Integer = datav.Table.Rows.Count
                Dim cols As Integer = datav.Table.Columns.Count
                Dim DataArray(rows - 1, cols - 1) As String
                Dim myExcel As Excel.Application = New Excel.Application
                For i = 0 To rows - 1
                    For j = 0 To cols - 1
                        If IsDBNull(datav.Table.Rows(i).Item(j)) Then
                            DataArray(i, j) = ""
                        Else
                            DataArray(i, j) = datav.Table.Rows(i).Item(j)
                        End If
                    Next
                Next
                myExcel.Application.Workbooks.Add(True)
                myExcel.Visible = True
                For j = 0 To cols - 1
                    myExcel.Cells(1, j + 1) = datav.Table.Columns(j).ColumnName
                Next
                myExcel.Range("A2").Resize(rows, cols).Value = DataArray

            Catch exp As Exception
                MessageBox.Show(exp.Message)
            End Try
        Else
            MessageBox.Show("没有数据!")
        End If
    End Function

感受:网络是最好的老师,只要你自觉的去学习。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值