码迷,mamicode.com
首页 > 其他好文 > 详细

将Excel另存为CSV格式文件

时间:2015-06-26 13:23:37      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:

直接将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

将Excel另存为CSV格式文件

标签:

原文地址:http://www.cnblogs.com/dreamer-fish/p/4601946.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!