はじめに
インストール
NuGet を通じて Aspose Cells をインストールする
NuGet は、最も簡単な方法で Aspose.Cells for C++ をダウンロードしてインストールする方法です。
- C++ の Microsoft Visual Studio プロジェクトを作成します。
- “Aspose.Cells.h” ヘッダーファイルをインクルードします。
- Microsoft Visual Studio および NuGet パッケージマネージャーを開きます。
- “aspose.cells.cpp” を検索して、必要な Aspose.Cells for C++ を見つけます。
- “インストール” をクリックすると、Aspose.Cells for C++ がダウンロードされ、プロジェクトに参照されます。
また、nuget web ページから aspose.cells をダウンロードすることもできます: Aspose.Cells for C++ NuGet パッケージ
詳細な手順については、こちら
Windows 上で Aspose.Cells for C++ を使用するデモ
- 次のページから Aspose.Cells for C++ をダウンロードします: Windows 用 Aspose.Cells for C++ をダウンロード
- パッケージを解凍すると、Aspose.Cells for C++ の使用方法に関する例が含まれています。
- example.sln を Visual Studio 2017 またはそれ以上のバージョンで開きます
- main.cpp: このファイルには Aspose.Cells for C++ をテストするためのコーディングが示されています
Linux 上で Aspose.Cells for C++ を使用するデモ
- 次のページから Aspose.Cells for C++ をダウンロードします: Linux 用 Aspose.Cells for C++ をダウンロード
- パッケージを解凍すると、Linux での Aspose.Cells for C++ の使用方法に関する例が含まれています。
- 例がある場所に移動していることを確認します。
- “cmake -S example -B example/build -DCMAKE_BUILD_TYPE=Release” を実行します
- “cmake –build example/build” を実行します
Mac OS 上で Aspose.Cells for C++ を使用するデモ
- 次のページから Aspose.Cells for C++ をダウンロードします: MacOS 用 Aspose.Cells for C++ をダウンロード
- パッケージを解凍すると、MacOS での Aspose.Cells for C++ の使用方法に関する例が含まれています。
- 例がある場所に移動していることを確認します。
- “cmake -S example -B example/build -DCMAKE_BUILD_TYPE=Release” を実行します
- “cmake –build example/build” を実行します
Hello Worldアプリケーションの作成
以下の手順により、Aspose.Cells APIを使用してHello Worldアプリケーションを作成できます:
- Workbook クラスのインスタンスを作成します。
- ライセンスをお持ちの場合は、適用します。 評価版を使用している場合は、ライセンスに関連するコード行をスキップします。
- Excelファイルのワークシートの任意のセルにアクセスします。
- セルに “Hello World!” という単語を挿入します。
- 変更されたMicrosoft Excelファイルを生成します。
上記の手順の実装は、以下の例で示されています。
コードサンプル: 新しいワークブックの作成
次の例では、新しいブックをゼロから作成し、最初のワークシートのセルA1に “Hello World!” を挿入し、Excelファイルを保存します。
#include "Aspose.Cells.h" | |
using namespace Aspose::Cells; | |
int main(void) | |
{ | |
Aspose::Cells::Startup(); | |
try | |
{ | |
// Create a License object | |
License license; | |
// Set the license of Aspose.Cells to avoid the evaluation limitations | |
license.SetLicense(u"Aspose.Cells.lic"); | |
// Instantiate a Workbook object that represents Excel file. | |
Workbook wb; | |
// When you create a new workbook, a default "Sheet1" is added to the workbook. | |
Worksheet sheet = wb.GetWorksheets().Get(0); | |
// Access the "A1" cell in the sheet. | |
Cell cell = sheet.GetCells().Get(u"A1"); | |
// Input the "Hello World!" text into the "A1" cell | |
cell.PutValue(u"Hello World!"); | |
// Save the Excel file. | |
wb.Save(u"MyBook_out.xlsx"); | |
} | |
catch (const CellsException& ex) | |
{ | |
std::cout << ex.GetErrorMessage().ToUtf8() << std::endl; | |
} | |
Aspose::Cells::Cleanup(); | |
} |
コードサンプル: 既存のファイルを開く
次の例では、既存のMicrosoft Excelテンプレートファイルを開き、セルA1の値を取得してチェックします。
#include "Aspose.Cells.h" | |
using namespace Aspose::Cells; | |
/*Check result*/ | |
#define EXPECT_TRUE(condition) \ | |
if (condition) printf("--%s,line:%d -> Ok --\n", __FUNCTION__, __LINE__); \ | |
else printf("--%s,line:%d->>>> Failed!!!! <<<<--\n", __FUNCTION__, __LINE__); | |
int main(void) | |
{ | |
Aspose::Cells::Startup(); | |
try | |
{ | |
// Create a License object | |
License license; | |
// Set the license of Aspose.Cells to avoid the evaluation limitations | |
//license.SetLicense(u"Aspose.Cells.lic"); | |
// Instantiate a Workbook object that represents Excel file. | |
Workbook wb;//(u"MyBook_out.xlsx"); | |
// When you create a new workbook, a default "Sheet1" is added to the workbook. | |
Worksheet sheet = wb.GetWorksheets().Get(0); | |
// Access the "A1" cell in the sheet. | |
Cell cell = sheet.GetCells().Get(u"A1"); | |
U16String strRet = cell.GetStringValue(); | |
//check value | |
EXPECT_TRUE(strRet == u"Hello World!"); | |
// Save the Excel file. | |
wb.Save(u"MyBook_out.xlsx"); | |
} | |
catch (const CellsException& ex) | |
{ | |
std::cout << ex.GetErrorMessage().ToUtf8() << std::endl; | |
} | |
Aspose::Cells::Cleanup(); | |
} |