Faster way of writing out to excel in powershell -


i have powershell script reads in csv , appends excel worksheet. runs quite painfully slow. have searched , seems limitation of using com write excel. suggestions have found speed write out entire ranges instead of cell cell. need format cells , doesn't seem possible when writing out ranges. suggestions on how optimize below code welcome. not have option use db.

$csvpath = "z:\script_test\" $outputfile = "z:\script_test\exceltest.xlsx"  foreach($csvfile in get-childitem $csvpath -filter "stats*.txt" ){ $csvfilepath = [io.path]::combine($csvpath, $csvfile) $rawcsvdata = import-csv -delimiter ";" -path $csvfilepath   $excel = new-object -comobject excel.application  $excel.visible = $false  $workbook = $excel.workbooks.open($outputfile) $excelworksheet = $excel.worksheets.item("2016") $excelworksheet.activate()  $excel.cells.item(1,1) = “pex”  $excel.cells.item(1,2) = “run date”  $excel.cells.item(1,3) = “execs”  $excel.cells.item(1,4) = “cpu av.”  $excel.cells.item(1,5) = “cpu hi.”  $excel.cells.item(1,6) = “cpu tot.”  $excel.cells.item(1,7) = “#value!”  $excel.cells.item(1,8) = “elaps av.”  $excel.cells.item(1,9) = “elapse hi.”  $excel.cells.item(1,10) = “elapse tot”   $i = $excelworksheet.usedrange.rows.count + 1  foreach($rawcsv in $rawcsvdata)   {   $rundate = $rawcsv.“run date     ”.replace("--1","")  $execs = $rawcsv."execs ".replace("?","")  $cpuav = $rawcsv.“cpu av.   ”.replace("-",":")  $cpuhi = $rawcsv.“cpu hi.   ”.replace("-",":")    $cputot = $rawcsv.“cpu tot.  ”.replace("-",":")   $elapseav = $rawcsv.“elapse av.”.replace("-",":")  $elapsehi = $rawcsv.“elapse hi.”.replace("-",":")   $elpasetot = $rawcsv.“elpase tot”.replace("-",":")  write-output("working" + $i) $excel.cells.item($i,1) = $rawcsv."pex "  $excel.cells.item($i,2) = $rundate     $excel.cells.item($i,2).numberformat = “yyyy/mm/dd” $excel.cells.item($i,3) = $execs  $excel.cells.item($i,4) = $cpuav   $excel.cells.item($i,4).numberformat = “hh:mm:ss.00” $excel.cells.item($i,5) = $cpuhi    $excel.cells.item($i,5).numberformat = “hh:mm:ss.00” $excel.cells.item($i,6) = $cputot  $excel.cells.item($i,6).numberformat = “hh:mm:ss.00” $excel.cells.item($i,7) = “=((hour(f"+$i+")*3600)+(minute(f"+$i+")*60)+second(f"+$i+"))*21”  $excel.cells.item($i,8) = $elapseav $excel.cells.item($i,8).numberformat = “hh:mm:ss.00” $excel.cells.item($i,9) = $elapsehi  $excel.cells.item($i,9).numberformat = “hh:mm:ss.00” $excel.cells.item($i,10) = $elpasetot $excel.cells.item($i,10).numberformat = “hh:mm:ss.00”  $i++ } $excelworksheet.usedrange.removeduplicates() #$workbook.saveas($outputfile)  $workbook.save() $excel.quit()  remove-variable -name excel  [gc]::collect()  [gc]::waitforpendingfinalizers()  move-item -path $csvfilepath -destination "z:\script_test\used files" } 

the slow part com object performance. won't able speed enough if keep working com object, sadly.

back in days had project related excel , found great module uses external dll, can take on it: psexcel

best part not need have excel installed, com object.


Comments

Popular posts from this blog

serialization - Convert Any type in scala to Array[Byte] and back -

matplotlib support failed in PyCharm on OSX -

python - Matplotlib: TypeError: 'AxesSubplot' object is not callable -