ファイルを開くさまざまな方法
Contents
[
Hide
]
Aspose.Cellsを使用すれば、たとえばデータを取得したり、開発プロセスを高速化するためのデザイナー テンプレートを使用したりするために、ファイルを開くことは簡単です。
パスを介してファイルを開く
開発者は、Workbookクラスのコンストラクターでfile_pathを指定することで、ローカルコンピューター上のMicrosoft Excelファイルを開くことができます。コンストラクターにパスをstringとして渡すだけです。Aspose.Cellsは自動的にファイル形式を検出します。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import aspose.cells | |
from aspose.cells 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!") |
ストリームを介してファイルを開く
Excelファイルをストリームとして簡単に開くこともできます。ファイルを含むBufferStreamオブジェクトを使用するオーバーロードされたコンストラクターを使用します。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import io | |
import aspose.cells | |
from aspose.cells import Workbook, CellsHelper, License, SaveFormat | |
with open('Input.xlsx', 'rb') as file: | |
input_stream = io.BytesIO(file.read()) | |
workbook = Workbook(input_stream) | |
out_stream = io.BytesIO() | |
workbook.save(out_stream, SaveFormat.XLSX) | |
out_bytes = out_stream.getvalue() | |
print(out_bytes) | |
out_stream.close() | |
input_stream.close() | |
print("Workbook opened using stream successfully!") |
データのみでファイルを開く
データのみでファイルを開くには、LoadOptionsおよびLoadFilterクラスを使用して、テンプレートファイルの関連属性とオプションを設定します。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import aspose.cells | |
from aspose.cells 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.load_filter = 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!") |
大規模なスプレッドシートをロードする際に、{0}コンストラクタがSystem.OutOfMemoryExceptionをスローする可能性がかなり高いです。この例外は、利用可能なメモリが不十分であるため、スプレッドシート全体を完全にメモリに読み込むことができず、メモリ設定を有効にしてスプレッドシートをロードする必要があることを示しています。
Workbookのコンストラクターが大きなスプレッドシートを完全に読み込むための十分なメモリがないことを示すSystem.OutOfMemoryExceptionをスローする可能性があります。これは利用可能なメモリが不十分であることを示唆し、スプレッドシートを完全に読み込むためにはメモリが有効になっている必要があるためです。