Need a help with a PowerShell script to import a CSV file with the column delimiters as | and the row delimiters as & (not carriage return) into Excel.
Here is the sample of CSV file:
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). What lines should I add to make it work with & symbol, not 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()
Workbooks.Open()andWorkbooks.OpenText()methods also only allow for specifying column delimiters, not row delimiters. Replacing the row delimiter in your input file is probably your best option.