我有問題在Excel表上做一個數據透視表,我不知道我做錯了什麼。powershell腳本創建excel數據透視表
這裏是我用
# 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
以下是錯誤消息中的powershell代碼我接收 異常調用「CreatePivotTable」與「1」的參數(一個或多個):「的參數是不正確的(從HRESULT異常:0x8007 0057(E_INVALIDARG))「 無法在此對象上找到'方向'屬性;確保它存在並可設置。 在C:\ ASM \ scripts \ Powershell \ TEST \ test_excel4.ps1:80 char:14 + $ PivotFields。 < < < <取向= $ xlRowField + CategoryInfo:InvalidOperation:(取向:字符串)[],的RuntimeException + FullyQualifiedErrorId:PropertyNotFound
住宅 '位置' 不能被此物體上找到;確保它存在並可設置。 在C:\ ASM \ scripts \ Powershell \ TEST \ test_excel4.ps1:81 char:14 + $ PivotFields。 < < < <位置= 1 + CategoryInfo:InvalidOperation:(位置:字符串)[],RuntimeException的 + FullyQualifiedErrorId:PropertyNotFound
感謝您對先進的幫助。
正確的Excel語法應該是'WB.PivotCaches.Add(...)。CreatePivotTable()'但是如果你看看你的'$ PivotTable'變量,你接收錯誤的那一行是閱讀$ workbook.PivotCaches()。創建(...)。CreatePivotTable()'這將是不正確的語法(我認爲) 這是一個有關在Excel中使用此方法可能會幫助 http:/ /msdn.microsoft.com/en-us/library/office/ff839885(v=office.15).aspx –