Forums     Knowledge Base     OfficeWriter Online     
 
This documentation is for
OfficeWriter v3.0.3
COM/ASP Platform

View Docs for Another
Version or Platform

ExcelApplication in Depth > Creating a PivotTable

ExcelApplication is not
available in ExcelWriterSE.

Creating a PivotTable

A PivotTable is an interactive table that summarizes data to present it in a meaningful way. You can rotate a PivotTable's rows and columns to see different summaries of the source data, or drill down to show details. By displaying different views of data, PivotTable reports allow you to easily compare data, see patterns and relationships, and analyze trends.

In a PivotTable, each column or field in your source data becomes a PivotTable field that summarizes multiple rows of information. In the example below, the Month column becomes the Month field, and each record is summarized in a single item. Sum of Sales is a data field. A data field contains values to be summarized. By default, number values in a data field are summarized by the Sum function and text values are summarized by the Count function. (To change the summarization type set SummarizeBy.) If a PivotTable contains more than one data field, a single field drop-down named Data appears in the report for access to all of the data fields.

Source Data   PivotTable
 

This section explains how to create a PivotTable in script. Alternatively, create a PivotTable in Excel, and use ExcelApp.Open or ExcelTemplate for ASP.NET, or to open and modify the workbook. ExcelApp.Open and ExcelTemplate preserve existing PivotTables.

Example

The following example opens the worksheet PivotData.xls - which contains source data - and creates a PivotTable based on the source data. Pivotdata.xls contains a named range called DataNamedRange. The source data is in DataNamedRange.

<%@ Language="VBScript" %>
<% Option Explicit %>
<!--METADATA TYPE="TypeLib" UUID="{7BCD2133-64A0-4770-843C-090637114583}" -->
<%
'--- Errors will be handled manually in the code
'--- For troubleshooting, comment out this line 
'--- to see the default ASP error output
On Error Resume Next

'--- Declarations
Dim XlwApp' As SAExcelApplication
Dim wsPivot' As SAWorksheet
Dim wsData' As SAWorksheet
Dim rngData' As SARange
Dim fldProduct' As IPivotField
Dim fldSales' As IPivotField
Dim fldMonth' As IPivotField
Dim pvtTable' As IPivotTable


'--- Create an instance of SoftArtisans ExcelWriter
Set XlwApp = Server.CreateObject("SoftArtisans.ExcelWriter")
CheckError Err, "SoftArtisans ExcelWriter could not be created."

'--- Open the workbook with the data we will make into a pivot table
XlwApp.Open("C:\reports\pivotdata.xls")
CheckError Err, "The ""pivotdata.xls"" data workbook could not be opened."

'--- Get a reference to the data sheet
Set wsData = XlwApp.Worksheets("DataSheet")

'--- Create a second worksheet and name it PivotSheet
Set wsPivot = XlwApp.Worksheets(2)
wsPivot.Name = "PivotSheet"

'--- The data in the workbook is in a named range called DataNamedRange
'--- Get a SARange object representing that range
Set rngData = XlwApp.NamedRange("DataNamedRange")
CheckError Err, "Could not find ""DataNamedRange"" named range."

'--- Every SAWorksheet has a PivotTables collection
'--- Pass a SARange object containing the data for
'--- the pivot table
'--- The range must be a contiguous region of data with
'--- no skipped columns.  The first row of the range must contain
'--- column headers.  All columns must begin on the same row.
Set pvtTable = wsPivot.PivotTables.CreatePivotTable(rngData, 2, 2)
CheckError Err, "Failed to create pivot table."

'--- Properties of IPivotTable
'--- Most properties of IPivotTable are listed here
'--- for demonstration purposes, though they don't all need
'--- to be set explicitly in your code
With pvtTable
  '--- True by default
  .AutoFormat = True
  .DataCollectionInRow = True
  .EnableDrillDown = True
  .EnableFieldDialog = True
  .EnableWizard = True
  .GrandTotalsForColumns = True
  .GrandTotalsForRows = True
  .PreserveFormatting = True
  .RepeatItemLabels = False
  .ShowValueForEmptyCells = True
    
  '--- False by default
  .ShowFriendlyErrorValue = False
  .InOutlineForm = False
  .MergeLabels = False ' Center label horizontally and vertically
  .SetPrintTitles = False
  .SubtotalHiddenPageItems = False
    
  .AutoFormatType = saxlAutoFormatReport1
  .Name = "Sales Report 2003" ' Programmatic name for the report
        
End With

'--- The Fields collection is populated automatically
'--- based on the range of data passed to the CretePivotTable method
Set fldProduct = pvtTable.Fields("Product")
Set fldSales = pvtTable.Fields("Sales")
Set fldMonth = pvtTable.Fields("Month")

'--- Month will be displayed on the left column (Y-axis)
fldMonth.Type = saxlPivotFieldRow
'--- Product name will be displayed across the top (X-axis)
fldProduct.Type = saxlPivotFieldColumn

'--- Sales will be displayed in the body of the table
fldSales.Type = saxlPivotFieldData
'--- Sum up all sales numbers for the table values
fldSales.SummarizeBy = saxlSummarizationSum

'--- Make the pivot table worksheet the active sheet
'--- when the workbook opens
wsPivot.SelectSheet

'--- Stream the workbook to the client
'--- Use saOpenInPlace or saDefault
'--- Pivot tables will not open properly with saOpenInExcel
XlwApp.Save "BasicPivotReport.xls", saOpenInPlace
CheckError Err, "An error occurred saving the workbook."

'--- Dereference ExcelWriter
Set XlwApp = Nothing
Response.End

'--- This subroutine is called after every major operation
'--- to see if an error has occurred.
'--- If an error has occurred, display a message and stop the script
Sub CheckError(ByRef ThisErr, ByRef msg)
  If ThisErr.number <> 0 Then
    Response.Clear
    Response.Write "<b>" & msg &"</b><br>" & _
        ThisErr.Description & " (" & ThisErr.Source & ")"
    Response.End
  End If
End Sub
%>

The IPivotTable object represents a single PivotTable in a worksheet. To create an IPivotTable object, use the CreatePivotTable method. Pass CreatePivotTable three parameters:

aDataRange An SARange object containing the source data for the PivotTable. The range must be a contiguous region of data with no skipped columns. The first row of the range must contain column headers. All columns must begin at the same row.
aRow aRow and aColumn specify where to put the PivotTable (the table's upper left corner) in the specified worksheet.
aColumn

In addition to the IPivotTable object, ExcelWriter includes:

  • IPivotTables
    Represents the set of PivotTables in a worksheet. Use the property WorkSheet.PivotTables to access the IPivotTables collection.

  • IPivotFields
    Represents the collection of pivot fields in a PivotTable. A pivot field is a category of data that is derived from a field in the PivotTable's source data. Use the property IPivotTable.Fields to access the IPivotFields collection.

  • IPivotField
    Represents a single pivot field in a PivotTable. A pivot field is a category of data that is derived from a field in the PivotTable's source data. To create an IPivotField, use the property IPivotTable.Fields.

ExcelWriter provides numerous methods and properties that allow you to customize your PivotTable. For more information, see IPivotTable and IPivotField in the Programmer's Reference.



Copyright 2005 © SoftArtisans, Inc. All Rights Reserved.