直接将Excel另存为CSV,速度很快:
$FilePath_Public_ip_maps_infos="D:\My Documents\Work\IP映射表.xlsx" $Excel = New-Object -Com Excel.Application $Excel.visible = $False $Excel.displayalerts=$False $WorkBook = $Excel.Workbooks.Open($FilePath_Public_ip_maps_infos) #保存为CSV格式,需要设置参数值为6 $Workbook.SaveAs("D:\My Documents\Work\Public_ip_maps_infos22.csv",6) $Excel.quit()
逐行读取Excel,然后将其保存为CSV,效率很低:
#读取负载均衡信息表 $strpath="D:\My Documents\Work\IP映射表.xlsx" $excel=new-object -comobject excel.application $WorkBook = $excel.Workbooks.Open($strpath) $WorkSheet = $WorkBook.Worksheets.Item(1) #已用行数 $usedrows = $worksheet.usedrange.rows.count $Public_ip_maps_info = $Public_ip_maps_infos = $null $Public_ip_maps_infos = @() For($i=2;$i -le $usedrows;$i++) { $Public_ip_maps_info = ""|Select public_ip,internal_ip,is_vip $Public_ip_maps_info.public_ip = $WorkSheet.Cells.Item($i,2).value2 $Public_ip_maps_info.internal_ip = $WorkSheet.Cells.Item($i,3).value2 $Public_ip_maps_info.is_vip = $WorkSheet.Cells.Item($i,4).value2 $Public_ip_maps_infos += $Public_ip_maps_info } $Public_ip_maps_infos $excel.Quit() $WorkSheet =$null $WorkBook = $null $excel = $null [GC]::Collect()
将CSV另存为Excel,参考:
http://www.pstips.net/converting-csv-to-excel-file.html
时间: 2024-10-23 08:49:52