この例では、C++でExcelファイルを編集する方法を説明します。 MicrosoftWindowsとLinuxなどの両方で使用できるシンプルなAPIインターフェイスを使用してC++でプログラムでExcelファイルを編集できます。
C++でExcelファイルを編集する手順
- NuGetパッケージマネージャーツールを使用してAspose.Cells.Cppを追加します
- Aspose::Cells名前空間への参照を追加します
- Workbookオブジェクトのインスタンスを作成して、編集用のExcelファイルをロードします
- ワークシート内のセルA1とC1にアクセスし、それぞれデータと数式文字列を設定します
- ワークブックの式を計算します
- 数式を含む出力ワークブックをC++のXLSXに保存します
次の例は、ブックにアクセスし、いくつかのAPI呼び出しを使用してC++でExcelファイルを更新するために非常に迅速かつ簡単に使用されます。 C ++を使用すると、日付、パーセンテージ、数値、その他の値など、プログラムで任意のコンテンツデータをExcelセルに設定できます。
C++でExcelファイルを編集するためのコード
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
#pragma once | |
#include "Aspose.Cells.h" | |
class ExcelFileEditing | |
{ | |
void UpdateCellInExcel() | |
{ | |
// Add Aspose.Cells for C++ API License | |
intrusive_ptr<License> CellRenderingLicense = new License(); | |
CellRenderingLicense->SetLicense(new String("CPP.Aspose.Total.lic")); | |
// Instantiate the Workbook class object to load Excel file for editing | |
intrusive_ptr<IWorkbook> WbWithDataAndFormula = Factory::CreateIWorkbook(); | |
// Access cell A1 from a first worksheet to set data | |
intrusive_ptr <ICell> DataCell = WbWithDataAndFormula->GetIWorksheets()->GetObjectByIndex(0)-> | |
GetICells()->GetObjectByIndex(new String("A1")); | |
// Set some value in cell | |
DataCell->PutValue(100); | |
// Access cell C1 from first worksheet to update formula | |
intrusive_ptr <ICell> ForumulCell = WbWithDataAndFormula->GetIWorksheets()->GetObjectByIndex(0)-> | |
GetICells()->GetObjectByIndex(new String("C1")); | |
// Update the formula for selected cell | |
ForumulCell->SetFormula(new String("=Sum(A1,A20)")); | |
// Calculate the workbook after formula is updated | |
WbWithDataAndFormula->CalculateFormula(); | |
// Save the output workbook with formula to XLSX | |
WbWithDataAndFormula->Save(new String("WorkbookWithFormula.xlsx")); | |
} | |
}; |
- C ++でExcelファイルを編集する*このプロセス全体は、MicrosoftOfficeや相互運用機能に依存することなく実現されます。この時点で、ExcelファイルをPDFとして保存する場合でも、例C++を使用してExcelをPDFに変換する方法を調べることができます。