数式と関数は Microsoft Excel の基本機能であり、ユーザーはデータに対してさまざまな数学的計算、統計的および論理的操作を実行できます。 数式は自動計算のためにセルに入力できる式であり、通常はセル参照、定数、および実行する計算を指定する演算子で構成されます。 一方、関数は、セル範囲の合計、平均、最大値、最小値の計算など、特定のタスクを実行する事前に構築された数式です。 Excel を使用してデータを処理するユーザーにとって、数式と関数は不可欠なツールです。 財務データ、実験データ、その他のデータセットを分析する場合でも、数式と関数を使用すると、データを計算し、結果を迅速かつ正確に分析することができます。この記事では、Spire.XLS for C++ を使用して、Excel ファイルで数式と関数を挿入または読み取る方法について説明します。 Excel に数式と関数を挿入する Excel で数式と関数を読み込む Spire.XLS for C++ をインストールします Spire.XLS for C++ をアプリケーションに組み込むには、2つの方法があります。一つは NuGet 経由でインストールする方法、もう一つは当社のウェブサイトからパッケージをダウンロードし、ライブラリをプログラムにコピーする方法です。NuGet 経由のインストールの方が便利で、より推奨されます。詳しくは、以下のリンクからご覧いただけます。 Spire.XLS for C++ を C++ アプリケーションに統合する方法 Excel に数式と関数を挿入する Spire.XLS for C++ の Worksheet->GetRange(int row, int column)->SetFormula(LPCWSTR_S value) メソッドは、ワークシートの特定のセルに数式または関数を追加するために使用されます。主な手順は次のとおりです。 Workbook クラスのインスタンスを初期化します。 Workbook->GetWorksheets()->Get(int index) メソッドを使用して、インデックスによって特定のワークシートを取得します。 Worksheet->GetRange(int row, int column)->SetText(LPCWSTR_S value) メソッドと Worksheet->GetRange(int row, int column)->SetNumberValue(double value) メソッドを使用して、特定のセルにテキストとデータを追加します。 Worksheet->GetRange(int row, int column)->SetText(LPCWSTR_S value) メソッドと Worksheet->GetRange(int row, int column)->SetFormula(LPCWSTR_S value) メソッドを使用して、特定のセルにテキストと数式を追加します。 Worksheet->GetRange(int row, int column)->SetText(LPCWSTR_S value) メソッドと Worksheet->GetRange(int row, int column)->SetFormula(LPCWSTR_S value) メソッドを使用して、特定のセルにテキストと関数を追加します。 Workbook->SaveToFile(LPCWSTR_S fileName, ExcelVersion version) メソッドを使用して結果ファイルを保存します。 C++ #include "Spire.Xls.o.h"; using namespace Spire::Xls; using namespace std; int main() { //Workbookクラスのインスタンスを初期化する intrusive_ptr<Workbook> workbook = new Workbook(); //最初のワークシートを取得する intrusive_ptr<Worksheet> sheet = dynamic_pointer_cast<Worksheet>(workbook->GetWorksheets()->Get(0)); //2つの変数を宣言:currentRow、currentFormula int currentRow = 1; wstring currentFormula = L""; //ワークシートにテキストを追加してセルのスタイルを設定する sheet->GetRange(currentRow, 1)->SetText(L"データ:"); sheet->GetRange(currentRow, 1)->GetStyle()->GetFont()->SetIsBold(true); sheet->GetRange(currentRow, 1)->GetStyle()->SetFillPattern(ExcelPatternType::Solid); sheet->GetRange(currentRow, 1)->GetStyle()->SetKnownColor(ExcelColors::LightGreen1); sheet->GetRange(currentRow, 1)->GetStyle()->GetBorders()->Get(BordersLineType::EdgeBottom)->SetLineStyle(LineStyleType::Medium); //ワークシートにデータを追加する sheet->GetRange(++currentRow, 1)->SetNumberValue(7.3); sheet->GetRange(currentRow, 2)->SetNumberValue(5); sheet->GetRange(currentRow, 3)->SetNumberValue(8.2); sheet->GetRange(currentRow, 4)->SetNumberValue(4); sheet->GetRange(currentRow, 5)->SetNumberValue(3); sheet->GetRange(currentRow, 6)->SetNumberValue(11.3); currentRow++; //ワークシートにテキストを追加してセルのスタイルを設定する sheet->GetRange(++currentRow, 1)->SetText(L"数式"); sheet->GetRange(currentRow, 2)->SetText(L"結果"); sheet->GetRange(currentRow, 1, currentRow, 2)->GetStyle()->GetFont()->SetIsBold(true); sheet->GetRange(currentRow, 1, currentRow, 2)->GetStyle()->SetKnownColor(ExcelColors::LightGreen1); sheet->GetRange(currentRow, 1, currentRow, 2)->GetStyle()->SetFillPattern(ExcelPatternType::Solid); sheet->GetRange(currentRow, 1, currentRow, 2)->GetStyle()->GetBorders()->Get(BordersLineType::EdgeBottom)->SetLineStyle(LineStyleType::Medium); //ワークシートにテキストと数式を追加する currentFormula = (L"="Hello""); sheet->GetRange(++currentRow, 1)->SetText((L"'" + currentFormula).c_str()); sheet->GetRange(currentRow, 2)->SetFormula(currentFormula.c_str()); currentFormula = (L"=300"); sheet->GetRange(++currentRow, 1)->SetText((L"'" + currentFormula).c_str()); sheet->GetRange(currentRow, 2)->SetFormula(currentFormula.c_str()); currentFormula = (L"=3389.639421"); sheet->GetRange(++currentRow, 1)->SetText((L"'" + currentFormula).c_str()); sheet->GetRange(currentRow, 2)->SetFormula(currentFormula.c_str()); currentFormula = (L"=false"); sheet->GetRange(++currentRow, 1)->SetText((L"'" + currentFormula).c_str()); sheet->GetRange(currentRow, 2)->SetFormula(currentFormula.c_str()); currentFormula = (L"=1+2+3+4+5-6-7+8-9"); sheet->GetRange(++currentRow, 1)->SetText((L"'" + currentFormula).c_str()); sheet->GetRange(currentRow, 2)->SetFormula(currentFormula.c_str()); currentFormula = (L"=33*3/4-2+10"); sheet->GetRange(++currentRow, 1)->SetText((L"'" + currentFormula).c_str()); sheet->GetRange(currentRow, 2)->SetFormula(currentFormula.c_str()); currentFormula = (L"=Sheet1!$B$2"); sheet->GetRange(++currentRow, 1)->SetText((L"'" + currentFormula).c_str()); sheet->GetRange(currentRow, 2)->SetFormula(currentFormula.c_str()); //ワークシートにテキストと関数を追加する //AVERAGE currentFormula = (L"=AVERAGE(Sheet1!$D$2:F$2)"); sheet->GetRange(++currentRow, 1)->SetText((L"'" + currentFormula).c_str()); sheet->GetRange(currentRow, 2)->SetFormula(currentFormula.c_str()); //COUNT currentFormula = (L"=COUNT(3,5,8,10,2,34)"); sheet->GetRange(++currentRow, 1)->SetText((L"'" + currentFormula).c_str()); sheet->GetRange(currentRow, 2)->SetFormula(currentFormula.c_str()); //NOW currentFormula = (L"=NOW()"); sheet->GetRange(++currentRow, 1)->SetText((L"'" + currentFormula).c_str()); sheet->GetRange(currentRow, 2)->SetFormula(currentFormula.c_str()); sheet->GetRange(currentRow, 2)->GetStyle()->SetNumberFormat(L"yyyy-MM-DD"); //SECOND currentFormula = (L"=SECOND(0.503)"); sheet->GetRange(++currentRow, 1)->SetText((L"'" + currentFormula).c_str()); sheet->GetRange(currentRow++, 2)->SetFormula(currentFormula.c_str()); //MINUTE currentFormula = (L"=MINUTE(0.78125)"); sheet->GetRange(currentRow, 1)->SetText((L"'" + currentFormula).c_str()); sheet->GetRange(currentRow++, 2)->SetFormula(currentFormula.c_str()); //MONTH currentFormula = (L"=MONTH(9)"); sheet->GetRange(currentRow, 1)->SetText((L"'" + currentFormula).c_str()); sheet->GetRange(currentRow++, 2)->SetFormula(currentFormula.c_str()); //DAY currentFormula = (L"=DAY(10)"); sheet->GetRange(currentRow, 1)->SetText((L"'" + currentFormula).c_str()); sheet->GetRange(currentRow++, 2)->SetFormula(currentFormula.c_str()); //TIME currentFormula = (L"=TIME(4,5,7)"); sheet->GetRange(currentRow, 1)->SetText((L"'" + currentFormula).c_str()); sheet->GetRange(currentRow++, 2)->SetFormula(currentFormula.c_str()); //DATE currentFormula = (L"=DATE(6,4,2)"); sheet->GetRange(currentRow, 1)->SetText((L"'" + currentFormula).c_str()); sheet->GetRange(currentRow++, 2)->SetFormula(currentFormula.c_str()); //RAND currentFormula = (L"=RAND()"); sheet->GetRange(currentRow, 1)->SetText((L"'" + currentFormula).c_str()); sheet->GetRange(currentRow++, 2)->SetFormula(currentFormula.c_str()); //HOUR currentFormula = (L"=HOUR(0.5)"); sheet->GetRange(currentRow, 1)->SetText((L"'" + currentFormula).c_str()); sheet->GetRange(currentRow++, 2)->SetFormula(currentFormula.c_str()); //MOD currentFormula = (L"=MOD(5,3)"); sheet->GetRange(currentRow, 1)->SetText((L"'" + currentFormula).c_str()); sheet->GetRange(currentRow++, 2)->SetFormula(currentFormula.c_str()); //WEEKDAY currentFormula = (L"=WEEKDAY(3)"); sheet->GetRange(currentRow, 1)->SetText((L"'" + currentFormula).c_str()); sheet->GetRange(currentRow++, 2)->SetFormula(currentFormula.c_str()); //YEAR currentFormula = (L"=YEAR(23)"); sheet->GetRange(currentRow, 1)->SetText((L"'" + currentFormula).c_str()); sheet->GetRange(currentRow++, 2)->SetFormula(currentFormula.c_str()); //NOT currentFormula = (L"=NOT(true)"); sheet->GetRange(currentRow, 1)->SetText((L"'" + currentFormula).c_str()); sheet->GetRange(currentRow++, 2)->SetFormula(currentFormula.c_str()); //OR currentFormula = (L"=OR(true)"); sheet->GetRange(currentRow, 1)->SetText((L"'" + currentFormula).c_str()); sheet->GetRange(currentRow++, 2)->SetFormula(currentFormula.c_str()); //AND currentFormula = (L"=AND(TRUE)"); sheet->GetRange(currentRow, 1)->SetText((L"'" + currentFormula).c_str()); sheet->GetRange(currentRow++, 2)->SetFormula(currentFormula.c_str()); //VALUE currentFormula = (L"=VALUE(30)"); sheet->GetRange(currentRow, 1)->SetText((L"'" + currentFormula).c_str()); sheet->GetRange(currentRow++, 2)->SetFormula(currentFormula.c_str()); //LEN currentFormula = (L"=LEN("world")"); sheet->GetRange(currentRow, 1)->SetText((L"'" + currentFormula).c_str()); sheet->GetRange(currentRow++, 2)->SetFormula(currentFormula.c_str()); //MID currentFormula = (L"=MID("world",4,2)"); sheet->GetRange(currentRow, 1)->SetText((L"'" + currentFormula).c_str()); sheet->GetRange(currentRow++, 2)->SetFormula(currentFormula.c_str()); //ROUND currentFormula = (L"=ROUND(7,3)"); sheet->GetRange(currentRow, 1)->SetText((L"'" + currentFormula).c_str()); sheet->GetRange(currentRow++, 2)->SetFormula(currentFormula.c_str()); //SIGN currentFormula = (L"=SIGN(4)"); sheet->GetRange(currentRow, 1)->SetText((L"'" + currentFormula).c_str()); sheet->GetRange(currentRow++, 2)->SetFormula(currentFormula.c_str()); //INT currentFormula = (L"=INT(200)"); sheet->GetRange(currentRow, 1)->SetText((L"'" + currentFormula).c_str()); sheet->GetRange(currentRow++, 2)->SetFormula(currentFormula.c_str()); //ABS currentFormula = (L"=ABS(-1.21)"); sheet->GetRange(currentRow, 1)->SetText((L"'" + currentFormula).c_str()); sheet->GetRange(currentRow++, 2)->SetFormula(currentFormula.c_str()); //LN currentFormula = (L"=LN(15)"); sheet->GetRange(currentRow, 1)->SetText((L"'" + currentFormula).c_str()); sheet->GetRange(currentRow++, 2)->SetFormula(currentFormula.c_str()); //EXP currentFormula = (L"=EXP(20)"); sheet->GetRange(currentRow, 1)->SetText((L"'" + currentFormula).c_str()); sheet->GetRange(currentRow++, 2)->SetFormula(currentFormula.c_str()); //SQRT currentFormula = (L"=SQRT(40)"); sheet->GetRange(currentRow, 1)->SetText((L"'" + currentFormula).c_str()); sheet->GetRange(currentRow++, 2)->SetFormula(currentFormula.c_str()); //PI currentFormula = (L"=PI()"); sheet->GetRange(currentRow, 1)->SetText((L"'" + currentFormula).c_str()); sheet->GetRange(currentRow++, 2)->SetFormula(currentFormula.c_str()); //COS currentFormula = (L"=COS(9)"); sheet->GetRange(currentRow, 1)->SetText((L"'" + currentFormula).c_str()); sheet->GetRange(currentRow++, 2)->SetFormula(currentFormula.c_str()); //SIN currentFormula = (L"=SIN(45)"); sheet->GetRange(currentRow, 1)->SetText((L"'" + currentFormula).c_str()); sheet->GetRange(currentRow++, 2)->SetFormula(currentFormula.c_str()); //MAX currentFormula = (L"=MAX(10,30)"); sheet->GetRange(currentRow, 1)->SetText((L"'" + currentFormula).c_str()); sheet->GetRange(currentRow++, 2)->SetFormula(currentFormula.c_str()); //MIN currentFormula = (L"=MIN(5,7)"); sheet->GetRange(currentRow, 1)->SetText((L"'" + currentFormula).c_str()); sheet->GetRange(currentRow++, 2)->SetFormula(currentFormula.c_str()); //AVERAGE currentFormula = (L"=AVERAGE(12,45)"); sheet->GetRange(currentRow, 1)->SetText((L"'" + currentFormula).c_str()); sheet->GetRange(currentRow++, 2)->SetFormula(currentFormula.c_str()); //SUM currentFormula = (L"=SUM(18,29)"); sheet->GetRange(currentRow, 1)->SetText((L"'" + currentFormula).c_str()); sheet->GetRange(currentRow++, 2)->SetFormula(currentFormula.c_str()); //IF currentFormula = (L"=IF(4,2,2)"); sheet->GetRange(currentRow, 1)->SetText((L"'" + currentFormula).c_str()); sheet->GetRange(currentRow++, 2)->SetFormula(currentFormula.c_str()); //SUBTOTAL currentFormula = (L"=SUBTOTAL(3,Sheet1!A2:F2)"); sheet->GetRange(currentRow, 1)->SetText((L"'" + currentFormula).c_str()); sheet->GetRange(currentRow++, 2)->SetFormula(currentFormula.c_str()); //Set width of the 1st, 2nd and 3rd columns sheet->SetColumnWidth(1, 32); sheet->SetColumnWidth(2, 16); sheet->SetColumnWidth(3, 16); //セルのスタイルの作成 intrusive_ptr<CellStyle> style = workbook->GetStyles()->Add(L"Style"); //水平方向の配置を左側に設定する style->SetHorizontalAlignment(HorizontalAlignType::Left); //ワークシートにスタイルを適用する sheet->ApplyStyle(style); //結果ファイルを保存する workbook->SaveToFile(L"InsertFormulasAndFunctions.xlsx", ExcelVersion::Version2016); workbook->Dispose(); } Excel で数式と関数を読み込む Excel ワークシートの数式や関数を読み取るには、シート内のすべてのセルをループします。その後、Cell->GetHasFormula() メソッドを使用して数式または関数のあるセルを見つけ、CellRange->GetFormula() メソッドを使用してセルの数式や関数を取得します。具体的な手順は以下の通りです。 Workbook クラスのインスタンスを初期化します。 Workbook->LoadFromFile() メソッドを使用して Excel ファイルを読み込みます。 Workbook->GetWorksheets()->Get(int index) メソッドを使用して、インデックスによって特定のワークシートを取得します。 Worksheet->GetAllocatedRange() メソッドを使用して、シートで使用されているセル範囲を取得します。 wstring 変数を宣言します。 範囲内のすべてのセルをループします。 Cell->GetHasFormula() メソッドを使用して、数式と関数を含むセルを見つけます。 CellRange->GetRangeAddressLocal() と CellRange->GetFormula() メソッドを使用して、セルの名と数式/関数を取得します。 セルの名と数式/関数を wstring 変数に追加します。 wstring 変数の内容を .txt ファイルに書き込みます。 C++ #include "Spire.Xls.o.h"; #include <locale> #include <codecvt> using namespace Spire::Xls; using namespace std; int main() { //Workbookクラスのインスタンスを初期化する intrusive_ptr<Workbook>workbook = new Workbook(); //Excelファイルをロードする workbook->LoadFromFile(L"InsertFormulasAndFunctions.xlsx"); //最初のワークシートを取得する intrusive_ptr<Worksheet> sheet = dynamic_pointer_cast<Worksheet>(workbook->GetWorksheets()->Get(0)); //ワークシートの特定の範囲にアクセスする intrusive_ptr <IXLSRange>usedRange = sheet->GetAllocatedRange(); //wstring変数を宣言する wstring buffer = L""; //範囲内のすべてのセルをループする for (int i = 0; i < usedRange->GetCells()->GetCount(); i++) { intrusive_ptr < CellRange> cell = usedRange->GetCells()->GetItem(i); //セルに数式または関数があるかどうかをチェックする if (cell->GetHasFormula()) { //セルの名を取得する wstring cellName = cell->GetRangeAddressLocal(); //数式または関数を取得する wstring formula = cell->GetFormula(); //セルの名と数式/関数をwstring変数に追加 buffer += ((cellName + L"セルに数式を含める: " + formula + L"n").c_str()); } } //wstring変数の内容を.txtファイルに書き込む wofstream write(L"ReadFormulasAndFunctions.txt"); auto LocUtf8 = locale(locale(""), new std::codecvt_utf8<wchar_t>); write.imbue(LocUtf8); write << buffer; write.close(); workbook->Dispose(); } 一時ライセンスを申請する 結果ドキュメントから評価メッセージを削除したい場合、または機能制限を取り除く場合は、についてこのメールアドレスはスパムボットから保護されています。閲覧するにはJavaScriptを有効にする必要があります。 にお問い合わせ、30 日間有効な一時ライセンスを取得してください。