powershell脚本创建Excel数据透视表

1 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 
Run Code Online (Sandbox Code Playgroud)

这是我收到的错误消息

使用“1”个参数调用“CreatePivotTable”时出现异常:“参数不正确。(来自 HRESULT 的异常:0x8007 0057 (E_INVALIDARG))”在此对象上找不到属性“方向”;确保它存在并且可设置。在 C:\ASM\scripts\Powershell\TEST\test_excel4.ps1:80 字符:14

  • $PivotFields。<<<< 方向 = $xlRowField
    • 类别信息:无效操作:(方向:字符串)[],运行时异常
    • FullQualifiedErrorId:PropertyNotFound

在此对象上找不到属性“位置”;确保它存在并且可设置。在 C:\ASM\scripts\Powershell\TEST\test_excel4.ps1:81 字符:14

  • $PivotFields。<<<< 位置 = 1
    • 类别信息:无效操作:(位置:字符串)[],运行时异常
    • FullQualifiedErrorId:PropertyNotFound

Mic*_*lli 5

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

首先,最好准确选择表中所需的行和列,而无需选择整个电子表格。

执行此操作的一个好方法是从一个范围开始,然后要求 Excel 选择每个单元格,直到找到一个空单元格,如下所示:

$range1=$ws3.range("A1")
$range1=$ws3.Range($range1,$range1.End($xlDirection::xlDown))
Run Code Online (Sandbox Code Playgroud)

$xlDirection注意as的定义

$xlDirection = [Microsoft.Office.Interop.Excel.XLDirection]
Run Code Online (Sandbox Code Playgroud)

对于第二列:

$range2=$ws3.range("B1")
$range2=$ws3.Range($range2,$range2.End($xlDirection::xlDown))
Run Code Online (Sandbox Code Playgroud)

并将它们组合成一个选择:

$selection = $ws3.Range($range1, $range2)
Run Code Online (Sandbox Code Playgroud)

然后,在创建数据透视表时,为其命名(即"Tables1")很重要。稍后我们将使用该名称来引用它:

$PivotTable.CreatePivotTable("R1C6","Tables1") | Out-Null 
Run Code Online (Sandbox Code Playgroud)

最后,我们要定义数据透视表中的哪个字段执行什么操作以及它的位置是什么。在我们的例子中,我们希望该列既是 $xlRowField 又是 $xlDataField,并且我们只需重写该值,如下所示:

$PivotFields = $ws3.PivotTables("Tables1").PivotFields("ColumnA")
$PivotFields.Position = 1
$PivotFields.Orientation = $xlRowField
$PivotFields.Orientation = $xlDataField
Run Code Online (Sandbox Code Playgroud)

这是完整的代码:

# 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
Run Code Online (Sandbox Code Playgroud)

编辑:

字段自定义的一个示例是:

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

$PivotFields = $ws3.PivotTables("Tables1").PivotFields("ColumnB")
$PivotFields.Orientation = $xlHidden
$PivotFields.Orientation = $xlDataField
Run Code Online (Sandbox Code Playgroud)

或者这个

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

$PivotFields = $ws3.PivotTables("Tables1").PivotFields("ColumnB")
$PivotFields.Orientation = $xlPageField
$PivotFields.Orientation = $xlDataField
Run Code Online (Sandbox Code Playgroud)