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
3 Answers
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:
-
Thanks! But, the code you gave me can be used without those programs? – Carpetfizz Nov 02 '12 at 18:25
-
1Ah, so you have to have those for these libraries to work.. the program is $200 haha. When you save them as a .csv, can you use standard read/write functions? – Carpetfizz Nov 02 '12 at 20:11
-
2Yes, that's why they're so popular. – ApplePie Nov 03 '12 at 11:26
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.
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

- 555
- 4
- 20