好的。所以我正在使用libXL来解析.xlsx文件。我遇到了一个奇怪的错误,因为当我调用sheet-> readNum(row,col)函数时,它可以完成其工作,但是仅执行45-47次。我检查了许多文件。检查.xls和.xlsx。完成多个调试会话。在〜45 readNums()之后,readNum()函数将返回0,而不是此处的实数。好的,这是我的代码:(不要看问题,而是为我公司的人力资源部做的)
#define _CRT_SECURE_NO_DEPRECATE
#include "libxl.h"
#include <iostream>
#include <stdio.h>
#include <string>
char dict[14] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N'};
FILE* file;
unsigned int find_index(char* c)
{
for (auto i = 0; i < 14; i++)
{
if (dict[i] == *c) return i;
}
return 0;
}
void writeData(std::string name, int currentNumber, int allNumberTime)
{
int hours, mins, secs;
hours = allNumberTime / 3600;
mins = (allNumberTime - hours * 3600) / 60;
secs = allNumberTime - hours * 3600 - mins * 60;
file = fopen("results.txt", "at");
fprintf(file, "%d\t%.2d:%.2d:%.2d\n", currentNumber, hours, mins, secs);
fclose(file);
}
int main(int argc, char* argv[])
{
//NO LIBXL LICENSE, SO GO FOR .TXT FILE
remove("results.txt");
printf("\t\t\tProgram do zestawien\n-------------------------------------------------------------------------------\n");
//Finding the column indexes
//Input file
std::wstring inputFile;
std::cout << "Podaj nazwe pliku wejsciowego (bez rozszerzenia, format .xlsx): ";
std::getline(std::wcin, inputFile);
inputFile += L".xls";
const wchar_t* inputF = inputFile.c_str();
char column;
auto c = &column;
std::cout << "Podaj nazwe kolumny z numerami: ";
std::cin >> column;
int numberColIndex = find_index(c);
std::cout << "Podaj nazwe kolumny z czasami polaczen: ";
std::cin >> column;
std::cout << "\n";
int timeColIndex = find_index(c);
//Opening xlsx file
//libxl::Book* book = xlCreateXMLBook();
libxl::Book* book = xlCreateBook();
//NO LIBXL LICENSE :(
//libxl::Book* results = xlCreateXMLBook();
//libxl::Format* HHMMSS = book->addFormat();
//HHMMSS->setNumFormat(libxl::NUMFORMAT_CUSTOM_HMMSS);
unsigned int currentNumber, numberCount = 1;
//Dates and times
int year, month, day, hour, min, sec;
int time, allNumberTime = 0;
if (book->load(inputF))
{
for (int i = 0; i < book->sheetCount(); i++)
{
libxl::Sheet* sheet = book->getSheet(i);
//NO LIBXL LICENSE :(
//libxl::Sheet* resultsSheet = results->addSheet(L"Sheet1");
if (sheet) //add && resultsSheet WHEN LICENSE
{
//printf("PIERWSZY NUMER: %.0f\n", sheet->readNum(0, numberColIndex));
currentNumber = sheet->readNum(0, numberColIndex);
for (int col = numberColIndex, row = 0; row < sheet->lastRow(); row++)
{
libxl::CellType cellType = sheet->cellType(row, col);
switch (cellType)
{
case libxl::CELLTYPE_NUMBER:
{
int readNumber=sheet->readNum(row, numberColIndex);
if (readNumber == currentNumber)
{
book->dateUnpack(sheet->readNum(row, col + timeColIndex - numberColIndex), &year, &month, &day, &hour, &min, &sec);
time = hour * 3600 + min * 60 + sec;
allNumberTime += time;
std::cout << "Przeczytalem wiersz nr: " << row << " w arkuszu nr: " << i << std::endl;
}
else
{
if (sheet->readNum(row, col) != NULL)
{
std::cout << "Przeczytalem wiersz nr: " << row << " w arkuszu nr: " << i << " - ZMIANA NUMERU" << std::endl;
std::cout << numberCount << ". " << "Podliczony czas dla numeru: " << currentNumber << " to " << allNumberTime << " sekund" << std::endl;
numberCount++;
//NO LIBXL LICENSE :(
//int resultsRow=0, resultsNumCol=0, resultsTimeCol=1;
//sheet->writeNum(resultsRow, resultsNumCol, currentNumber);
//sheet->writeNum(resultsRow, resultsTimeCol, book->datePack(year, month, day, hour, min, sec), HHMMSS);
//resultsRow++;
writeData("results.txt", currentNumber, allNumberTime);
currentNumber = sheet->readNum(row, numberColIndex);
allNumberTime = 0;
book->dateUnpack(sheet->readNum(row, col + timeColIndex - numberColIndex), &year, &month, &day, &hour, &min, &sec);
time = hour * 3600 + min * 60 + sec;
allNumberTime += time;
}
}
}
default: break;
}
}
//std::cout<<"Przeczytalem wiersz nr: "<<row<<" w arkuszu nr: "<<i<<std::endl;
std::cout << numberCount << ". " << "Podliczony czas dla numeru: " << currentNumber << " to " << allNumberTime << " sekund" << std::endl;
writeData("results.txt", currentNumber, allNumberTime);
allNumberTime = 0;
numberCount++;
}
}
}
book->release();
//NO LIBXL LICENSE :(
//results->save(L"results.xlsx");
std::cout << "\nWyniki zapisane w pliku results.txt - wystarczy zaimportowac do Excela" << std::endl;
fflush(stdin);
std::cin.get();
return 0;}
它做的一切都很完美,但只有像我说的那样-在开始时。是我的错,还是获得libXL支持的票是一个好主意?
最佳答案
这不是崩溃。
official webpage of libxl告诉:
它将在每个电子表格的第一行中写一个横幅,并且将只能读取100个单元格(第一行不可用)。购买许可证密钥以删除横幅和阅读限制
关于c++ - Libxl readNum()在短时间后崩溃,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35423091/