異なる形式のファイルを開く

異なる形式のファイルを開く

Aspose.Cells を使用すると、開発者は、SpreadsheetML、カンマ区切り値 (CSV)、タブ区切りまたはタブ区切り値 (TSV)、ODS ファイルなど、さまざまな形式のスプレッドシート ファイルを開くことができます。このようなファイルを開くには、開発者は異なる Microsoft Excel バージョンのファイルを開く場合と同じ方法を使用できます。

SpreadsheetML ファイルを開く

SpreadsheetML ファイルは、書式設定、数式など、スプレッドシートに関するすべての情報を含むスプレッドシートの XML 表現です。

#include "Aspose.Cells.h"
// The path to the documents directory.
StringPtr dataDir = new String("");
// Opening XML Files
intrusive_ptr <Aspose::Cells::ILoadOptions> loadOptions = Factory::CreateILoadOptions(LoadFormat::LoadFormat_Xml);
// Create a Workbook object and opening the file from its path
intrusive_ptr<IWorkbook> workbook = Factory::CreateIWorkbook(
dataDir->StringAppend(new String("Input.xml")), loadOptions);
// Show following message on console
Console::WriteLine(new String("XML file opened successfully!"));

HTML ファイルを開く

Aspose.Cells を使用すると、HTML ファイルを Workbook オブジェクトで開くことができます。 HTML ファイルは Microsoft Excel 向けである必要があります。つまり、MS-Excel で開くことができます。

#include "Aspose.Cells.h"
// The path to the documents directory.
StringPtr dataDir = new String("");
// Opening HTML Files
intrusive_ptr <Aspose::Cells::ILoadOptions> loadOptions = Factory::CreateILoadOptions(LoadFormat::LoadFormat_Html);
// Create a Workbook object and opening the file from its path
intrusive_ptr<IWorkbook> workbook = Factory::CreateIWorkbook(
dataDir->StringAppend(new String("Input.html")), loadOptions);
// Show following message on console
Console::WriteLine(new String("HTML file opened successfully!"));

CSV ファイルを開く

コンマ区切り値 (CSV) ファイルには、値がコンマで区切られたレコードが含まれています。データは、各列がコンマ文字で区切られ、二重引用符で囲まれたテーブルとして保存されます。フィールド値に二重引用符が含まれている場合は、二重引用符のペアでエスケープされます。 Microsoft Excel を使用して、スプレッドシート データを CSV にエクスポートすることもできます。

#include "Aspose.Cells.h"
// The path to the documents directory.
StringPtr dataDir = new String("");
// Opening CSV Files
intrusive_ptr <Aspose::Cells::ILoadOptions> loadOptions = Factory::CreateILoadOptions(LoadFormat::LoadFormat_CSV);
// Create a Workbook object and opening the file from its path
intrusive_ptr<IWorkbook> workbook = Factory::CreateIWorkbook(
dataDir->StringAppend(new String("Input.csv")), loadOptions);
// Show following message on console
Console::WriteLine(new String("CSV file opened successfully!"));
view raw OpenCSVFile.cpp hosted with ❤ by GitHub

CSV ファイルを開いて無効な文字を置き換える

Excel で、特殊文字を含む CSV ファイルを開くと、文字が自動的に置き換えられます。以下のコード例で示されている Aspose.Cells API でも同じことが行われます。

#include "Aspose.Cells.h"
// The path to the documents directory.
StringPtr dataDir = new String("");
// Opening CSV Files
// Instantiate LoadOptions specified by the LoadFormat.
intrusive_ptr <Aspose::Cells::ITxtLoadOptions> loadOptions = Factory::CreateITxtLoadOptions(LoadFormat::LoadFormat_CSV);
loadOptions->SetSeparator(';');
// Create a Workbook object and opening the file from its path
intrusive_ptr<IWorkbook> workbook = Factory::CreateIWorkbook(
dataDir->StringAppend(new String("InvalidCharacters.csv")), loadOptions);
// Show following message on console
Console::WriteLine(new String("CSV file opened successfully!"));
// Save for check
workbook->Save(new String("Output.xlsx"));

この機能をテストするために、次のリンクからサンプル ソース ファイルをダウンロードできます。

無効な文字.csv

カスタム セパレータを使用してテキスト ファイルを開く

テキスト ファイルは、書式設定なしでスプレッドシート データを保持するために使用されます。このファイルは、カスタマイズされた区切り文字を持つことができる一種のプレーン テキスト ファイルです。

