Carpetfizz
Carpetfizz

Reputation: 9149

Read cells from Excel in C++?

I was wondering how you can read specific cells from an Excel spreadsheet, in C++. I understand we have to use the "fstream" library, but I don't know exactly how I could get those values from a certain cell, and print it on the screen. Any help would be appreciated, thanks! Carpetfizz

Upvotes: 3

Views: 27390

Answers (3)

HumbleWebDev
HumbleWebDev

Reputation: 565

For writing, use:

https://sourceforge.net/projects/simplexlsx/

For reading, I use:

sourceforge.net/projects/xlsxio/?source=directory

Also, for xlsxio, I wrote an OO wrapper, to make it more friendly for c++ integration. It only supports reading, but you should probable use simplexlsx for writing anyways!

#include "XlsxBook.h"
#include "XlsxSheet.h"

https://drive.google.com/file/d/0B_HJu4VOsY8hMnRla2NMOEM3Z2M/view?usp=sharing

Upvotes: 0

0x90
0x90

Reputation: 40982

in linux you have this free: http://libxls.sourceforge.net/

in windows you have http://www.libxl.com/ which seems to cost money:

Book* book = xlCreateBook();
if(book)
{
    if(book->load(L"example.xls"))
    {
        Sheet* sheet = book->getSheet(0);
        if(sheet)
        {
            const wchar_t* s = sheet->readStr(2, 1);
            if(s) wcout << s << endl;

            double d = sheet->readNum(3, 1);
            cout << d << endl;
        }
    }

I think the best thing to do is to save the files as .csv which is more friendly to work with.

more references:

  1. What is a simple and reliable C library for working with Excel files?

  2. Reading from and writing to Excel files in C++

Upvotes: 3

user529758
user529758

Reputation:

Excel versions before Excel 2007 use a proprietary binary format, but Excel 2007 and later versions use XML (writes Wikipedia).

There's also a C++ library for dealing with Excel files.

Upvotes: 1

Related Questions