libxl readNum()在短时间内崩溃

时间:2016-02-16 03:22:12

标签: c++ excel parsing libxl

好。所以我使用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支​​持票是个好主意?

1 个答案:

答案 0 :(得分:1)

这不是崩溃。

official webpage of libxl告诉我们:

它会在每个电子表格的第一行写一个横幅,它只能读取100个单元格(第一行不可用)。购买许可证密钥以删除横幅和阅读限制