PowerShell脚本来创buildExcel数据透视表

我遇到的问题在Excel工作表上做了一个关键,我不知道我做错了什么。

这是我使用的PowerShell代码

# requires excell COM #Create excel COM object $excel = New-Object -ComObject excel.application #Make Visible $excel.Visible = $True #Add a workbook $workbook = $excel.Workbooks.Add() #Remove other worksheets 1..2 | ForEach { $Workbook.worksheets.item(2).Delete() } #Connect to first worksheet to rename and make active $serverInfoSheet = $workbook.Worksheets.Item(1) $serverInfoSheet.Name = 'DiskInformation' $serverInfoSheet.Activate() | Out-Null #Create a Title for the first worksheet and adjust the font $row = 1 $Column = 1 #Create a header for Disk Space Report; set each cell to Bold and add a background color $serverInfoSheet.Cells.Item($row,$column)= 'ColumnA' $serverInfoSheet.Cells.Item($row,$column).Interior.ColorIndex =48 $serverInfoSheet.Cells.Item($row,$column).Font.Bold=$True $Column++ $serverInfoSheet.Cells.Item($row,$column)= 'ColumnB' $serverInfoSheet.Cells.Item($row,$column).Interior.ColorIndex =48 $serverInfoSheet.Cells.Item($row,$column).Font.Bold=$True $Column++ #Now it is time to add the data into the worksheet! #Increment Row and reset Column back to first column $row++ $Column = 1 $serverInfoSheet.Cells.Item($row,$column)= "a" $Column++ $serverInfoSheet.Cells.Item($row,$column)= "b" $Column++ #Increment to next row and reset Column to 1 $Column = 1 $row++ # rename workbook $workbook = $workbook #$workbook = $excel.Workbooks.Add() # Get sheets $ws3 = $workbook.worksheets | where {$_.name -eq "DiskInformation"} #<------- Selects sheet 3 $xlPivotTableVersion12 = 3 $xlPivotTableVersion10 = 1 $xlCount = -4112 $xlDescending = 2 $xlDatabase = 1 $xlHidden = 0 $xlRowField = 1 $xlColumnField = 2 $xlPageField = 3 $xlDataField = 4 # R1C1 means Row 1 Column 1 or "A1" # R65536C5 means Row 65536 Column E or "E65536" $PivotTable = $workbook.PivotCaches().Create($xlDatabase,"Report!R1C1:R65536C5",$xlPivotTableVersion10) $PivotTable.CreatePivotTable("Pivot!R1C1") | Out-Null [void]$ws3.Select() $ws3.Cells.Item(3,1).Select() $workbook.ShowPivotTableFieldList = $true $PivotFields = $ws3.PivotTables('Tables1') #.PivotFields("Computername") # Worksheet Name is Server $PivotFields.Orientation = $xlRowField $PivotFields.Position = 1 

这里是我收到exception调用“1”参数的exception的错误消息:“该参数是不正确的(exception从HRESULT:0x8007 0057(E_INVALIDARG))”属性'方向'找不到此对象; 确保它存在并可设置。 在C:\ ASM \ scripts \ Powershell \ TEST \ test_excel4.ps1:80 char:14 + $ PivotFields。 <<<<方向= $ xlRowField + CategoryInfo:InvalidOperation :(方向:string)[],RuntimeException + FullyQualifiedErrorId:PropertyNotFound

属性“位置”不能在这个对象上find; 确保它存在并可设置。 在C:\ ASM \ scripts \ Powershell \ TEST \ test_excel4.ps1:81 char:14 + $ PivotFields。 <<<< Position = 1 + CategoryInfo:InvalidOperation:(Position:String)[],RuntimeException + FullyQualifiedErrorId:PropertyNotFound

感谢您的帮助。

数据透视表和字段的创build有几个问题。

首先,在表格中select所需的行和列,而不select整个电子表格更好。

一个很好的方法是从一个范围开始,然后让Excelselect每个单元格直到find一个空单元格,如下所示:

 $range1=$ws3.range("A1") $range1=$ws3.Range($range1,$range1.End($xlDirection::xlDown)) 

注意$xlDirection的定义为

 $xlDirection = [Microsoft.Office.Interop.Excel.XLDirection] 

和第二列:

 $range2=$ws3.range("B1") $range2=$ws3.Range($range2,$range2.End($xlDirection::xlDown)) 

并将它们组合成一个单一的select:

 $selection = $ws3.Range($range1, $range2) 

