0

I've got a language phrase translation table in Excel.
I would like to write, in C++, a program that creates C++ source data tables from the Excel Spreadsheet.

Is there are recommended library for this?

I'll be using Visual Studio 2019.
I'm using Microsoft Excel for Microsoft 365 MSO (16.0.13127.21668) 64-bit.

Preference is freeware. The application I'm writing will be an inhouse tool and not inside any product.

2 Answers 2

1

I have not worked with excel files from C++, specifically, but github appears to have these 3 projects that are each quite popular.


OpenXLSX (The readme specifically metnions that it works with VS 2019 using the cmake extension)

Many programming languages have the ability to modify Excel files, either natively or in the form of open source libraries. This includes Python, Java and C#. For C++, however, things are more scattered. While there are some libraries, they are generally less mature and have a smaller feature set than for other languages.

Because there are no open source library that fully fitted my needs, I decided to develop the OpenXLSX library.


xlnt appears to be the C++ Excel library with the most stars on github. It appears that its syntax closely matches Excel's own. Its feature list indicates that it supports a standard spreadsheet made up of simple column/row data, but I also see it specifically does not support "Tables", so you might want to double check if this one is going to work for you before diving in.


readxl claims to offer a simplicity which they suggest matches this cheatsheet from rstudio: dataimport1 dataimport2


If you're curious, I found these using a wonderful new(to me) tool https://www.awesomeopensource.com that makes finding the gems hidden within github a breeze with its simple category based listings ranked by stars. I know that sounds like something easily done within github itself, but I implore you to give it a try and see the difference!

0

Spire.XLS for C++ library can be used to read Excel files in C++ applications. You can install the library through NuGet:

PM> Install-Package Spire.XLS.Cpp

After installation, you can refer to the following code to read an Excel file:

#include "Spire.Xls.o.h";
#include <locale>
#include <codecvt>

using namespace Spire::Xls;
using namespace Spire::Common;
using namespace std;

int main()
{
    //Create a Workbook instance
    Workbook* workbook = new Workbook();
    //Load an Excel file
    workbook->LoadFromFile(L"Sample1.xlsx");

    //Get the first worksheet
    Worksheet* sheet = workbook->GetWorksheets()->Get(0);

    //Get the cell collection of the worksheet
    IList<XlsRange>* cellCollection = sheet->GetCells();

    //Create a wstring object to append cell values
    wstring buffer = L"";

    //Iterate through all the cells in the collection
    for (int i = 0; i < cellCollection->GetCount(); i++)
    {
        XlsRange* cr = cellCollection->GetItem(i);
        //Get cell addresses and values
        wstring cell = cr->GetRangeAddressLocal();
        wstring result = L"Cell: " + cell + L"   Value: " + cr->GetValue();
        buffer += (result + L"\n");
    }

    //Save to a TXT file
    wofstream write(L"CellValues.txt");
    auto LocUtf8 = locale(locale(""), new std::codecvt_utf8<wchar_t>);
    write.imbue(LocUtf8);
    write << buffer;
    write.close();
    workbook->Dispose();
    delete workbook;
}

Note: I work for the company that developed this library.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.