zoukankan      html  css  js  c++  java
  • 将数据从DataGridView导出到Excel

    将数据从DataGridView导出到Excel,并设置是否要导出隐藏的列 
     Public Function ExportExcel(ByVal dgv As DataGridView, ByVal IsOnlyVisible As BooleanAs Boolean
        
    Try
          
    Dim n, i, j, row, col As Integer
          
    Dim excel As Excel.Application = New 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
          
    ' col = 1

          
    For i = 0 To dgv.RowCount - 1
            col 
    = 1
            
    For j = 0 To dgv.ColumnCount - 1
              
    'System.Windows.Forms.Application.DoEvents()
              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 Function
  • 相关阅读:
    1086. Tree Traversals Again (25)
    1094. The Largest Generation (25)
    1076. Forwards on Weibo (30)
    1083. List Grades (25)
    1082. Read Number in Chinese (25)
    【七夕特辑】程序员表白网页合集
    flex布局
    Nodejs进阶:基于express+multer的文件上传
    Git 和 SVN 之间的五个基本区别
    React通用后台管理系统
  • 原文地址:https://www.cnblogs.com/top5/p/1682624.html
Copyright © 2011-2022 走看看