#include "Aspose.Cells.h"
// The path to the documents directory.
StringPtr dataDir = new String("");
// Set for TxtLoadOptions
intrusive_ptr <Aspose::Cells::ITxtLoadOptions> loadOptions = Factory::CreateITxtLoadOptions();
loadOptions->SetSeparator(',');
// Create a Workbook object and opening the file from its path
intrusive_ptr<IWorkbook> workbook = Factory::CreateIWorkbook(
dataDir->StringAppend(new String("CustomSeparator.txt")), loadOptions);
// Show following message on console
Console::WriteLine(new String("TXT file opened successfully!"));
// Save for check
workbook->Save(new String("Output.xlsx"));

この機能をテストするために、次のリンクからサンプル ソース ファイルをダウンロードできます。

CustomSeparator.txt

タブ区切りファイルを開く

タブ区切り (テキスト) ファイルにはスプレッドシート データが含まれますが、書式設定はありません。データは、表やスプレッドシートのように行と列に配置されます。基本的に、タブ区切りファイルは、各列の間にタブがある特別な種類のプレーン テキスト ファイルです。

#include "Aspose.Cells.h"
// The path to the documents directory.
StringPtr dataDir = new String("");
// Set LoadOptions
intrusive_ptr <Aspose::Cells::ILoadOptions> loadOptions = Factory::CreateILoadOptions(LoadFormat::LoadFormat_TabDelimited);
// Create a Workbook object and opening the file from its path
intrusive_ptr<IWorkbook> workbook = Factory::CreateIWorkbook(
dataDir->StringAppend(new String("TabDelimited.txt")), loadOptions);
// Show following message on console
Console::WriteLine(new String("TabDelimited file opened successfully!"));
// Save for check
workbook->Save(new String("Output.xlsx"));

タブ区切り値 (TSV) ファイルを開く

タブ区切り値 (TSV) ファイルにはスプレッドシート データが含まれていますが、書式設定はありません。テーブルやスプレッドシートのように、データが行と列に配置されるタブ区切りファイルと同じです。

#include "Aspose.Cells.h"
// The path to the documents directory.
StringPtr dataDir = new String("");
// Set LoadOptions
intrusive_ptr <Aspose::Cells::ILoadOptions> loadOptions = Factory::CreateILoadOptions(LoadFormat::LoadFormat_TSV);
// Create a Workbook object and opening the file from its path
intrusive_ptr<IWorkbook> workbook = Factory::CreateIWorkbook(
dataDir->StringAppend(new String("Input.tsv")), loadOptions);
// Show following message on console
Console::WriteLine(new String("TSV file opened successfully!"));
// Save for check
workbook->Save(new String("Output.xlsx"));
view raw OpenTSVFile.cpp hosted with ❤ by GitHub

SXC ファイルを開く

StarOffice Calc は Microsoft Excel に似ており、数式、チャート、関数、およびマクロをサポートしています。このソフトウェアで作成されたスプレッドシートは、拡張子 SXC で保存されます。 SXC ファイルは、OpenOffice.org Calc スプレッドシート ファイルにも使用されます。次のコード サンプルで示すように、Aspose.Cells は SXC ファイルを読み取ることができます。

#include "Aspose.Cells.h"
// The path to the documents directory.
StringPtr dataDir = new String("");
// Set LoadOptions
intrusive_ptr <Aspose::Cells::ILoadOptions> loadOptions = Factory::CreateILoadOptions(LoadFormat::LoadFormat_SXC);
// Create a Workbook object and opening the file from its path
intrusive_ptr<IWorkbook> workbook = Factory::CreateIWorkbook(
dataDir->StringAppend(new String("Input.sxc")), loadOptions);
// Show following message on console
Console::WriteLine(new String("SXC file opened successfully!"));
// Save for check
workbook->Save(new String("Output.xlsx"));
view raw OpenSXCFile.cpp hosted with ❤ by GitHub

FODS ファイルを開く

FODS ファイルは、圧縮せずに OpenDocument XML で保存されたスプレッドシートです。次のコード サンプルで示すように、Aspose.Cells は FODS ファイルを読み取ることができます。

#include "Aspose.Cells.h"
// The path to the documents directory.
StringPtr dataDir = new String("");
// Set LoadOptions
intrusive_ptr <Aspose::Cells::ILoadOptions> loadOptions = Factory::CreateILoadOptions(LoadFormat::LoadFormat_FODS);
// Create a Workbook object and opening the file from its path
intrusive_ptr<IWorkbook> workbook = Factory::CreateIWorkbook(
dataDir->StringAppend(new String("Input.fods")), loadOptions);
// Show following message on console
Console::WriteLine(new String("FODS file opened successfully!"));
// Save for check
workbook->Save(new String("Output.xlsx"));