さまざまな形式のファイルを開く

異なるフォーマットのファイルを開く

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

SpreadsheetML ファイルを開く

SpreadsheetML ファイルは、書式設定、数式など、スプレッドシートに関するすべての情報を含む XML 表現です。Microsoft Excel XP 以降、XML エクスポートオプションが追加され、スプレッドシートを SpreadsheetML ファイルにエクスポートできます。

Aspose::Cells::Startup();
// The path to the documents directory.
U16String dataDir(u"");
// Opening XML Files
LoadOptions loadOptions(LoadFormat::Xml);
// Create a Workbook object and opening the file from its path
Workbook workbook(dataDir + u"Input.xml", loadOptions);
// Show following message on console
std::cout << "XML file opened successfully!" <<std::endl;
Aspose::Cells::Cleanup();

HTML ファイルを開く

Aspose.Cells は、HTML ファイルを Workbook オブジェクトに開くことを可能にします。HTML ファイルは Microsoft Excel 向けである必要があります。

Aspose::Cells::Startup();
// The path to the documents directory.
U16String dataDir(u"");
// Opening HTML Files
LoadOptions loadOptions(LoadFormat::Html);
// Create a Workbook object and opening the file from its path
Workbook workbook(dataDir + u"Input.html", loadOptions);
// Show following message on console
std::cout << "HTML file opened successfully!" << std::endl;
Aspose::Cells::Cleanup();

CSV ファイルを開く

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

Aspose::Cells::Startup();
// The path to the documents directory.
U16String dataDir = u"";
// Instantiate LoadOptions specified by the LoadFormat.
LoadOptions loadOptions4(LoadFormat::Csv);
// Create a Workbook object and opening the file from its path
Workbook wbCSV(dataDir + u"Book_CSV.csv", loadOptions4);
std::cout << "CSV file opened successfully!" << std::endl;
Aspose::Cells::Cleanup();

CSV ファイルを開くと無効な文字を置換する

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

Aspose::Cells::Startup();
//Source directory
U16String sourceDir = u"";
U16String filename = sourceDir + u"InvalidCharacters.csv";
//Load CSV file
LoadFilter filter(LoadDataFilterOptions::CellData);
TxtLoadOptions csvOpt;
csvOpt.SetSeparator(';');
csvOpt.SetLoadFilter(&filter);
csvOpt.SetCheckExcelRestriction(false);
csvOpt.SetConvertNumericData(false);
csvOpt.SetConvertDateTimeData(false);
Workbook workbook(filename, csvOpt);
std::cout << "CSV file opened successfully!" << std::endl;
// Save for check
workbook.Save(u"Output.xlsx");
Aspose::Cells::Cleanup();

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

InvalidCharacters.csv

カスタム区切り記号を使用してテキストファイルを開く

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

Aspose::Cells::Startup();
// The path to the documents directory.
U16String dataDir = u"";
U16String filePath = dataDir + u"CustomSeparator.txt";
// Instantiate Text File's LoadOptions
TxtLoadOptions txtLoadOptions;
// Specify the separator
txtLoadOptions.SetSeparator(u',');
// Specify the encoding type
txtLoadOptions.SetEncoding(EncodingType::UTF8);
// Create a Workbook object and opening the file from its path
Workbook wb(filePath, txtLoadOptions);
// Save file
wb.Save(dataDir + u"output.xlsx");
Aspose::Cells::Cleanup();

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

CustomSeparator.txt

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

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

Aspose::Cells::Startup();
// The path to the documents directory.
U16String dataDir = u"";
// Opening Tab Delimited Files
// Instantiate LoadOptions specified by the LoadFormat.
LoadOptions loadOptions5(LoadFormat::TabDelimited);
// Create a Workbook object and opening the file from its path
Workbook wbTabDelimited(dataDir + u"Book1TabDelimited.txt", loadOptions5);
// Show following message on console
std::cout << "TabDelimited file opened successfully!" << std::endl;
Aspose::Cells::Cleanup();

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

タブ区切り値(TSV)ファイルには、スプレッドシートデータが含まれていますが、フォーマットはありません。データは表やスプレッドシートのように行と列で配置されています。データはタブ区切りファイルと同様であり、各列の間にタブがあります。

Aspose::Cells::Startup();
//Source directory
U16String sourceDir = u"";
// Instantiate LoadOptions specified by the LoadFormat.
LoadOptions loadOptions(LoadFormat::Tsv);
// Create a Workbook object and opening the file from its path
Workbook workbook(sourceDir + u"SampleTSVFile.tsv", loadOptions);
// Using the Sheet 1 in Workbook
Worksheet worksheet = workbook.GetWorksheets().Get(0);
// Accessing a cell using its name
Cell cell = worksheet.GetCells().Get(u"C3");
std::cout << "Cell Name: " << cell.GetName().ToUtf8() << " Value: " << cell.GetStringValue().ToUtf8() << std::endl;
Aspose::Cells::Cleanup();

SXCファイルを開く

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

Aspose::Cells::Startup();
//Source directory
U16String sourceDir = u"";
// Instantiate LoadOptions specified by the LoadFormat.
LoadOptions loadOptions(LoadFormat::Sxc);
// Create a Workbook object and opening the file from its path
Workbook workbook(sourceDir + u"SampleSXC.sxc", loadOptions);
// Using the Sheet 1 in Workbook
Worksheet worksheet = workbook.GetWorksheets().Get(0);
// Accessing a cell using its name
Cell cell = worksheet.GetCells().Get(u"C3");
std::cout << "Cell Name: " << cell.GetName().ToUtf8() << " Value: " << cell.GetStringValue().ToUtf8() << std::endl;
Aspose::Cells::Cleanup();

FODSファイルを開く

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

Aspose::Cells::Startup();
//Source directory
U16String sourceDir = u"";
// Instantiate LoadOptions specified by the LoadFormat.
LoadOptions loadOptions(LoadFormat::Fods);
// Create a Workbook object and opening the file from its path
Workbook workbook(sourceDir + "SampleFods.fods", loadOptions);
// Show following message on console
std::cout << "FODS file opened successfully!" << std::endl;
Aspose::Cells::Cleanup();