我的最终目标是使用PowerShell将某些CSV文件中的数据转换/导出到具有多个工作表的Excel工作簿中。我有大约90%的工作,但我似乎无法删除CSV文件内容中的一些逗号。
我尝试了一些正则表达式,但它们没有用。
到目前为止,我的运气是它要么不删除任何内容,要么删除CSV中的每个逗号,然后通过将所有内容放入单个列中来中断导出到Excel,或者删除单元格中包含逗号的所有内容。
以下是我尝试使用的CSV的部分内容示例。
Software Name Vendor
Software A Vendor A
Software B Vendor B
Software C Vendor, C
Software D Vendor D
Software E Vendor, E
以下是我正在整理的脚本中的一段代码。
$Excel = new-object -comobject Excel.Application
$Excel.SheetsInNewWorkbook = $GetCSV.Count
$AddWorkBook = $Excel.Workbooks.Add()
$NewWorkSheet=1
foreach ($CSV in $GetCSV) {
(Get-Content $CSV | Select-Object -Skip 1) | Set-Content $CSV
$Row=1
$Column=1
$WorkSheet = $AddWorkBook.WorkSheets.Item($NewWorkSheet)
$Name = $CSV.Name -replace ('.CSV','')
$WorkSheet.Name = $Name
$GetFile = (Get-Content $CSV)
foreach($Line in $GetFile) {
$LineContens=$Line -split ‘,(?!\s*\w+”)’
foreach($Cell in $LineContens) {
$WorkSheet.Cells.Item($Row,$Column) = $Cell
$Column++
}
$Column=1
$Row++
}
$NewWorkSheet++
}
$AddWorkBook.SaveAs($WorkBookName)
答案 0 :(得分:2)
,(?!(?<=",)")
替换为: 没有
此正则表达式将执行以下操作:
现场演示
https://regex101.com/r/uY6iG1/2
示例文字
"Software Name","Vendor"
"Software A","Vendor A"
"Software B","Vendor B"
"Software C","Vendor, C"
"Software D,","Vendor D"
"Soft,ware E","Vendor, E"
替换后
"Software Name","Vendor"
"Software A","Vendor A"
"Software B","Vendor B"
"Software C","Vendor C"
"Software D","Vendor D"
"Software E","Vendor E"
NODE EXPLANATION
----------------------------------------------------------------------
, ','
----------------------------------------------------------------------
(?! look ahead to see if there is not:
----------------------------------------------------------------------
(?<= look behind to see if there is:
----------------------------------------------------------------------
", '",'
----------------------------------------------------------------------
) end of look-behind
----------------------------------------------------------------------
" '"'
----------------------------------------------------------------------
) end of look-ahead
----------------------------------------------------------------------