Diversi modi per aprire i file
Contents
[
Hide
]
Con Aspose.Cells è semplice aprire file, ad esempio, per recuperare dati o utilizzare un modello di designer per velocizzare il processo di sviluppo.
Apertura di un file tramite un percorso
Gli sviluppatori possono aprire un file Excel Microsoft utilizzando il percorso del file sul computer locale specificandolo nelCartella di lavorocostruttore di classe. Basta passare il percorso nel costruttore come acorda. Aspose.Cells rileverà automaticamente il tipo di formato del file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 un flusso
È anche semplice aprire un file Excel come flusso. Per fare ciò, usa una versione sovraccaricata del costruttore che accetta ilBufferStreamoggetto che contiene il file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 con solo dati, utilizzare l’estensioneLoadOptions eCarica filtroclasses per impostare l’attributo correlato e le opzioni delle classi per il file modello da caricare.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!") |
Verrà generata un’eccezione se si tenta di aprire file Excel non nativi o altri formati di file (ad esempio PPT/PPTX, DOC/DOCX, ecc.) entro Aspose.Cells.
Ci sono buone possibilità che ilCartella di lavoro il costruttore può lanciareSystem.OutOfMemoryException durante il caricamento di fogli di calcolo di grandi dimensioni. Questa eccezione suggerisce che la memoria disponibile non è sufficiente per caricare completamente il foglio di calcolo nella memoria, pertanto il foglio di calcolo deve essere caricato abilitando le Preferenze memoria.