跳转至

综合示例

完整报表生成

from symphra_excel import Workbook
from symphra_excel.styles import PredefinedStyles, CellStyle
from symphra_excel.images import ExcelImage

with Workbook() as wb:
    sheet = wb.create_worksheet("综合报表")

    logo = ExcelImage("logo.png", width=100, height=40)
    sheet.insert_image("A1", logo)

    sheet.set_cell_value("A3", "2024年度销售报表")
    sheet.apply_style("A3", PredefinedStyles.title_style())
    sheet.merge_cells("A3:D3")

    headers = ["日期", "产品", "销售额", "利润"]
    for col_idx, header in enumerate(headers, start=1):
        cell = f"{chr(64+col_idx)}5"
        sheet.set_cell_value(cell, header)
        sheet.apply_style(cell, PredefinedStyles.header_style())

    data = [
        ["2024-01-01", "产品A", 1000, 300],
        ["2024-01-02", "产品B", 1500, 450],
        ["2024-01-03", "产品C", 1200, 360],
    ]

    for row_idx, row in enumerate(data, start=6):
        sheet.set_cell_value(f"A{row_idx}", row[0])
        sheet.apply_style(f"A{row_idx}", PredefinedStyles.data_style())

        sheet.set_cell_value(f"B{row_idx}", row[1])
        sheet.apply_style(f"B{row_idx}", PredefinedStyles.data_style())

        sheet.set_cell_value(f"C{row_idx}", row[2])
        sheet.apply_style(f"C{row_idx}", PredefinedStyles.currency_style())

        sheet.set_cell_value(f"D{row_idx}", row[3])
        sheet.apply_style(f"D{row_idx}", PredefinedStyles.currency_style())

    total_row = len(data) + 6
    sheet.set_cell_value(f"B{total_row}", "总计")
    sheet.apply_style(f"B{total_row}", PredefinedStyles.total_style())

    sheet.set_cell_value(f"C{total_row}", f"=SUM(C6:C{total_row-1})")
    sheet.apply_style(f"C{total_row}", PredefinedStyles.total_style())

    sheet.set_cell_value(f"D{total_row}", f"=SUM(D6:D{total_row-1})")
    sheet.apply_style(f"D{total_row}", PredefinedStyles.total_style())

    for col in ["A", "B", "C", "D"]:
        sheet.set_column_width(col, 15)

    wb.save("comprehensive_report.xlsx")

相关文档