我正在使用Libxl for c ++来读取.xlsx文件。代码如下
void main()
{
Book* book = xlCreateXMLBook();
if (book) {
if(book->load("input.xlsx")) {
Sheet* sheet = book->getSheet(0);
if (sheet) {
long int id;
for(int i=1; i<15; i++) {
id = sheet->readNum(i,0);
cout << id << endl;
}
}
}
}
book->release();
}
excel表具有以下数据,输出似乎不同。
Original Data | Output from code
UNKNOWN | 0
47012 | 141366
48964 | 154840
425214 | 0
47018 | 134427
感谢任何帮助。
答案 0 :(得分:0)
readNum返回double而不是long int
我假设&#34; UNKNOWN&#34;是一个字符串。如果是这样,你可以这样做:
double id;
const wchar_t* str;
for(int i=1; i<15; i++) {
str = sheet->readStr(i, 0);
if (str) {
wcout << str << endl; // need to use wcout as it's a wide string
} else {
id = sheet->readNum(i,0);
cout << id << endl;
}
}
根据文档,如果单元格不包含字符串,readStr将返回NULL,因此您可以回退读取数字。