打开文件的不同方式

通过路径打开文件

开发人员可以通过在本地计算机上指定它的文件路径来打开 Microsoft Excel 文件**工作簿**类构造函数。只需将构造函数中的路径作为*细绳*Aspose.Cells 会自动检测文件格式类型。

import jpype
import asposecells
jpype.startJVM()
from asposecells.api 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!")
jpype.shutdownJVM()

通过流打开文件

将 Excel 文件作为流打开也很简单。为此,请使用构造函数的重载版本,该版本采用缓冲流包含文件的对象。

import jpype
import asposecells
jpype.startJVM()
from asposecells.api import Workbook
from jpype import java
fis = java.io.FileInputStream("Input.xlsx")
workbook = Workbook(fis)
print("Workbook opened using stream successfully!!")
workbook.save("Output.pdf")
fis.close()
jpype.shutdownJVM()

打开一个只有数据的文件

要打开仅包含数据的文件,请使用**加载选项**和**加载过滤器**classes 为要加载的模板文件设置类的相关属性和选项。

import jpype
import asposecells
jpype.startJVM()
from asposecells.api 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.setLoadFilter(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!")
jpype.shutdownJVM()