Different Ways to Open Files
Opening a File via a Path
Developers can open a Microsoft Excel file using its file path on the local computer by specifying it in the Workbook class constructor. Simply pass the path in the constructor as a string. Aspose.Cells will automatically detect the file format type.
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() |
Opening a File via a Stream
It is also simple to open an Excel file as a stream. To do so, use an overloaded version of the constructor that takes the BufferStream object that contains the file.
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() |
Opening a File with Data only
To open a file with data only, use the LoadOptions and LoadFilter classes to set the related attribute and options of the classes for the template file to be loaded.
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() |