[英]Loop through folders and merge excel files - 1 Excel File, Multiple Worksheets
[英]Creating Powershell script to loop through excel file and create folders
我是Powerscript的新手,正在尋找編寫解決方案以遍歷excel文件並為文件中的每一行創建一個文件夾。
目前,我有以下內容:
$objExcel = new-object -comobject excel.application
$objExcel.Visible = $True
$ExcelFilesLocation = “D:\Users\”
$UserWorkBook = $objExcel.Workbooks.Open($ExcelFilesLocation + “ProjectCodes.xlsx”)
$UserWorksheet = $UserWorkBook.Worksheets.Item(1)
$intRow = 2
Do {
$Projectcode = $UserWorksheet.Cells.Item($intRow, 1).Value()
$pos = $userLogOnEmail.IndexOf(“@”)
$intRow++
} While ($UserWorksheet.Cells.Item($intRow,1).Value() -ne $null)
$objExcel.Quit()
$a = Release-Ref($UserWorksheet)
$a = Release-Ref($UserWorkBook)
$a = Release-Ref($objExcel)
想法是循環瀏覽每一行的項目代碼列。 然后創建一個以項目代碼命名的文件夾。
花了很多時間用PowerShell處理Excel COM對象后,我的建議是放棄! Office COM對象令人困惑,笨拙且緩慢。
用更好的東西替換您在PowerShell中訪問Excel所使用的技術。 例如,此模塊: https : //github.com/dfinke/ImportExcel 。 如果使用的是PS 5,則可以使用Install-Module ImportExcel -Scope CurrentUser
。
除了更易於使用和更快之外,它不需要安裝Excel,從而使最終腳本更易於移植。
誠然,您沒有使用此模塊獲得完整的Excel功能,但是由於您似乎只需要讀取單元格,就可以了。
或者,將Excel文件另存為CSV,然后改用Import-Csv
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.