打开文件的不同方式

如何通过路径打开Excel文件

开发者可以通过在 Workbook 类的构造函数中指定本地计算机上的文件路径来打开 Microsoft Excel 文件。只需将路径作为字符串传入构造函数即可。Aspose.Cells for Python via .NET 会自动检测文件格式类型。

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!")

如何通过流打开Excel文件

通过流打开Excel文件也很简单。为此,请使用接收包含文件的对象的构造函数的重载版本。

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()

如何只打开具有数据的文件

要仅打开具有数据的文件,使用LoadOptionsLoadFilter类设置文件模板的相关属性和选项。

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!")