JsonHandler — Aspose.Cells FOSS for Python API Reference

aspose-cells-foss poate exporta datele din foaia de calcul în JSON prin JsonHandler clasă. Ieșirea reprezintă fiecare rând ca un obiect JSON indexat pe antetul coloanei, colectat într-un array.

ImageRenderOptions: aspose.cells_foss

from aspose.cells_foss import JsonHandler, JsonSaveOptions
from aspose.cells_foss import save_workbook_as_json

JsonSaveOptions

Opțiuni care controlează modul în care foaia de calcul este serializată în JSON.

ImageRenderOptions

JsonSaveOptions()

ImageRenderOptions

JsonSaveOptions nu are proprietăți configurabile. Transmite o instanță către JsonHandler.save_json() pentru a utiliza comportamentul de serializare implicit.


JsonHandler

JsonHandler conține doar metode statice și nu este niciodată instanțiată.

JsonHandler.save_json

JsonHandler.save_json(workbook, file_path: str, options: JsonSaveOptions | None = None) -> None

Exportă prima foaie de calcul într-un fișier JSON la file_path. Dacă options este None, JsonSaveOptions sunt utilizate.

JsonHandler.save_json_to_dict

JsonHandler.save_json_to_dict(workbook, options: JsonSaveOptions | None = None) -> dict

Serializare a foii de calcul în Python dict (sau list) și îl returnează fără a scrie niciun fișier. Util pentru post-procesare programatică.


Funcție de modul

save_workbook_as_json

save_workbook_as_json(workbook, file_path: str, options: JsonSaveOptions | None = None) -> None

Wrapper convenabil pentru JsonHandler.save_json(workbook, file_path, options).


ImageRenderOptions

Exemplul următor scrie un tabel de vânzări și îl exportă într-un fișier JSON cu format de dată ISO.

from aspose.cells_foss import Workbook, JsonHandler, JsonSaveOptions
import datetime

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

# Header row
ws.cells["A1"].put_value("Date")
ws.cells["B1"].put_value("Product")
ws.cells["C1"].put_value("Amount")

# Data rows
ws.cells["A2"].put_value(datetime.date(2025, 1, 15))
ws.cells["B2"].put_value("Widget A")
ws.cells["C2"].put_value(1250.00)

ws.cells["A3"].put_value(datetime.date(2025, 1, 22))
ws.cells["B3"].put_value("Widget B")
ws.cells["C3"].put_value(840.50)

opts = JsonSaveOptions()

# Save to file
JsonHandler.save_json(wb, "sales.json", options=opts)

# Or retrieve as a Python dict
data = JsonHandler.save_json_to_dict(wb, options=opts)
print(data)

Exemplu de ieșire (sales.json):

{
  "Sales": [
    {"Date": "2025-01-15", "Product": "Widget A", "Amount": 1250.0},
    {"Date": "2025-01-22", "Product": "Widget B", "Amount": 840.5}
  ]
}

Vezi și

 Română