我在Stack Overflow的另一篇文章中找到了一个非常好的Powershell代码。在下面;它将.csv数据加载到.xlsx中。它正是我想要的。在CMD中可以这样做吗?我一直在寻找等效的CMD,但是我一直在寻找Powershell版本。
### Set input and output path
$inputCSV = "C:\Users\username\Desktop\LSGTransactions.CSV"
$outputXLSX = "C:\Users\username\Desktop\Book1.XLSX"
### 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
### QueryTables does the same as when clicking "Data » From Text" in Excel
$TxtConnector = ("TEXT;" + $inputCSV)
$Connector =
$worksheet.QueryTables.add($TxtConnector,$worksheet.Range("A1"))
$query = $worksheet.QueryTables.item($Connector.name)
### Set the delimiter (, or ;) according to your regional settings
$query.TextFileOtherDelimiter = $Excel.Application.International(5)
### Set the format to delimited and text for every column
### A trick to create an array of 2s is used with the preceding comma
$query.TextFileParseType = 1
$query.TextFileColumnDataTypes = ,2 * $worksheet.Cells.Columns.Count
$query.AdjustColumnWidth = 1
### Execute & delete the import query
$query.Refresh()
$query.Delete()
### Save & close the Workbook as XLSX. Change the output extension for Excel
2003
$Workbook.SaveAs($outputXLSX,51)
$excel.Quit()
答案 0 :(得分:1)
通常,CMD中不提供COM对象(PowerShell中带有Excel)
但是,一种解决方法是从批处理文件中调用Powershell脚本,然后将文件名作为参数传递。
powershell.exe yourscript.ps1
您可能还需要在系统上设置执行策略,以启用运行的PS脚本(https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.security/set-executionpolicy?view=powershell-6)
支持COM的另一种语言可以是vbscript,您可以从中运行
cscript.exe script.vbs