简体   繁体   English

导入Excel,使用PowerShell导出CSV

[英]Import Excel, Export CSV with PowerShell

Due to restrictions I either need to use VB or PowerShell for this task. 由于限制,我需要使用VB或PowerShell执行此任务。

I have an Excel that looks like: 我有一个看起来像的Excel:

ColumA,              ColumB,ColumC,ColumD,ColumE,ColumF
000|Txt,MoreTxt ,    ColumB,ColumC,ColumD,ColumE,ColumF

I read about import_csv -header, but I'm under to successfully do it. 我读到有关import_csv -header的信息,但我正在努力做到这一点。 I'll post my script below. 我将在下面发布我的脚本。 The export I expect is: 我期望的出口是:

ColumA, ColumB, ColumC, ColumD, ColumE, ColumF
000,    ColumB, ColumC, ColumD, ColumE, ColumF

Only Colum gets modified, and I -only- need the digits from before that pipe. 只有Colum会被修改,而我(仅)需要该管道之前的数字。 It also has to stay three digits, so 1 becomes 001, etc. 它还必须保留三位数,因此1变为001,依此类推。

This is the script I modified based on some previous inquiries I saw, and the MS Tutorial. 这是我根据之前看到的一些查询和《 MS教程》修改的脚本。

   $file = import-csv "C:\path\to\my\file\test.csv"
    foreach ($row in $file){
        $tempfile = New-Object psobject -Property @{
            ColumA = $row. 'ListName'.substring(0,2)
            ColumB = $row. 'ColumB'
            ColumC = $row. 'ColumC'
            ColumE = $row. 'ColumE'
            ColumF = $row. 'ColumF'
        }
        $expandfile = @()
        $expandfile += $tempfile | select ColumA, ColumB, ColumC, ColumD,  ColumE, ColumF
    }

PS gives me both errors on not liking everything I have in quotes (Which I thought was the column name, but I guess not. And also a parse error on the entire array. Essentially the entire script. PS给了我两个错误,就是我不喜欢用引号引起来的所有错误(我以为是列名,但我想不是。而且还存在整个数组的解析错误。本质上是整个脚本。

UPDATE Providing real examples of source. 更新提供源的真实示例。

"Tiam
Name",SiamName,Siam,Ciam,Piam,Liam,Niam,Diam
"002|City, State","City, State - Some text (15092)",1,"3,408",99,"3,408",780,22.89%
"009|City, State","City, State - Some Text (E) (15450)",1,"1,894",81,"1,894",543,28.67%

Edit: 编辑:

$expandfile = Import-Csv "C:\path\to\my\file\test.csv" | ForEach-Object {
    $_."Tiam`r`nName" = $_."Tiam`r`nName".SubString(0,3)
    $_
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM