zoukankan      html  css  js  c++  java
  • 从DataGridView中将数据导出Excel(vb.net)

     DataGridView中将数据导出Excel

    Public Sub ExportExcel(ByVal dgv As DataGridView, ByVal isonlyvisible As Boolean)

            Try

                Dim n, i, j, row, col As Integer

     

                Dim excel As New Microsoft.Office.Interop.Excel.Application

                excel.Application.Workbooks.Add(True)

                col = 1

                '记录列名

                For n = 0 To dgv.ColumnCount - 1

                    If isonlyvisible Then

                        If dgv.Columns(n).Visible Then

                            excel.Cells(1, col) = dgv.Columns(n).HeaderText

                            col = col + 1

                        End If

                    Else

                        excel.Cells(1, n + 1) = dgv.Columns(n).HeaderText

                    End If

                Next

                row = 2

                For i = 0 To dgv.RowCount - 1

                    col = 1

                    For j = 0 To dgv.ColumnCount - 1

                        If isonlyvisible Then

                            If dgv.Columns(j).Visible Then

                                excel.Cells(i + 2, col) = dgv.Rows(i).Cells(j).Value

                                col = col + 1

                            End If

                        Else

                            excel.Cells(i + 2, j + 1) = dgv.Rows(i).Cells(j).Value

                        End If

                    Next

                Next

                excel.Visible = True

            Catch ex As Exception

                Throw ex

     

            End Try

     

        End Sub

  • 相关阅读:
    PHP返回随机颜色
    SQL Server 系统表介绍:sys.dm_exec_requests
    ORA27300 ORA27301 ORA27302 ORA27157
    Linux的subversion安装配置
    批处理计算n天前\后的日期
    Linux下vsftp配置
    RedHat Linux 5企业版开启VNCSERVER远程桌面功能
    WAS 6.1命令行(静默)安装
    五板斧封杀Windows操作系统默认共享(图)
    Select Top在不同数据库中的使用用法:
  • 原文地址:https://www.cnblogs.com/CharmingDang/p/9664013.html
Copyright © 2011-2022 走看看