Excel(.xls文件) – 4张不可能?

再次需要你的帮助!

这个脚本不起作用。 它适用于前3张,但不适用于最后一张。 如果我切换itemnumber(例如3-> 4和4-> 3),新的3工作,新的4不工作。 这是一些错误? 还是我错过了一些命令行,以增加“最大纸张数”?

$Path = "C:\test.xls" #Excelvar: $Row = [int] 2 $Excel = New-Object -ComObject Excel.Application $Excel.Visible = $true $Excel.DisplayAlerts = $false #Sheets: $ADUsers = "Active Directory Users" $Groups = "Create Groups" $UsertoGroup = "User to groups" $DNS = "DNS" #$Worksheet = $Workbook.Sheets.Add() $checkxls = test-path -pathtype Any $Path if ($checkxls -eq $false) { $wb = $Excel.Workbooks.Add() $ws1 = $wb.Worksheets.Item(1) $ws1.Name = $ADUsers $ws1.activate() $ws2 = $wb.Worksheets.Item(2) $ws2.Name = $Groups $ws2.activate() $ws3 = $wb.Worksheets.Item(3) $ws3.Name = $UserToGroup $ws3.activate() $ws4 = $wb.Worksheets.Item(4) $ws4.Name = $DNS $ws4.activate() $wb.SaveAs($Path) $wb.Close() $Excel.Quit() 

错误代码:

 "Invalid Index. (Exception by HRESULT: 0x8002000B (DISP_E_BADINDEX))" 

提前帮助Thx。

额外的信息:使用Excel 2010使用PowerShell 3.0

我想这是因为你正在引用一个不同的工作簿

这条线

  $wb = $Excel.Workbooks.Add() 

意味着你正在使用一个新的工作簿。

尝试添加

  $wb.Worksheets.Add() 

工作簿创build后,看看是否有效。


 $Path = "C:\test.xls" #Excelvar: $Row = [int] 2 $Excel = New-Object -ComObject Excel.Application $Excel.Visible = $true $Excel.DisplayAlerts = $false #Sheets: $ADUsers = "Active Directory Users" $Groups = "Create Groups" $UsertoGroup = "User to groups" $DNS = "DNS" #$Worksheet = $Workbook.Sheets.Add() $checkxls = test-path -pathtype Any $Path if ($checkxls -eq $false) { $wb = $Excel.Workbooks.Add() $wb.Worksheets.add() $ws1 = $wb.Worksheets.Item(1) $ws1.Name = $ADUsers $ws1.activate() $ws2 = $wb.Worksheets.Item(2) $ws2.Name = $Groups $ws2.activate() $ws3 = $wb.Worksheets.Item(3) $ws3.Name = $UserToGroup $ws3.activate() $ws4 = $wb.Worksheets.Item(4) $ws4.Name = $DNS $ws4.activate() $wb.SaveAs($Path) $wb.Close() $Excel.Quit() 

试图在Excel中添加Sheet4,它是代码阅读Sheet4就好了

 #Declare the file path and sheet name $file = "C:\Documents\Folder\ExcelFile.xlsx" $sheetName = "Sheet1" #Create an instance of Excel.Application and Open Excel file $objExcel = New-Object -ComObject Excel.Application $workbook = $objExcel.Workbooks.Open($file) $sheetCount = $workbook.Worksheets.Count $sheet = $workbook.Worksheets.Item($sheetName) $sheet4 = $workbook.Worksheets.Item("Sheet4") Write-Host $sheetCount #sheet count is 4 $objExcel.Visible=$false #Count max row $rowMax = ($sheet.UsedRange.Rows).count #Declare the starting positions $rowName,$colName = 1,1 $rowAge,$colAge = 1,2 $rowCity,$colCity = 1,3 #loop to get values and store it for ($i=1; $i -le $rowMax-1; $i++) { $name = $sheet.Cells.Item($rowName+$i,$colName).text $age = $sheet.Cells.Item($rowAge+$i,$colAge).text $city = $sheet.Cells.Item($rowCity+$i,$colCity).text Write-Host ("My Name is: "+$name) Write-Host ("My Age is: "+$age) Write-Host ("I live in: "+$city) } #used $rowMax from Sheet1, you can declare a separate for Sheet4 for ($i=1; $i -le $rowMax-1; $i++) { $name = $sheet4.Cells.Item($rowName+$i,$colName).text $age = $sheet4.Cells.Item($rowAge+$i,$colAge).text $city = $sheet4.Cells.Item($rowCity+$i,$colCity).text Write-Host ("My Name is: "+$name) Write-Host ("My Age is: "+$age) Write-Host ("I live in: "+$city) } #close excel file $objExcel.quit() 

请原谅我的例子,只是一个noob脚本:)