然后当创build一个数据透视表时,重要的是给它一个名字(即"Tables1" )。 稍后我们将使用该名称来引用它:

 $PivotTable.CreatePivotTable("R1C6","Tables1") | Out-Null 

最后,我们要定义数据透视表中的哪个字段做什么和它的位置。 在我们的例子中,我们希望列是$ xlRowField和$ xlDataField,我们只是覆盖如下的值:

 $PivotFields = $ws3.PivotTables("Tables1").PivotFields("ColumnA") $PivotFields.Position = 1 $PivotFields.Orientation = $xlRowField $PivotFields.Orientation = $xlDataField 

这是整个代码:

 # requires excell COM #Create excel COM object $excel = New-Object -ComObject excel.application #Make Visible $excel.Visible = $True #Add a workbook $workbook = $excel.Workbooks.Add() #Remove other worksheets 1..2 | ForEach { $Workbook.worksheets.item(2).Delete() } #Connect to first worksheet to rename and make active $serverInfoSheet = $workbook.Worksheets.Item(1) $serverInfoSheet.Name = 'DiskInformation' $serverInfoSheet.Activate() | Out-Null #Create a Title for the first worksheet and adjust the font $row = 1 $Column = 1 #Create a header for Disk Space Report; set each cell to Bold and add a background color $serverInfoSheet.Cells.Item($row,$column)= 'ColumnA' $serverInfoSheet.Cells.Item($row,$column).Interior.ColorIndex =48 $serverInfoSheet.Cells.Item($row,$column).Font.Bold=$True $Column++ $serverInfoSheet.Cells.Item($row,$column)= 'ColumnB' $serverInfoSheet.Cells.Item($row,$column).Interior.ColorIndex =48 $serverInfoSheet.Cells.Item($row,$column).Font.Bold=$True $Column++ #Now it is time to add the data into the worksheet! #Increment Row and reset Column back to first column $row++ $Column = 1 $serverInfoSheet.Cells.Item($row,$column)= "a" $Column++ $serverInfoSheet.Cells.Item($row,$column)= "b" $Column++ #Increment to next row and reset Column to 1 $Column = 1 $row++ # rename workbook $workbook = $workbook #$workbook = $excel.Workbooks.Add() # Get sheets $ws3 = $workbook.worksheets | where {$_.name -eq "DiskInformation"} #<------- Selects sheet 3 $xlPivotTableVersion12 = 3 $xlPivotTableVersion10 = 1 $xlCount = -4112 $xlDescending = 2 $xlDatabase = 1 $xlHidden = 0 $xlRowField = 1 $xlColumnField = 2 $xlPageField = 3 $xlDataField = 4 $xlDirection = [Microsoft.Office.Interop.Excel.XLDirection] # R1C1 means Row 1 Column 1 or "A1" # R65536C5 means Row 65536 Column E or "E65536" $range1=$ws3.range("A1") $range1=$ws3.Range($range1,$range1.End($xlDirection::xlDown)) $range2=$ws3.range("B1") $range2=$ws3.Range($range2,$range2.End($xlDirection::xlDown)) $selection = $ws3.Range($range1, $range2) $PivotTable = $workbook.PivotCaches().Create($xlDatabase,$selection,$xlPivotTableVersion10) $PivotTable.CreatePivotTable("R1C6","Tables1") | Out-Null [void]$ws3.Select() $ws3.Cells.Item(3,1).Select() $workbook.ShowPivotTableFieldList = $true $PivotFields = $ws3.PivotTables("Tables1").PivotFields("ColumnA") $PivotFields.Orientation = $xlRowField $PivotFields.Orientation = $xlDataField $PivotFields = $ws3.PivotTables("Tables1").PivotFields("ColumnB") $PivotFields.Orientation = $xlRowField $PivotFields.Orientation = $xlDataField 

编辑:

字段定制的一个例子是这样的:

 $PivotFields = $ws3.PivotTables("Tables1").PivotFields("ColumnA") $PivotFields.Orientation = $xlHidden $PivotFields.Orientation = $xlDataField $PivotFields = $ws3.PivotTables("Tables1").PivotFields("ColumnB") $PivotFields.Orientation = $xlHidden $PivotFields.Orientation = $xlDataField 

或这一个

 $PivotFields = $ws3.PivotTables("Tables1").PivotFields("ColumnA") $PivotFields.Orientation = $xlPageField $PivotFields.Orientation = $xlDataField $PivotFields = $ws3.PivotTables("Tables1").PivotFields("ColumnB") $PivotFields.Orientation = $xlPageField $PivotFields.Orientation = $xlDataField