DEV Community

stnt
stnt

Posted on • Edited on

PowerShell misc.

get all excel processes stop when it's done



& {  # Create a temporary child scope.

  $excel = new-object -ComObject excel.application # create excel object
  $workbook = $excel.Workbooks.Add() # add a workbook
  $ws1 = $workbook.Worksheets.Item(1) # reference the 1st sheet

  # You must *always* call .Quit(), otherwise the Excel process lingers
  # for the entire OS users session.
  $excel.Quit()

} # On exiting this block, $excel, $workbook, and $ws1
  # go out of scope and release the COM objects when the
  # garbage collector runs next.

# Run the garbage collector now.
# The Excel process should terminate shortly after.



ーーーー



#CSV を読み紺で Excel で出力する。
#既にファイルがある場合は強制的に上書きされる

#参考:XlFileFormat 列挙型
#https://msdn.microsoft.com/ja-jp/library/microsoft.office.interop.excel.xlfileformat(v=office.11).aspx


$excel = New-Object -ComObject Excel.Application
$book = $excel.Workbooks.Open("c:\temp\test.csv")

#警告なしに上書き
$excel.DisplayAlerts = $false



#Excel 2003形式の xls で保存する場合

$book.SaveAs("c:\temp\test.xls", [Microsoft.Office.Interop.Excel.XlFileFormat]::xlWorkbookNormal)



#Excel 2007以後の形式の xlsx で保存する場合

$book.SaveAs("c:\temp\test.xlsx", [Microsoft.Office.Interop.Excel.XlFileFormat]::xlExcel5)



#HTML 形式で保存する場合

$book.SaveAs("c:\temp\test.html", [Microsoft.Office.Interop.Excel.XlFileFormat]::xlHtml)




$excel.Quit()



#プロセスを終了する
Enter fullscreen mode Exit fullscreen mode

Top comments (0)