megnlingling 发表于 2018-9-2 11:52:18

powershell 操作Excel 附加释放资源

  $strPath="C:\powershell\test.xls"
  $objexcel=New-Object -ComObject excel.application
  $objexcel.Visible=$false
  $workbook=$objexcel.Workbooks.add()
  $sheet=$workbook.worksheets.item(1)
  $x=2
  $strComputer="."
  $objwmi=Get-WmiObject -Class win32_share
  $sheet.cells.item(1,1)=("Name of Share")
  $sheet.cells.item(1,2)=("Description of share")
  $sheet.cells.item(1,3)=("Type of share")
  foreach($objshare in $objwmi)
  {
  $sheet.cells.item($x,1)=$objshare.Name
  $sheet.cells.item($x,2)=$objshare.Description
  $sheet.cells.item($x,3)=$objshare.type
  $x++
  }
  $range=$sheet.usedrange
  $range.Entirecolumn.autofit()
  $objexcel.ActiveWorkbook.SaveAs($strPath)
  $objexcel.quit()
  $range=$null
  $sheet=$null
  $workbook=$null
  $objexcel=$null
  ::collect()

页: [1]
查看完整版本: powershell 操作Excel 附加释放资源