Create and manage tables of Microsoft Excel files.

Create Table

One of the advantages of spreadsheets is that they allow you to create different types of lists, for example, phone lists, task lists, lists of transactions, assets or liabilities. Several users can work together to use, create and maintain various lists.

Aspose.Cells for Python via .NET supports creating and managing Lists.

Advantages of a List Object

There are quite a few advantages when you convert a list of data to an actual List Object

  • New rows and columns are automatically included.
  • A total row at the bottom of your list can be easily added to display SUM, AVERAGE, COUNT, etc.
  • Columns added to the right are automatically incorporated into the List object.
  • Charts based on rows and columns will be expanded automatically.
  • Named ranges assigned to rows and columns will be expanded automatically.
  • The list is protected from accidental row and column deletion.

Creating a List Object using Microsoft Excel

  • Selecting data range for creating a List object
  • This displays the Create List dialog.
  • Implement the List object for the data and specifying total row (Select Data, then List, followed by Total Row).

Using Aspose.Cells for Python via .NET API

Aspose.Cells for Python via .NET provides a class, Workbook, that represents a Microsoft Excel file. The Workbook class contains a  collection that allows access to each worksheet in an Excel file.

A worksheet is represented by the Worksheet class. The Worksheet class provides a wide range of properties and methods for managing a worksheet. To create a ListObject in a worksheet, use the list_objects collection property of the Worksheet class. Each ListObject is, in fact, an object of the add class, which further provides the add method for adding a List object and specifying a range of cells for the list.

According to the specified range of cells, the List object is created by Aspose.Cells for Python via .NET. Use attributes (for example, show_totals, ListColumns, etc.) of the ListObject class to control the list.

In the example given below, we have created the same ListObject using Aspose.Cells for Python via .NET API as we created using Microsoft Excel in the above section.

from aspose.cells import Workbook
from aspose.cells.tables import TotalsCalculation
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
dataDir = RunExamples.GetDataDir(".")
# Create a Workbook object.
# Open a template excel file.
workbook = Workbook(dataDir + "book1.xls")
# Get the List objects collection in the first worksheet.
listObjects = workbook.worksheets[0].list_objects
# Add a List based on the data source range with headers on.
listObjects.add(1, 1, 7, 5, True)
# Show the total row for the List.
listObjects[0].show_totals = True
# Calculate the total of the last (5th ) list column.
listObjects[0].list_columns[4].totals_calculation = TotalsCalculation.SUM
# Save the excel file.
workbook.save(dataDir + "output.xls")

Format a Table

To manage and analyze a group of related data, it is possible to turn a range of cells into a list object (also known as an Excel table). A table is a series of rows and columns that contain related data managed independently from the data in other rows and columns. By default, every column in the table has filtering enabled in the header row so that you can filter or sort your list object data quickly. You can add a total row (a special row in a list that provides a selection of aggregate functions useful for working with numerical data) to the list object that provides a drop-down list of aggregate functions for each total row cell. Aspose.Cells for Python via .NET provides options for creating and managing lists (or tables).

Formatting a List Object

Aspose.Cells for Python via .NET provides a class, Workbook, that represents a Microsoft Excel file. The Workbook class contains a worksheets collection that allows access to each worksheet in an Excel file.

A worksheet is represented by the Worksheet class. The Worksheet class provides a wide range of properties and methods for managing worksheets. To create a ListObject in a worksheet, use list_objects collection property of the Worksheet class. Each ListObject is, in fact, an object of the ListObjectCollection class, which further provides the add method to add a List object and specify the range of cells it should encompass. According to the specified range of cells, a ListObject is created in the worksheet by Aspose.Cells. Use attributes (for example, table_style_type) of the ListObject class to format the table for your requirement.

The example below adds sample data to a worksheet, adds a ListObject and apply default styles to it. ListObject styles are supported by Microsoft Excel 2007/2010.

