Diverse modalità per aprire i file
Apertura di un file tramite un percorso
Gli sviluppatori possono aprire un file Microsoft Excel utilizzando il percorso del file sul computer locale specificandolo nel costruttore della classe Workbook. Basta passare il percorso nel costruttore come una stringa. Aspose.Cells rileverà automaticamente il tipo di formato del file.
import aspose.cells | |
from aspose.cells import Workbook | |
# Opening a File via a Path | |
# The path to the documents directory. | |
dataDir = "" | |
# Opening through Path | |
# Creating a Workbook object and opening an Excel file using its file path | |
workbook = Workbook(dataDir + "Input.xlsx") | |
print("Workbook opened using path successfully!") |
Apertura di un File tramite uno Stream
È anche semplice aprire un file Excel come uno stream. Per farlo, utilizzare una versione sovraccaricata del costruttore che accetta l’oggetto BufferStream che contiene il file.
import io | |
import aspose.cells | |
from aspose.cells import Workbook, CellsHelper, License, SaveFormat | |
with open('Input.xlsx', 'rb') as file: | |
input_stream = io.BytesIO(file.read()) | |
workbook = Workbook(input_stream) | |
out_stream = io.BytesIO() | |
workbook.save(out_stream, SaveFormat.XLSX) | |
out_bytes = out_stream.getvalue() | |
print(out_bytes) | |
out_stream.close() | |
input_stream.close() | |
print("Workbook opened using stream successfully!") |
Apertura di un File con solo Dati
Per aprire un file solo con dati, utilizzare le classi LoadOptions e LoadFilter per impostare l’attributo e le opzioni correlate delle classi per il file di modello da caricare.
import aspose.cells | |
from aspose.cells import Workbook, LoadOptions, LoadFormat, LoadFilter, LoadDataFilterOptions | |
# Opening a File with Data only | |
# The path to the documents directory. | |
dataDir = "" | |
# Load only specific sheets with data and formulas | |
# Other objects, items etc. would be discarded | |
# Instantiate LoadOptions specified by the LoadFormat | |
loadOptions = LoadOptions(LoadFormat.XLSX) | |
# Set LoadFilter property to load only data & cell formatting | |
loadOptions.load_filter = LoadFilter(LoadDataFilterOptions.CELL_DATA) | |
# Create a Workbook object and opening the file from its path | |
workbook = Workbook(dataDir + "Input.xlsx", loadOptions) | |
print("File data imported successfully!") |