Diverse modalità per aprire i file
Come aprire un file Excel tramite un percorso
Gli sviluppatori possono aprire un file Microsoft Excel usando il percorso del file sul computer locale specificandolo nel costruttore della classe Workbook. Basta passare il percorso nel costruttore come stringa. Aspose.Cells for Python via .NET rileverà automaticamente il tipo di formato del file.
from aspose.cells import Workbook | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# The path to the documents directory. | |
dataDir = RunExamples.GetDataDir(".") | |
# Opening through Path | |
# Creating a Workbook object and opening an Excel file using its file path | |
workbook1 = Workbook(dataDir + "Book1.xlsx") | |
print("Workbook opened using path successfully!") |
Come aprire un file Excel tramite uno stream
È anche semplice aprire un file Excel come uno stream. Per farlo, utilizzare una versione sovraccaricata del costruttore che prende l’oggetto Stream che contiene il file.
from aspose.cells import Workbook | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# The path to the documents directory. | |
dataDir = RunExamples.GetDataDir(".") | |
# Opening through Stream | |
# Create a Stream object | |
fstream = open(dataDir + "Book2.xls", "rb") | |
# Creating a Workbook object, open the file from a Stream object | |
# That contains the content of file and it should support seeking | |
workbook2 = Workbook(fstream) | |
print("Workbook opened using stream successfully!") | |
fstream.close() |
Come aprire un file con solo dati
Per aprire un file solo con i dati, utilizza le classi LoadOptions e LoadFilter per impostare l’attributo correlato e le opzioni delle classi per il file modello da caricare.
from aspose.cells import LoadDataFilterOptions, LoadFilter, LoadFormat, LoadOptions, Workbook | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# The path to the documents directory. | |
dataDir = RunExamples.GetDataDir(".") | |
# 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 | |
book = Workbook(dataDir + "Book1.xlsx", loadOptions) | |
print("File data imported successfully!") |