winform 数据库资料导出Excel方法(适用于资料数据较多加载慢,不用呈现至Data

时间:2021-10-11 03:04:25

 Private Sub savefile(ByVal dgv2 As DataTable)

        Dim app As Object = CreateObject("Excel.Application")
        Dim xlbook As Object = app.workbooks.add()

        Dim xR As Integer = dgv2.Rows.Count
        Dim xC As Integer = dgv2.Columns.Count
        Dim maxR As Integer = 20000
        Dim PageMax As Integer = Int(xR / maxR)
        If CInt(xR / maxR   0.5) > PageMax Then
            PageMax  = 1
        End If
        Dim i As Integer = 0
        Dim col As Integer = 0
        Dim row As Integer = 0
        For j As Integer = 1 To PageMax
            Dim xlsheet As Object = xlbook.worksheets.add()
            xlsheet.name = "工作表" & j
            xlsheet.activate()
            If j = PageMax Then
                maxR = xR - maxR * (PageMax - 1)
            End If
            Dim rawData1(maxR, xC - 1) As Object
            For col = 0 To xC - 1
                rawData1(0, col) = dgv2.Columns(col).ColumnName
            Next
            For row = 0 To maxR - 1
                For col = 0 To xC - 1
                    rawData1(row   1, col) = dgv2.Rows(i).ItemArray(col).ToString
                Next
                i  = 1
            Next
            xlsheet.range(xlsheet.cells(1, 1), xlsheet.cells(maxR   1, xC)).value2 = rawData1
            xlsheet.Columns.AutoFit()
        Next


        ‘ ****************
        ‘ * 砞﹚砰把计 *
        ‘ ****************
        With app.cells
            ‘.font.Name = "vendana"
            ‘.font.Size = 10
            ‘.HorizontalAlignment = -4108       ‘ キ﹡い
            ‘.VerticalAlignment = -4108         ‘ ﹡い
            .Columns.AutoFit()
        End With
        app.visible = True
        app.Quit()
        DGV1.Columns.Clear()
        Me.StatusBar1.Panels(0).Text = ""
    End Sub

  DataTable表格导出Excle

如果含有日期格式则需要转换下,oracle 下使用to_char()进行转换成toString格式