标题:将数据从DataGridView导出到Excel,并设置是否要导出隐藏的列
只看楼主
wasdyjyj
Rank: 1
等 级:新手上路
帖 子:56
专家分:0
注 册:2007-5-8
 问题点数:0 回复次数:1 
将数据从DataGridView导出到Excel,并设置是否要导出隐藏的列
Public Function ExportExcel()Function ExportExcel(ByVal dgv As DataGridView, ByVal IsOnlyVisible As Boolean) As 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
2个参数分别是datagridview的ID与是否导出隐藏列
搜索更多相关主题的帖子: Excel DataGridView 数据 隐藏 设置 
2008-04-28 09:52
wasdyjyj
Rank: 1
等 级:新手上路
帖 子:56
专家分:0
注 册:2007-5-8
得分:0 
定义function那里粘贴的时候没弄好,拷贝的时候主义下,把多的Function ExportExcel()删掉
2008-04-28 09:54



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-211416-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.083116 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved