Olika sätt att öppna filer
Så öppnar du en Excel-fil via en sökväg
Utvecklare kan öppna en Microsoft Excel-fil med dess filväg på den lokala datorn genom att specificera den i Workbook-klasskonstruktören. Ange helt enkelt sökvägen som en sträng i konstruktören. Aspose.Cells för Python via .NET kommer automatiskt att upptäcka filformatet.
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!") |
Så öppnar du en Excel-fil via en ström
Det är också enkelt att öppna en Excel-fil som en ström. För att göra detta, använd en överlagrad version av konstruktören som tar Stream-objektet som innehåller filen.
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() |
Så öppnar du en fil med endast data
För att öppna en fil med endast data, använd LoadOptions och LoadFilter klasserna för att ställa in de relaterade attributen och alternativen för klasserna för mönsterfilen som ska laddas.
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!") |