from aspose.cells import Workbook
from aspose.cells.tables import TableStyleType, TotalsCalculation
from os import os, path
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
dataDir = RunExamples.GetDataDir(".")
# Create directory if it is not already present.
IsExists = path.isdir(dataDir)
if notIsExists:
os.makedirs(dataDir)
# Create a workbook.
workbook = Workbook()
# Obtaining the reference of the default(first) worksheet
sheet = workbook.worksheets[0]
# Obtaining Worksheet's cells collection
cells = sheet.cells
# Setting the value to the cells
cell = cells.get("A1")
cell.put_value("Employee")
cell = cells.get("B1")
cell.put_value("Quarter")
cell = cells.get("C1")
cell.put_value("Product")
cell = cells.get("D1")
cell.put_value("Continent")
cell = cells.get("E1")
cell.put_value("Country")
cell = cells.get("F1")
cell.put_value("Sale")
cell = cells.get("A2")
cell.put_value("David")
cell = cells.get("A3")
cell.put_value("David")
cell = cells.get("A4")
cell.put_value("David")
cell = cells.get("A5")
cell.put_value("David")
cell = cells.get("A6")
cell.put_value("James")
cell = cells.get("A7")
cell.put_value("James")
cell = cells.get("A8")
cell.put_value("James")
cell = cells.get("A9")
cell.put_value("James")
cell = cells.get("A10")
cell.put_value("James")
cell = cells.get("A11")
cell.put_value("Miya")
cell = cells.get("A12")
cell.put_value("Miya")
cell = cells.get("A13")
cell.put_value("Miya")
cell = cells.get("A14")
cell.put_value("Miya")
cell = cells.get("A15")
cell.put_value("Miya")
cell = cells.get("B2")
cell.put_value(1)
cell = cells.get("B3")
cell.put_value(2)
cell = cells.get("B4")
cell.put_value(3)
cell = cells.get("B5")
cell.put_value(4)
cell = cells.get("B6")
cell.put_value(1)
cell = cells.get("B7")
cell.put_value(2)
cell = cells.get("B8")
cell.put_value(3)
cell = cells.get("B9")
cell.put_value(4)
cell = cells.get("B10")
cell.put_value(4)
cell = cells.get("B11")
cell.put_value(1)
cell = cells.get("B12")
cell.put_value(1)
cell = cells.get("B13")
cell.put_value(2)
cell = cells.get("B14")
cell.put_value(2)
cell = cells.get("B15")
cell.put_value(2)
cell = cells.get("C2")
cell.put_value("Maxilaku")
cell = cells.get("C3")
cell.put_value("Maxilaku")
cell = cells.get("C4")
cell.put_value("Chai")
cell = cells.get("C5")
cell.put_value("Maxilaku")
cell = cells.get("C6")
cell.put_value("Chang")
cell = cells.get("C7")
cell.put_value("Chang")
cell = cells.get("C8")
cell.put_value("Chang")
cell = cells.get("C9")
cell.put_value("Chang")
cell = cells.get("C10")
cell.put_value("Chang")
cell = cells.get("C11")
cell.put_value("Geitost")
cell = cells.get("C12")
cell.put_value("Chai")
cell = cells.get("C13")
cell.put_value("Geitost")
cell = cells.get("C14")
cell.put_value("Geitost")
cell = cells.get("C15")
cell.put_value("Geitost")
cell = cells.get("D2")
cell.put_value("Asia")
cell = cells.get("D3")
cell.put_value("Asia")
cell = cells.get("D4")
cell.put_value("Asia")
cell = cells.get("D5")
cell.put_value("Asia")
cell = cells.get("D6")
cell.put_value("Europe")
cell = cells.get("D7")
cell.put_value("Europe")
cell = cells.get("D8")
cell.put_value("Europe")
cell = cells.get("D9")
cell.put_value("Europe")
cell = cells.get("D10")
cell.put_value("Europe")
cell = cells.get("D11")
cell.put_value("America")
cell = cells.get("D12")
cell.put_value("America")
cell = cells.get("D13")
cell.put_value("America")
cell = cells.get("D14")
cell.put_value("America")
cell = cells.get("D15")
cell.put_value("America")
cell = cells.get("E2")
cell.put_value("China")
cell = cells.get("E3")
cell.put_value("India")
cell = cells.get("E4")
cell.put_value("Korea")
cell = cells.get("E5")
cell.put_value("India")
cell = cells.get("E6")
cell.put_value("France")
cell = cells.get("E7")
cell.put_value("France")
cell = cells.get("E8")
cell.put_value("Germany")
cell = cells.get("E9")
cell.put_value("Italy")
cell = cells.get("E10")
cell.put_value("France")
cell = cells.get("E11")
cell.put_value("U.S.")
cell = cells.get("E12")
cell.put_value("U.S.")
cell = cells.get("E13")
cell.put_value("Brazil")
cell = cells.get("E14")
cell.put_value("U.S.")
cell = cells.get("E15")
cell.put_value("U.S.")
cell = cells.get("F2")
cell.put_value(2000)
cell = cells.get("F3")
cell.put_value(500)
cell = cells.get("F4")
cell.put_value(1200)
cell = cells.get("F5")
cell.put_value(1500)
cell = cells.get("F6")
cell.put_value(500)
cell = cells.get("F7")
cell.put_value(1500)
cell = cells.get("F8")
cell.put_value(800)
cell = cells.get("F9")
cell.put_value(900)
cell = cells.get("F10")
cell.put_value(500)
cell = cells.get("F11")
cell.put_value(1600)
cell = cells.get("F12")
cell.put_value(600)
cell = cells.get("F13")
cell.put_value(2000)
cell = cells.get("F14")
cell.put_value(500)
cell = cells.get("F15")
cell.put_value(900)
# Adding a new List Object to the worksheet
listObject = sheet.list_objects[sheet.list_objects.add("A1", "F15", True)]
# Adding Default Style to the table
listObject.table_style_type = TableStyleType.TABLE_STYLE_MEDIUM10
# Show Total
listObject.show_totals = True
# Set the Quarter field's calculation type
listObject.list_columns[1].totals_calculation = TotalsCalculation.COUNT
# Saving the Excel file
workbook.save(dataDir + "output.xlsx")

Advance topics