2015-01-08 131 views
1

我有問題在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

感謝您對先進的幫助。

+0

正確的Excel語法應該是'WB.PivotCaches.Add(...)。CreatePivotTable()'但是如果你看看你的'$ PivotTable'變量,你接收錯誤的那一行是閱讀$ workbook.PivotCaches()。創建(...)。CreatePivotTable()'這將是不正確的語法(我認爲) 這是一個有關在Excel中使用此方法可能會幫助 http:/ /msdn.microsoft.com/en-us/library/office/ff839885(v=office.15).aspx –

回答

2

創建數據透視表和字段有幾個問題。

首先,在沒有選擇整個電子表格的情況下,準確選擇表格中的哪些行和列即可。

一個很好的辦法做到這一點是開始一個範圍,然後讓Excel中,選擇的每一個細胞,直到它找到一個空的,就像這樣:

$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)) 

,並結合成單一選擇:

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

然後,當創建數據透視表時,重要的是給它一個名稱(即, "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 
+0

我收到以下錯誤異常設置「位置」:「無法設置PivotField類的位置屬性」行90.還有一種方法可以從數據透視表中刪除總數和計數嗎? – Joe

+0

使用Excel 2010設置Position'$ PivotFields.Position = 1'的行可以工作。不過,我在Excel 2013中看到一個錯誤。只需刪除這兩行,它應該可以工作。 –

+0

關於這些領域,你需要嘗試不同的可能性,看看有什麼適合的。有很多用於自定義數據透視表的功能。您可以通過使用'$ PivotFields.Orientation'屬性來執行此操作,並添加/刪除所需內容。 –

相關問題