powershell 功能 - 数据 - UI:将数据网格导出到CSV文件

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了powershell 功能 - 数据 - UI:将数据网格导出到CSV文件相关的知识,希望对你有一定的参考价值。

function export-DGV2CSV ([Windows.Forms.DataGridView] $grid, [String] $File)
<#
  .SYNOPSIS
  Export basic datagrid to CSV file

  .PARAMETER grid
  Datagrid object
  .PARAMETER file
  Path to CSV file
#>
{
  if ($grid.RowCount -eq 0) { return } # nothing to do
  
  $row = New-Object Windows.Forms.DataGridViewRow
  $sw  = new-object System.IO.StreamWriter($File)
        
  #Write header line
  $sw.WriteLine( ($grid.Columns | % { $_.HeaderText } ) -join ',' )

  #Export contents
  $grid.Rows | % {
    $sw.WriteLine(
      ($_.Cells | % { $_.Value }) -join ','
      )
    }
  $sw.Close()
}

以上是关于powershell 功能 - 数据 - UI:将数据网格导出到CSV文件的主要内容,如果未能解决你的问题,请参考以下文章