Powershell Script to convert txt to xlsx

2019-07-27 14:36发布

问题:

I want to convert a textfile to Excel Format. I use the following script from Technet:

# Script name: ConvertTilde.ps1 
# Created on: 2007-01-06 
# Author: Kent Finkle 
# Purpose: How Can I Convert a Tilde-Delimited File to Microsoft Excel Format? 

$s = gc C:\Scripts\Test.txt 
$s = $s -replace("~","`t") 
$s | sc C:\Scripts\Test.txt 
$xl = new-object -comobject excel.application 
$xl.Visible = $true 
$wb = $xl.Workbooks.Open("C:\Scripts\Test.txt")
$wb.SaveAs(“D:\Excel\Test.xlsx”)

The Script works, excel opens and imports the txt, but the saved Files are just txt files renamed to xlsx - how can I get Excel to change the File Format?

回答1:

I would investigate the FileFormat parameter of the SaveAs method, although it won't magically convert a text file into a nicely formatted workbook.