好的。所以我使用 libXL 来解析 .xlsx 文件。我遇到了一个奇怪的错误,因为当我调用 sheet->readNum(row, col) 函数时,它完成了它的工作,但只有 45-47 次。我检查了很多文件。检查了 .xls 和 .xlsx。完成了多个调试会话。在 ~45 readNums() 之后,readNum() 函数返回 0,而不是这里的实数。好的,这是我的代码:(不要看 couts,它是为我公司的人力资源部做的)
#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 支持的票是个好主意?