Table — Aspose.Cells FOSS for Python API Reference

aspose-cells-foss đại diện cho các bảng có cấu trúc của Excel (ListObjects) thông qua the Table lớp và the TableCollection có thể truy cập dưới dạng ws.list_objects. Các bảng cung cấp các phạm vi có tên, bộ lọc tự động, hàng tiêu đề và kiểu dáng dải hàng được tích hợp trong định dạng XLSX.

Enumerations: aspose.cells_foss

from aspose.cells_foss import TableStyleInfo, TableColumn

TableStyleInfo

Mô tả kiểu dáng trực quan được áp dụng cho bảng (dải màu, làm nổi bật cột đầu tiên/cuối cùng).

Enumerations

TableStyleInfo()

Enumerations

EnumerationsEnumerationsEnumerations
namestrTên kiểu bảng Excel (ví dụ,., "TableStyleMedium9").
show_first_columnboolTrue để áp dụng định dạng đặc biệt cho cột đầu tiên.
show_last_columnboolTrue để áp dụng định dạng đặc biệt cho cột cuối cùng.
show_row_stripesboolTrue để áp dụng dải hàng xen kẽ.
show_column_stripesboolTrue để áp dụng dải cột xen kẽ.

TableColumn

Mô tả một cột đơn lẻ trong bảng.

Enumerations

TableColumn(col_id: int, name: str)
EnumerationsEnumerationsEnumerations
col_idint1-based column identifier within the table (not the worksheet column index).
namestrVăn bản tiêu đề cột. Phải là duy nhất trong bảng.

Enumerations

EnumerationsEnumerationsEnumerations
idintĐịnh danh cột (chỉ đọc).
namestrVăn bản tiêu đề cột.
totals_row_functionstrHàm tổng hợp hiển thị trong hàng tổng cộng: 'none', 'sum', 'average', 'count', 'countNums', 'max', 'min', 'stdDev', 'var', 'custom'.
totals_row_labelstrVăn bản nhãn hiển thị trong ô tổng cộng khi totals_row_function'none'.
totals_row_formulastrCông thức tùy chỉnh được sử dụng khi totals_row_function'custom'.

Enumerations

Table các đối tượng không được tạo trực tiếp. Sử dụng ws.list_objects.add(...) hoặc ws.list_objects.add_with_range(...).

Enumerations

EnumerationsEnumerationsEnumerations
namestrTên bảng nội bộ (phải là duy nhất trong workbook, không có dấu cách).
display_namestrTên hiển thị được hiển thị trong ô Tên của Excel.
refstrPhạm vi ô được bảng bao phủ (ví dụ,., "A1:D10").
has_headersboolTrue nếu hàng đầu tiên của phạm vi chứa tiêu đề cột.
show_totals_rowboolTrue để hiển thị một hàng tổng hợp tổng cộng ở cuối bảng.
show_auto_filterboolTrue để hiển thị các mũi tên thả xuống của bộ lọc tự động trong hàng tiêu đề.
table_style_infoTableStyleInfoCài đặt kiểu hiển thị (đánh dấu dải, làm nổi bật cột).
columnslist[TableColumn]Danh sách có thứ tự của TableColumn các đối tượng (một cho mỗi cột của bảng).

TableCollection

TableCollection được truy cập như ws.list_objects. Nó quản lý tất cả các bảng có cấu trúc trên một worksheet.

Enumerations

add

ws.list_objects.add(
    start_row: int,
    start_col: int,
    end_row: int,
    end_col: int,
    has_headers: bool = True,
    name: str | None = None
) -> Table

Tạo một bảng trên phạm vi ô đã cho. Tất cả các chỉ mục là 0-based. Nếu nameNone, một tên sẽ được tạo tự động (ví dụ,., "Table1").

add_with_range

ws.list_objects.add_with_range(
    cell_range: str,
    name: str | None = None,
    has_headers: bool = True
) -> Table

Tạo một bảng từ chuỗi phạm vi kiểu A1 (ví dụ,., "A1:D10"). Dễ đọc hơn so với add() cho các phạm vi được mã hoá cứng.

Thuộc tính và Lặp lại

EnumerationsEnumerations
countSố bảng trên trang tính này (chỉ đọc).
__len__()Enumerations count.
__iter__()Duyệt qua tất cả Table đối tượng.
__getitem__(index)Trả về Table ở chỉ số bắt đầu từ 0 index.

Enumerations

Ví dụ sau ghi một bộ dữ liệu tồn kho sản phẩm và chuyển nó thành một bảng Excel có kiểu dáng, kèm dòng tổng cộng.

from aspose.cells_foss import Workbook

wb = Workbook()
ws = wb.worksheets[0]
ws.name = "Inventory"

# Write header and data rows
headers = ["SKU", "Product", "Qty", "Unit Price"]
rows = [
    ["A001", "Widget Alpha", 150, 12.99],
    ["A002", "Widget Beta",   80, 24.50],
    ["A003", "Gadget Gamma",  45, 49.99],
    ["A004", "Gadget Delta",  210, 8.75],
]

for col, h in enumerate(headers):
    ws.cells[0][col].put_value(h)
for row_i, row in enumerate(rows, start=1):
    for col_i, val in enumerate(row):
        ws.cells[row_i][col_i].put_value(val)

# Create a table over A1:D5
table = ws.list_objects.add_with_range("A1:D5", name="InventoryTable")

# Configure style
table.table_style_info.name = "TableStyleMedium9"
table.table_style_info.show_row_stripes = True
table.table_style_info.show_first_column = False
table.table_style_info.show_last_column = False

# Add a totals row with sum on Qty and Unit Price columns
table.show_totals_row = True
table.columns[2].totals_row_function = "sum"    # Qty
table.columns[3].totals_row_function = "sum"    # Unit Price
table.columns[0].totals_row_label = "Total"

wb.save("inventory_table.xlsx")

Xem Thêm

 Tiếng Việt