powershell 将复制的范围从excel转换为PSObjects数组

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了powershell 将复制的范围从excel转换为PSObjects数组相关的知识,希望对你有一定的参考价值。

function ConvertFrom-ExcelClipboard {
     <#
        .SYNOPSIS
            Convert copied range from excel to an array of PSObjects
        .DESCRIPTION
            A range of cells copied into the clipboard is converted into PSObject taking the first row (or provided property names via Header parameter) as the properties.
        .EXAMPLE
            #Considering a range of cells including header has been copied to the clipboard
            ConvertFrom-ExcelClipboard
        .EXAMPLE
            #Convert excel range without headers providing property names through argument to the Headers parameter
            ConvertFrom-ExcelClipboard -Header test1,test2,test3
    #>
    [CmdletBinding()]
    [Alias('pasteObject')]
    param(
        #Specifies an alternate column header row. The column header determines the names of the properties of the object(s) created.
        [string[]]$Header,
        #If specified, the content of the clipboard is returned as is.
        [switch]$Raw
    )

    Add-Type -AssemblyName System.Windows.Forms
    $tb = New-Object System.Windows.Forms.TextBox
    $tb.Multiline = $true
    $tb.Paste()
    if ($Raw){
        $tb.Text
    }
    else{
        $ht = $PSBoundParameters
        #compare Header Count to Column count of first row
        if ($Header -and $ht.Header.Count -ne  $tb.Text.Split(@("`r`n"),'None')[0].Split("`t").Count){
            Write-Warning 'Header values do not equal the number of columns copied to the clipboard'
        }
        $tb.Text | ConvertFrom-Csv -Delimiter "`t" @PSBoundParameters
    }
}
pasteObject

以上是关于powershell 将复制的范围从excel转换为PSObjects数组的主要内容,如果未能解决你的问题,请参考以下文章

使用PowerShell将整个工作表复制到另一个工作表

powershell 按级别格式将 xml 转换为 csv(或 excel)

从 Excel 导入 SQL Server:将 varchar 数据类型转换为 datetime 数据类型导致值超出范围

复制自动过滤范围,vba excel

如何使用python将excel文件中的时间戳列表从UTC转换为旧金山时间(太平洋时间)? [复制]

从 Google 表格复制特定范围并将值粘贴到 excel