Excel(.xls文件)-4张工作表不可能?
再次需要你的帮助 这个脚本不起作用。它适用于前3张,但不适用于最后一张。如果我切换itemnumber(例如3->4和4->3),新的3工作,而新的4不工作。这是某种错误吗?还是我缺少了一些增加“最大工作表编号”的命令 错误代码:Excel(.xls文件)-4张工作表不可能?,excel,powershell,Excel,Powershell,再次需要你的帮助 这个脚本不起作用。它适用于前3张,但不适用于最后一张。如果我切换itemnumber(例如3->4和4->3),新的3工作,而新的4不工作。这是某种错误吗?还是我缺少了一些增加“最大工作表编号”的命令 错误代码: "Invalid Index. (Exception by HRESULT: 0x8002000B (DISP_E_BADINDEX))" 请提前向Thx寻求帮助 额外资料: 使用powershell 3.0 使用excel 2010我认为这是因为您引用的是不同的工
"Invalid Index. (Exception by HRESULT: 0x8002000B (DISP_E_BADINDEX))"
请提前向Thx寻求帮助
额外资料:
使用powershell 3.0
使用excel 2010我认为这是因为您引用的是不同的工作簿 这条线
$wb = $Excel.Workbooks.Add()
表示您正在使用新工作簿
尝试添加
$wb.Worksheets.Add()
创建工作簿后,查看是否有效
尝试在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脚本:)它是基于零的吗?所以0-3不是1-4不,0也有同样的问题。“无效索引。(HRESULT异常:0x8002000B(DISP_E_BADINDEX))”+$ws4=$wb.Worksheets.Item(0)从未与Powershell一起使用过,但这是我的两分钱。。。根据Excel设置,默认情况下,新工作簿只有3个工作表。如果您想使用它,您需要添加和额外的工作表。不确定在Powershell中是否有方法计算当前工作表的数量。一旦你得到了计数,你就可以添加新的工作表。@Siddharth我有同样的结论,但我以前也从未使用过powershell。+1我也觉得这就是原因。:)正如我在上面的评论中提到的,“不确定在Powershell中是否有方法计算当前工作表的数量。一旦获得了数量,就可以基于此添加新工作表。”@Sam Ward。。。这并不能解决问题。不能对空值表达式调用方法。+$wb.Sheets.Add()+~~~~~~~~~~~~~~~~~~~~~~~~~~类别信息:无效操作:(:)[],RuntimeException+FullyQualifiedErrorId:InvokeMethodFull$wb.Sheets.count在创建工作簿后获取我0,在重命名第三个工作表后获取我0。按如下方式尝试:
$wb=$Excel.Workbooks.Add()
$wb.Sheets.Add()
我刚刚添加了一个完整的示例,说明我认为可以解决您的问题。抱歉,我不熟悉Powershell语法。检查工作表计数的正确语法实际上是$Workbook.Worksheets.count,我相信。
$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()
#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()