问题
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
回答1:
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:
What is a simple and reliable C library for working with Excel files?
Reading from and writing to Excel files in C++
回答2:
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.
回答3:
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
来源:https://stackoverflow.com/questions/13200809/read-cells-from-excel-in-c