ファイルを開くさまざまな方法
Contents
[
Hide
]
Aspose.Cells を使用すると、ファイルを開いてデータを取得したり、デザイナー テンプレートを使用して開発プロセスをスピードアップしたりすることが簡単になります。
パス経由でファイルを開く
開発者は、ローカル コンピューター上のファイル パスを使用して Microsoft Excel ファイルを開くことができます。**ワークブック**クラス コンストラクタ。コンストラクターにパスを渡すだけです。ストリングAspose.Cells は、ファイル形式の種類を自動的に検出します。
This file contains hidden or 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
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Opening through Path | |
// Creating a Workbook object and opening an Excel file using its file path | |
Workbook workbook1 = new Workbook(dataDir + "Book1.xlsx"); | |
Console.WriteLine("Workbook opened using path successfully!"); |
ストリーム経由でファイルを開く
Excel ファイルをストリームとして開くのも簡単です。これを行うには、コンストラクターのオーバーロードされたバージョンを使用します。ストリームファイルを含むオブジェクト。
This file contains hidden or 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
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Opening through Stream | |
// Create a Stream object | |
FileStream fstream = new FileStream(dataDir + "Book2.xls", FileMode.Open); | |
// Creating a Workbook object, open the file from a Stream object | |
// That contains the content of file and it should support seeking | |
Workbook workbook2 = new Workbook(fstream); | |
Console.WriteLine("Workbook opened using stream successfully!"); | |
fstream.Close(); |
データのみのファイルを開く
データのみのファイルを開くには、**LoadOptionsとLoadFilter**classes を使用して、ロードするテンプレート ファイルの関連する属性とクラスのオプションを設定します。
This file contains hidden or 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
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Load only specific sheets with data and formulas | |
// Other objects, items etc. would be discarded | |
// Instantiate LoadOptions specified by the LoadFormat | |
LoadOptions loadOptions = new LoadOptions(LoadFormat.Xlsx); | |
// Set LoadFilter property to load only data & cell formatting | |
loadOptions.LoadFilter = new LoadFilter(LoadDataFilterOptions.CellData); | |
// Create a Workbook object and opening the file from its path | |
Workbook book = new Workbook(dataDir + "Book1.xlsx", loadOptions); | |
Console.WriteLine("File data imported successfully!"); |
表示されているシートのみを読み込んでいます
読み込み中**ワークブック**ワークブック内の表示可能なワークシートのデータのみが必要な場合があります。 Aspose.Cells を使用すると、ワークブックの読み込み中に非表示のワークシートのデータをスキップできます。これを行うには、**LoadFilter**クラスにそのインスタンスを渡します**LoadOptions.LoadFilter**財産。
This file contains hidden or 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
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
string sampleFile = "output.xlsx"; | |
string samplePath = dataDir + sampleFile; | |
// Create a sample workbook | |
// and put some data in first cell of all 3 sheets | |
Workbook createWorkbook = new Workbook(); | |
createWorkbook.Worksheets["Sheet1"].Cells["A1"].Value = "Aspose"; | |
createWorkbook.Worksheets.Add("Sheet2").Cells["A1"].Value = "Aspose"; | |
createWorkbook.Worksheets.Add("Sheet3").Cells["A1"].Value = "Aspose"; | |
createWorkbook.Worksheets["Sheet3"].IsVisible = false; | |
createWorkbook.Save(samplePath); | |
// Load the sample workbook | |
LoadOptions loadOptions = new LoadOptions(); | |
loadOptions.LoadFilter = new CustomLoad(); | |
Workbook loadWorkbook = new Workbook(samplePath, loadOptions); | |
Console.WriteLine("Sheet1: A1: {0}", loadWorkbook.Worksheets["Sheet1"].Cells["A1"].Value); | |
Console.WriteLine("Sheet1: A2: {0}", loadWorkbook.Worksheets["Sheet2"].Cells["A1"].Value); | |
Console.WriteLine("Sheet1: A3: {0}", loadWorkbook.Worksheets["Sheet3"].Cells["A1"].Value); |
これがの実装ですCustomnLoad上記のスニペットで参照されているクラス。
This file contains hidden or 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
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
class CustomLoad : LoadFilter | |
{ | |
public override void StartSheet(Worksheet sheet) | |
{ | |
if (sheet.IsVisible) | |
{ | |
// Load everything from visible worksheet | |
this.LoadDataFilterOptions = LoadDataFilterOptions.All; | |
} | |
else | |
{ | |
// Load nothing | |
this.LoadDataFilterOptions = LoadDataFilterOptions.Structure; | |
} | |
} | |
} |
Aspose.Cells までにネイティブでない Excel ファイルまたはその他のファイル形式 (PPT/PPTX、DOC/DOCX など) を開こうとすると、例外がスローされます。
かなりの可能性があります**ワークブック**コンストラクターがスローする可能性があります*System.OutOfMemoryException*大きなスプレッドシートの読み込み中。この例外は、スプレッドシートをメモリに完全にロードするには使用可能なメモリが不足していることを示しているため、メモリ設定を有効にしてスプレッドシートをロードする必要があります。