我使用下面的power shell代码将csv转换为excel。但性能似乎很糟糕,对于一个200mb的csv文件,我已经等了20分钟。任何人都知道如何提高性能。或者我必须务实地做(使用java使用apache poi库)?
function convertToExcel {
param($fileName, $outputDir) # the two parameters.
#generate OutputFileName
$outputFilename = -join($fileName.BaseName, ".xlsx");
#prepare excel sheet
$excel = New-Object -ComObject Excel.Application
#$excel.visible = $true
$workbook = $excel.workbooks.add()
$sheets = $workbook.sheets
$sheetCount = $Sheets.count
$mySheet = 1
$mySheetName = "Sheet" + $mySheet
$s1 = $sheets | where{$_.name -eq $mySheetName}
$s1.Activate()
#Delete all other Sheets
If ($sheetCount -gt 1) {
$Sheets | ForEach {
$tmpSheetName = $_.Name
$tmpSheet = $_
If ($tmpSheetName -ne "Sheet1") {
$tmpSheet.Delete()
}
}
}
#copy paste data from csv
#$s1.Name = $fileName.BaseName
$s1.Name = "sheet1"
$s1.Activate()
$s1Data = Import-Csv $file.FullName -Delimiter '^'
$s1Data | ConvertTo-Csv -Delimiter "`t" -NoTypeInformation | Clip
$s1.cells.item(1,1).Select()
$s1.Paste()
$workbook.SaveAs("$outputDir\$outputFilename")
$excel.Quit()
}
#Read Input from Arguments
if ($args.length -ne 3){
}else{
$fileName = $Args[0] #read fileName
$inputFolder = $Args[1] #read inputFolder
$outputFolder = $Args[2] #read outputFolder
$files = dir -Path $inputFolder $fileName
ForEach ($file in $files) {
convertToExcel $file $outputFolder
}
}
答案 0 :(得分:0)
您是否尝试过在Excel中打开CSV文件?这可能是最快的方法。
$app = New-Object -ComObject Excel.Application
$app.Visible = $true
$app.DisplayAlerts = $False
$app.Workbooks.Open("yourcsvfile.csv")
另一种方法是按行读取文件并添加整行,但速度会慢一些。
但是,转换为PS对象并逐个单元格写入始终是最慢的。
修改:这是一个完整的功能......
function ConvertToExcel {
param($fileName)
$outputFilename = -join($fileName.split('.')[0], ".xls")
$excel = New-Object -ComObject Excel.Application
#$excel.Visible = $true
$excel.DisplayAlerts = $False
$wb = $excel.Workbooks.Open($fileName)
$wb.SaveAs("$outputFilename", [Microsoft.Office.Interop.Excel.XlFileFormat]::xlExcel8)
Start-Sleep -Milliseconds 200
$excel.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel)
Remove-Variable excel
}
ConvertToExcel "D:\tst\serverlist.csv"