kumasysjp

xlsx(Excelファイル) 操作するライブラリOpenXLSX を使ったプログラム作成のためのひながた( Windows & Linux)

OpenXLSX は Kenneth Troldal 氏があらわしたC/C++ ライブラリである。

Windows プログラムの場合, 有名な NPOI (POI .NET版)や, .NETリポジトリNuget にある ClosedXML の選択肢がまづ考えられる.

一方で, この OpenXLSX は マイナー で  今のところ wrap製品がないが,   便利である.

(なお同名のopenxlsx という Rライブラリがあるが, こちらは Kenneth氏のソフトの wrap ではなく, 全く別物のようだ。)

それはともかく, Windows,  Linux 両方に対応するやうなプログラム環境に於いて Excel を用いる場合,  こういったC/C++ で書かれたシンプルなのが便利な気がした。

目的

Excelファイル(xlsx)を操作する Windows, Linux 両対応のプログラム作成のための ひながた

動作環境

ホストOS側 :   Windows 11

ゲストOS側:     Debian 12.6

VMクライアント:  VirtualBox 6.1

VSCode 1.100.2 (Linux 用のビルド・デバッグの場合)

Visual Studio 2022  (Windows 用のビルド・デバッグの場合)

ひな  サンプル内容

OpenXlsUser
OpenXls を同梱しています。

LinuxUniq.cpp


#include "build.h"
#ifdef Linux
#endif

 

 

WindowsUniq.cpp


#include "build.h"
#ifdef Linux
#endif
 

 

build.h


#include 

#ifdef __x86_64
#define x64
#endif

#ifdef Windows
#else
#define Linux

#endif
 

 

OpenXlsUser.cpp


#include 
#include 
#include 
#include "build.h"
using namespace std;
using namespace OpenXLSX;

int main()
{
    cout << "********************************************************************************\n";
    cout << "DEMO PROGRAM #01: Basic Usage\n";
    cout << "********************************************************************************\n";

    XLDocument doc;
    doc.create("./Demo01.xlsx");
    auto wks = doc.workbook().worksheet("Sheet1");

    wks.cell("A1").value() = 3.14159265358979323846;
    wks.cell("B1").value() = 42;
    wks.cell("C1").value() = "  Hello OpenXLSX!  ";
    wks.cell("D1").value() = true;
    wks.cell("E1").value() = std::sqrt(-2); 

    XLCellValue A1 = wks.cell("A1").value();
    XLCellValue B1 = wks.cell("B1").value();
    XLCellValue C1 = wks.cell("C1").value();
    XLCellValue D1 = wks.cell("D1").value();
    XLCellValue E1 = wks.cell("E1").value();

    double vA1 = wks.cell("A1").value();
    int vB1 = wks.cell("B1").value();
    std::string vC1 = wks.cell("C1").value();
    bool vD1 = wks.cell("D1").value();
    double vE1 = wks.cell("E1").value();

    cout << "Cell A1: (" << A1.typeAsString() << ") " << vA1 << endl;
    cout << "Cell B1: (" << B1.typeAsString() << ") " << vB1 << endl;
    cout << "Cell C1: (" << C1.typeAsString() << ") " << vC1 << endl;
    cout << "Cell D1: (" << D1.typeAsString() << ") " << vD1 << endl;
    cout << "Cell E1: (" << E1.typeAsString() << ") " << vE1 << endl << endl;

    cout << "Cell A1: (" << A1.typeAsString() << ") " << A1.get() << endl;
    cout << "Cell B1: (" << B1.typeAsString() << ") " << B1.get() << endl;
    cout << "Cell C1: (" << C1.typeAsString() << ") " << C1.get() << endl;
    cout << "Cell D1: (" << D1.typeAsString() << ") " << D1.get() << endl;
    cout << "Cell E1: (" << E1.typeAsString() << ") " << E1.get() << endl << endl;

    wks.cell("F1").value() = wks.cell(XLCellReference("C1")).value();
    XLCellValue F1 = wks.cell("F1").value();
    cout << "Cell F1: (" << F1.typeAsString() << ") " << F1.get() << endl << endl;

    std::tm tm;
    tm.tm_year = 121;
    tm.tm_mon = 8;
    tm.tm_mday = 1;
    tm.tm_hour = 12;
    tm.tm_min = 0;
    tm.tm_sec = 0;
    XLDateTime dt (tm);

    wks.cell("G1").value() = dt;

    XLCellValue G1 = wks.cell("G1").value();
    cout << "Cell G1: (" << G1.typeAsString() << ") " << G1.get() << endl;

    auto result = G1.get();

    cout << "Cell G1: (" << G1.typeAsString() << ") " << result.serial() << endl;
    auto tmo = result.tm();
    cout << "Cell G1: (" << G1.typeAsString() << ") " << std::asctime(&tmo);

    doc.save();
    doc.close();

    return 0;
}