Kundenspezifische Klasse in Excel umwandeln
Kundenspezifische Klasse in Excel umwandeln
Manchmal haben wir eine Sammlung von Klassen, und wenn wir Klasseninformationen in eine Excel-Datei importieren möchten, ist eine praktische Lösung, die Reflection-Mechanismus von Python zu verwenden, um sowohl die Klassendaten als auch die Namen der Mitgliedsvariablen einzuschließen, ohne im Voraus die spezifischen Metadaten der Klasse zu kennen. Hier ist ein Beispielcode, der demonstriert, wie man Daten aus einer benutzerdefinierten Klassensammlung mit Aspose.Cells für Python via .NET in eine Excel-Datei importiert: Die Datei ImportCustomObject.py definiert die Klasseninformationen, die importiert werden sollen, und nutzt Pythons Reflexionsmechanismus, um sowohl die Klassendaten als auch die Namen der Mitgliedsvariablen in bestimmte Zellbereiche einzufügen.
from datetime import date, datetime | |
import aspose.cells as ac | |
class MyClass: | |
def __init__(self, Name, Age, Is_Student, Date, DateTime, Height): | |
self.Name = Name | |
self.Age = Age | |
self.Is_Student = Is_Student | |
self.Date = Date | |
self.DateTime = DateTime | |
self.Height = Height | |
def import_custom_object(people, cells, row, column, import_options): | |
# If a header needs to be displayed, write the header (attribute names) | |
if import_options.is_field_name_shown: | |
header = vars(people[0]).keys() | |
for col_index, attr_name in enumerate(header): | |
cells.get(row, column + col_index).put_value(attr_name) | |
row += 1 | |
# Write the attribute values of the object | |
for obj in people: | |
for col_index, (attr_name, attr_value) in enumerate(vars(obj).items()): | |
cell = cells.get(row, column + col_index) | |
if isinstance(attr_value, (int, float, str, bool)): | |
cell.put_value(attr_value) | |
elif isinstance(attr_value, (date, datetime)): | |
cell.put_value(attr_value) | |
style = cell.get_style() | |
if isinstance(attr_value, datetime): | |
style.custom = "yyyy-MM-dd HH:mm:ss" | |
elif isinstance(attr_value, date): | |
style.number = 14 # Excel Date Format | |
cell.set_style(style) | |
else: | |
cell.put_value(str(attr_value)) | |
row += 1 |
Die Datei TestImportCustomObject.py zeigt einen einfachen Anwendungsfall. Benutzer können dieses Beispiel verwenden oder geringfügige Änderungen vornehmen, um ihre eigenen Daten zu importieren.
from datetime import date, datetime | |
import aspose.cells as ac | |
from ImportCustomObject import MyClass, import_custom_object | |
people = [ | |
MyClass("Bob", 30, True, date(2024, 3, 9), datetime.now(), 112.0), | |
MyClass("Lucy", 25, False, date(2024, 3, 10), datetime.now(), 111.1), | |
MyClass("Tom", 45, True, date(2024, 3, 11), datetime.now(), 110.8), | |
] | |
workbook = ac.Workbook() | |
worksheet = workbook.worksheets[0] | |
cells = worksheet.cells | |
import_options = ac.ImportTableOptions() | |
# Display the table header or not | |
import_options.is_field_name_shown = True | |
start_row = 4 | |
start_column = 1 | |
import_custom_object(people, cells, start_row, start_column, import_options) | |
workbook.save("import_custom_object.xlsx") |