PowerShell 操作 OFFICE


 

UiPath操作Office軟件的方式,這里說一下用PowerShell調用Office的COM組件的方式

老生常談~每個程序員都要至少掌握一門腳本編程語言。。。

EXCEL:

1 $excel = New-Object -ComObject Excel.Application;
2 $excel.DisplayAlerts = $False;
3 $wb=$excel.Workbooks.Open("oldexcelpath");
4 $wb.SaveAs("newexcelpath",51,"password");
5 $wb.Close();
6 $excel.Quit();
View Code

在ISE上運行正常,但移植到UiPath中就報錯了 類似Old Format or Invalid Type Library

將地域重新設置下就可以了。

1 $currentThread = [System.Threading.Thread]::CurrentThread;
2 $culture = [System.Globalization.CultureInfo]::GetCultureInfo("en-us");
3 $currentThread.CurrentCulture = $culture;
4 $currentThread.CurrentUICulture = $culture;
View Code

WORD:

1 $Word=NEW-Object –ComObject Word.Application;
2 $Document=$Word.documents.open("oldpath");
3 $Document.SaveAs([ref]"newpath",[ref]12,[ref][Type].Missing,[ref]"password");
4 $Document.Close();
5 $Word.Quit();
View Code

詳細參數列表,請參見MSDN

workbooks-open-method-excel

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM