用于创建excel数据透视表的powershell脚本

时间:2015-01-08 22:05:58

标签: excel powershell pivot-table

我在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 

以下是我收到的错误消息 异常调用&#34; CreatePivotTable&#34;用&#34; 1&#34;参数:&#34;参数不正确。 (来自HRESULT的例外:0x8007 0057(E_INVALIDARG))&#34; 物业&#39;定位&#39;在这个对象上找不到;确保它存在且可设置。 在C:\ ASM \ scripts \ Powershell \ TEST \ test_excel4.ps1:80 char:14 + $ PivotFields。 &LT;&LT;&LT;&LT; Orientation = $ xlRowField     + CategoryInfo:InvalidOperation :( Orientation:String)[],RuntimeException     + FullyQualifiedErrorId:PropertyNotFound

财产&#39;职位&#39;在这个对象上找不到;确保它存在且可设置。 在C:\ ASM \ scripts \ Powershell \ TEST \ test_excel4.ps1:81 char:14 + $ PivotFields。 &LT;&LT;&LT;&LT;位置= 1     + CategoryInfo:InvalidOperation :( Position:String)[],RuntimeException     + FullyQualifiedErrorId:PropertyNotFound

感谢您提供的帮助。

2 个答案:

答案 0 :(得分:4)

创建数据透视表和字段时存在一些问题。

首先,在不选择整个电子表格的情况下,精确选择表中所需的行和列会更好。

一个很好的方法是从一个范围开始,然后让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

答案 1 :(得分:0)

尝试一下

$excel = New-Object -ComObject Excel.application
$excel.Visible = $True
$wb = $excel.Workbooks.Open('C:\Users\XYZ\Desktop\new.xlsx')
$ws2 = $wb.Sheets.Add()

#$ws2.Select()
$row=12
$pivCache = $wb.PivotCaches().Create(1,"Sheet1!R1C1:R"+$row+"C5",4)
$pivTable = $pivCache.CreatePivotTable("Sheet4!R1C1","PivotTable1",4)
$shape = $ws2.Shapes.AddChart()
#$shape.Select()
$chart = $shape.Chart
$chart.ChartType = 4
#$range = $ws2.Range("A1:C18")
#$chart.SetSourceData($range)
#$shape.IncrementLeft(19)
#$shape.IncrementTop(15)
$db2Field = $pivTable.AddDataField($pivTable.PivotFields("db2cpu"),"Sum of DB2 CPU",-4157)
$inField = $pivTable.PivotFields("intime")
$inField.Orientation = 1
$inField.Position = 1
$fidField = $pivTable.PivotFields("fid")
$fidField.Orientation = 2
$fidField.Position = 1