[英]convert csv file to excel using powershell
I'm trying to convert a csv file to excel using powershell.我正在尝试使用 powershell 将 csv 文件转换为 excel。 I got some help from another post and the script runs, but the output is a blank xlsx file.
我从另一篇文章中得到了一些帮助,脚本运行了,但输出是一个空白的 xlsx 文件。
Can someone help please?有人可以帮忙吗?
Here is the powershell script这是powershell脚本
param (
[Parameter(Mandatory=$true)][string]$inputfile,
[Parameter(Mandatory=$true)][string]$outputfile
)
$excel = New-Object -ComObject Excel.Application
$excel.Visible = $false
$wb = $excel.Workbooks.Add()
$ws = $wb.Sheets.Item(1)
$ws.Cells.NumberFormat = "@"
write-output "Opening $inputfile"
$i = 1
Import-Csv $inputfile | Foreach-Object {
$j = 1
foreach ($prop in $_.PSObject.Properties)
{
if ($i -eq 1) {
$ws.Cells.Item($i, $j) = $prop.Name
} else {
$ws.Cells.Item($i, $j) = $prop.Value
}
$j++
}
$i++
}
$wb.SaveAs($outputfile,51)
$wb.Close()
$excel.Quit()
write-output "Success"
Then I'm running the following command然后我运行以下命令
.\\csv_to_excel.ps1 -inputfile "C:\\Scripts\\testcsv.csv" -outputfile "C:\\Scripts\\data.xlsx" .\\csv_to_excel.ps1 -inputfile "C:\\Scripts\\testcsv.csv" -outputfile "C:\\Scripts\\data.xlsx"
Also, if anyone has experience with the Import Excel powershell module and has some kind of guide for that, I would appreciate that as well.另外,如果有人有使用导入 Excel powershell 模块的经验并有相关指南,我也将不胜感激。
Thanks谢谢
To use the Excel Module by Doug Finke from a computer with PowerShell 5 or a lower version with PSGet installed:在装有 PowerShell 5 或更低版本且安装了PSGet的计算机上使用 Doug Finke 的 Excel 模块:
Install-Module importexcel
Import-CSV $inputfile | Export-Excel $outputfile
Where $inputfile
and $outputfile
are the file locations like your script $inputfile
和$outputfile
是像你的脚本一样的文件位置
#As an example of an EASY way to format to excel merely use "Export-Excel" #作为一个简单的格式化方法的例子,只需使用“Export-Excel”
'Get-Service -ComputerName localhost |Select-Object -Property Status,Name,DisplayName|Export-Excel -Path '.\\output.xlsx' 'Get-Service -ComputerName localhost |Select-Object -Property Status,Name,DisplayName|Export-Excel -Path '.\\output.xlsx'
#Voila! #瞧!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.