码迷,mamicode.com
首页 > 数据库 > 详细

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

时间:2019-11-02 17:41:42      阅读:124      评论:0      收藏:0      [点我收藏+]

标签:usb   idv   lsh   tag   excel   The   auto   stat   view   

 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格式

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

标签:usb   idv   lsh   tag   excel   The   auto   stat   view   

原文地址:https://www.cnblogs.com/yunchen/p/11782905.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!