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

Posted yunchen

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了winform 数据库资料导出Excel方法(适用于资料数据较多加载慢,不用呈现至DatagridView)相关的知识,希望对你有一定的参考价值。

 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)的主要内容,如果未能解决你的问题,请参考以下文章

如何用winform向excel中插入数据,就好像把excel当作数据库是一样的,我只会BS的 CS不会写 求教

winform中DataGridView的数据实现导出excel

Winform datagirdview导出Excel 封装方法 好用!!!

C#winform中如何把表导出到EXCEL

Winform 导入导出方法

c#如何在winform中打开一个excel文档呢