使用powershell将文本列添加到csv的开头

问题描述 投票:0回答:2

我正在使用下面的脚本将电子表格转换为csv格式。我需要做的是在开头添加一个标有“订单类型代码”的列,其下的每个条目都会简单地说“SO”

#CBRCust,StoreNum,StoreName,Load,Stop,Item,Quantity,Pack,Size,ItemDescription,UPC

$headers = "CBRCust","StoreNum","StoreName","Load","Stop","Item","Quantity","Pack","Size","ItemDescription","UPC"
$source = Import-Csv Sample.csv -Header $headers

#setup Output Table
$tabName = "Output"

#Create Table object
$table = New-Object system.Data.DataTable “$tabName”

#Define Columns
$col1 = New-Object system.Data.DataColumn ColumnName1,([string])
$col2 = New-Object system.Data.DataColumn ColumnName2,([string])

#Add the Columns
$table.columns.add($col1)
$table.columns.add($col2)

#Index the source and create a new table.foreach ($row in $source)
{
    $newrow = $table.NewRow()
    $newrow.ColumnName1 = $row."CBRCust"
    $newrow.ColumnName2 = $row."StoreName"
   $table.Rows.Add($newrow)
}

    #output Table
$table | format-table -AutoSize 
#save file
$table | Export-Csv Output.csv -NoTypeInformation

我尝试使用下面的脚本,但它没有用

#Define Columns
$col1 = New-Object system.Data.DataColumn OrderTypeCode,([string])
$col2 = New-Object system.Data.DataColumn ColumnTest1,([string])
$col3 = New-Object system.Data.DataColumn ColumnName2,([string])


#Add the Columns
$table.columns.add($col1)
$table.columns.add($col2)

#Index the source and create a new table.
foreach ($row in $source)
{
    $newrow = $table.NewRow()
    $newrow.OrderTypeCode = 'SO' 
    $newrow.ColumnTest1 = $row."CBRCust"
    $newrow.ColumnName2 = $row."StoreName"
    $table.Rows.Add($newrow)
}
powershell csv
2个回答
1
投票

好吧,如果你不需要DataTable,看看它是否能满足你的需求:

$headers = "CBRCust","StoreNum","StoreName","Load","Stop","Item","Quantity","Pack","Size","ItemDescription","UPC"
$source = Import-Csv Sample.csv -Header $headers
$source | Select CBRCust, StoreName, @{n='OrderTypeCode'; e={'SO'}} | Export-CSV Output.csv -NoTypeInformation

0
投票

所以我想出了怎么做。只需将列定义为文本字段而不是数据列

#CBRCust,StoreNum,StoreName,Load,Stop,Item,Quantity,Pack,Size,ItemDescription,UPC

$headers = "CBRCust","StoreNum","StoreName","Load","Stop","Item","Quantity","Pack","Size","ItemDescription","UPC"
$source = Import-Csv Sample.csv -Header $headers

#setup Output Table
$tabName = "Output"

#Create Table object
$table = New-Object system.Data.DataTable “$tabName”

#Define Columns
$col1 = 'Order Type Code'
$col2 = New-Object system.Data.DataColumn OrderNo,([string])
$col3 = New-Object system.Data.DataColumn OrderNo2,([string])
$col4 = New-Object system.Data.DataColumn OrderNo3,([string])
$col5 = New-Object system.Data.DataColumn OrderNo4,([string])
$col6 = New-Object system.Data.DataColumn OrderNo5,([string])
$col7 = New-Object system.Data.DataColumn VendorCode,([string])
$col8 = New-Object system.Data.DataColumn AccountCode,([string])
$col9 = New-Object system.Data.DataColumn FromWarehouse,([string])
$col10 = New-Object system.Data.DataColumn ToWarehouse,([string])
$col11 = New-Object system.Data.DataColumn AddressOverride,([string])
$col12 = New-Object system.Data.DataColumn AddressLine1,([string])
$col13 = New-Object system.Data.DataColumn AddressLine2,([string])
$col14 = New-Object system.Data.DataColumn City,([string])
$col15 = New-Object system.Data.DataColumn State,([string])
$col16 = New-Object system.Data.DataColumn Zip,([string])
$col17 = New-Object system.Data.DataColumn Country,([string])
$col18 = New-Object system.Data.DataColumn ContactName,([string])
$col19 = New-Object system.Data.DataColumn ContactPhone,([string])
$col20 = New-Object system.Data.DataColumn ContactExt,([string])
$col21 = New-Object system.Data.DataColumn ContactFax,([string])
$col22 = New-Object system.Data.DataColumn OwnerCode,([string])
$col23 = New-Object system.Data.DataColumn CarrierCode,([string])
$col24 = New-Object system.Data.DataColumn LeavesDate,([string])
$col25 = New-Object system.Data.DataColumn ArrivesDate,([string])
$col26 = New-Object system.Data.DataColumn DeliveryInstructions,([string])
$col27 = New-Object system.Data.DataColumn Stop,([string])


#Add the Columns
$table.columns.add($col1)
$table.columns.add($col2)
$table.columns.add($col3)
$table.columns.add($col4)
$table.columns.add($col5)
$table.columns.add($col6)
$table.columns.add($col7)
$table.columns.add($col8)
$table.columns.add($col9)
$table.columns.add($col10)
$table.columns.add($col11)
$table.columns.add($col12)
$table.columns.add($col13)
$table.columns.add($col14)
$table.columns.add($col15)
$table.columns.add($col16)
$table.columns.add($col17)
$table.columns.add($col18)
$table.columns.add($col19)
$table.columns.add($col20)
$table.columns.add($col21)
$table.columns.add($col22)
$table.columns.add($col23)
$table.columns.add($col24)
$table.columns.add($col25)
$table.columns.add($col26)
$table.columns.add($col27)

#Index the source and create a new table.
foreach ($row in $source)
{
    $newrow = $table.NewRow()
    $newrow.'Order Type Code' = 'SO'
    $newrow.OrderNo = $row.'SO'
    $newrow.OrderNo = $row."CBRCust"
    $newrow.OrderNo2 = $row."StoreName"
    $newrow.OrderNo3 = $row.""
    $newrow.OrderNo4 = $row."" 
    $newrow.OrderNo5  = $row.""
    $newrow.VendorCode  = $row.""
    $newrow.AccountCode = $row.""
    $newrow.FromWarehouse = $row.""
    $newrow.ToWarehouse = $row.""
    $newrow.AddressOverride  = $row.""
    $newrow.AddressLine1  = $row.""
    $newrow.AddressLine2   = $row.""
    $newrow.City = $row.""     
    $newrow.State  = $row.""
    $newrow.Zip = $row.""
    $newrow.Country  = $row.""
    $newrow.ContactName  = $row.""
    $newrow.ContactPhone = $row.""
    $newrow.ContactExt  = $row.""
    $newrow.ContactFax  = $row.""
    $newrow.OwnerCode  = $row.""
    $newrow.CarrierCode  = $row.""
    $newrow.LeavesDate = $row.""
    $newrow.ArrivesDate  = $row.""
    $newrow.DeliveryInstructions  = $row.""
    $newrow.Stop  = $row.""
    $table.Rows.Add($newrow)
}

#output Table
$table | format-table -AutoSize 
#save file
$table | Export-Csv Output.csv -NoTypeInformation

#$table | ConvertTo-Csv -NoTypeInformation | foreach {$_.Replace('"','')} | Out-File 1output.csv
© www.soinside.com 2019 - 2024. All rights reserved.