从DataGridView导出

时间:2019-06-17 10:46:44

标签: powershell datagridview

我需要在PowerShell GUI上创建一个按钮,以在按下时保存DataGridView的内容。这需要打开一个文件对话框以选择目标位置并为其指定文件名。

查看代码以了解我已经尝试过的内容

$btnSave_Click={
    $saveFile = New-Object System.Windows.Forms.SaveFileDialog
    $saveFile.Filter = "Comma Delimited (*.csv) | *.csv "
    $saveFile.FileName
    if ($saveFile.ShowDialog() -eq 'OK')
    {
        $results.SelectAll()
        $results.ClipboardCopyMode = 'EnableAlwaysIncludeHeaderText'
        $results.GetClipboardContent().GetData('Text') | Out-File 
    $saveFile.FileName
    }
}

收到的错误是

ERROR: You cannot call a method on a null-valued expression.
ERROR: char:4
ERROR: +             $results.SelectAll()
ERROR: +             ~~~~~~~~~~~~~~~~~~~~~~~~~
ERROR:     + CategoryInfo          : InvalidOperation: (:) [],     
RuntimeException
ERROR:     + FullyQualifiedErrorId : InvokeMethodOnNull
ERROR:
ERROR: The property 'ClipboardCopyMode' cannot be found on this object.  
Verify that the property exists and can be set.
ERROR: char:4
ERROR: +             $results.ClipboardCopyMode   
= 'EnableAlwaysIncludeHea ...
ERROR: +                
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ERROR:     + CategoryInfo          : InvalidOperation: (:) [],   
RuntimeException
ERROR:     + FullyQualifiedErrorId : PropertyNotFound
ERROR:
ERROR: You cannot call a method on a null-valued expression.
ERROR: char:4
ERROR: +             $results.GetClipboardContent().GetData('Text') | 
Out ...
ERROR: +             
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ERROR:     + CategoryInfo          : InvalidOperation: (:) [], 
RuntimeException
ERROR:     + FullyQualifiedErrorId : InvokeMethodOnNull
ERROR:

1 个答案:

答案 0 :(得分:0)

也许你是说

+-----------+------------+------------------------------------------------+
| long_name | short_name |                     types                      |
+-----------+------------+------------------------------------------------+
| Abu Dhabi | Abu Dhabi  | [ "administrative_area_level_1", "political" ] |
+-----------+------------+------------------------------------------------+