简体   繁体   English

使用PowerShell将带有行定界符的CSV导入Excel

[英]Import CSV with row delimiters into Excel using PowerShell

Need a help with a PowerShell script to import a CSV file with the column delimiters as | 需要PowerShell脚本帮助,以列分隔符为|导入CSV文件| and the row delimiters as & (not carriage return) into Excel. 并将行定界符作为& (不是回车符)插入Excel。

Here is the sample of CSV file: 这是CSV文件的示例:

row1col1|row1col2|row1col3|row1col4&row2col1|row2col2|row2col3|row2col4&row3col1|row3col2|row3col3|row3col4 etc.

I found this script, but in only works when row delimiter is CR (carriage return). 我找到了此脚本,但仅在行定界符为CR(回车)时有效。 What lines should I add to make it work with & symbol, not CR. 我应该添加哪些行才能使其与&符号(而不是CR)一起使用。

#Define locations and delimiter
$csv = "file.csv"   #Location of the source file
$xlsx = "file.xlsx" #Desired location of output

$delimiter = "|" #Specify the delimiter used in the file

# Create a new Excel workbook with one empty sheet
$excel = New-Object -ComObject Excel.Application
$workbook = $excel.Workbooks.Add(1)
$worksheet = $workbook.Worksheets.Item(1)

# Build the QueryTables.Add command and reformat the data
$TxtConnector = ("TEXT;" + $csv)

$Connector = $worksheet.QueryTables.Add($TxtConnector, $worksheet.Range("A1"))

$query = $worksheet.QueryTables.Item($Connector.Name)
$query.TextFileOtherDelimiter = $delimiter
$query.TextFileParseType = 1
$query.TextFileColumnDataTypes = ,1 * $worksheet.Cells.Columns.Count
$query.AdjustColumnWidth = 1

# Execute & delete the import query
$query.Refresh()
$query.Delete()

# Save & close the Workbook as XLSX.
$Workbook.SaveAs($xlsx, 51)
$excel.Quit()

Most of he time I use the module ImportExcel ( https://www.powershellgallery.com/packages/ImportExcel ) to import or export data to/from Excel. 大多数时候,我使用模块ImportExcel( https://www.powershellgallery.com/packages/ImportExcel )将数据导入Excel或从Excel导出数据。 It is much faster then the com interface. 它比com界面快得多。 That makes my solution a 2-liner 这使我的解决方案成为2线

(Get-Content .\sample.csv) -replace '&',"`r`n" |Set-Content .\sample.csv
Import-Csv .\sample.csv -Delimiter '|' | Export-Excel -Path .\export2.xlsx -WorksheetName SHEET1

Again: you need a module. 同样:您需要一个模块。 It works even when Excel is not installed! 即使未安装Excel,它也可以工作!

You can't use a text connector with everything on one line. 您不能将文本连接器的所有内容都放在一行上。 You'll have to add some code at the beginning of your script to get it to work. 您必须在脚本的开头添加一些代码才能使其正常工作。

$folder = 'c:\path\to\file'
$file = 'test.csv'
$delimiter = '|'
$lineseparator = '&'

$csvfile = Join-Path $folder $file

# step by step
$contents = Get-Content $csvfile -Raw
$replacedcontents = $contents -replace $lineseparator, "`r`n"
$replacedcontents | Out-File $csvfile -Force

# or use this oneliner
(Get-Content $csvfile -Raw) -replace $lineseparator, "`r`n" | Out-File $csvfile -Force

I've used `r`n (the Windows standard, char 13 & 10) but it will probably work with just `n as well. 我用`r`n (Windows标准,焦炭13 10),但它可能会与刚工作`n为好。

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

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