dbzhang800 / QtXlsxWriter

.xlsx file reader and writer for Qt5
http://qtxlsx.debao.me
Other
1.24k stars 632 forks source link

Please add a function to read from the file! #5

Closed Tamonius closed 11 years ago

Tamonius commented 11 years ago

Please add a function to read from the file! xlsx.read ("A1"); well, or like =)

dbzhang800 commented 11 years ago

Hi, thanks for your feedback. New member function QXlsx::Document::cellAt(const QString &) has just been added in the dev branch.

Tamonius commented 11 years ago

thank you so much for an added feature. how to use it?

dbzhang800 commented 11 years ago

Hi, the code will look like:

QXlsx::Document xlsx;
xlsx.write("A1", "Hello Qt!");
xlsx.write("A2", 12345);
qDebug()<<xlsx.cellAt("A2")->value();

More work is still needed, take string type for example, Cell::value() returns the string index in the sharedStringTable instead of the string itself at present.

Tamonius commented 11 years ago
QXlsx::Document xlsx;
xlsx.write("A1", "Hello Qt!");
xlsx.write("A2", 12345);
xlsx.write("A3", "54321");
xlsx.saveAs("Test.xlsx");
QXlsx::Document xlsx2("Test.xlsx");
qDebug()<<xlsx2.cellAt("A1")->value(); //QVariant(QString, "0")
qDebug()<<xlsx2.cellAt("A2")->value(); //QVariant(QString, "12345")
qDebug()<<xlsx2.cellAt("A3")->value(); //QVariant(QString, "1")

and how to get the text from cell A1, A3?

dbzhang800 commented 11 years ago

Hi, this have been fixed in the dev branch, you can given a try to it. So xlsx2.cellAt("A1")->value() should works for string type.

Tamonius commented 11 years ago

Thank you very much for the work done. Found another bug when trying to read

xlsx.cellAt(1,1)->value(); 

application error. =)

dbzhang800 commented 11 years ago

Hi, cellAt(1,1) is used to access "B2", if you want to access "A1", cellAt(0, 0) or cellAt("A1") should be used. For non-exist cell, cellAt() returns a NULL pointer.

Tamonius commented 11 years ago

How about determining the end of the file, that is, to read a file using while

dbzhang800 commented 11 years ago

Ok, firstRow/firstColumn/lastRow/lastColumn are added to the Worksheet class in the dev branch now. You can give a try to it.

Tamonius commented 11 years ago

I solved the problem, I do not know how well, but: read in the QVector arr2;

    fileName = QFileDialog::getOpenFileName(this, tr("Open File"),
                                                     "/home",
                                                     tr("Exel (*.xlsx)"));
    QXlsx::Document xlsx(fileName);
    int i = 1;
    while (xlsx.currentWorksheet()) {
        QString cel = QString("A%1").arg(i);
        if(xlsx.cellAt(cel) == 0x0)
            break;
        arr2.append(xlsx.cellAt(cel)->value().toString());
        i++;
    }
    //qDebug()<<arr2;
    this->writeFiles();

write

    QXlsx::Document xlsx;
    int a = 1;
    for(int i = 0;i<arr2.count();i++){
        QString A = QString("A%1").arg(a);
        QString B = QString("B%1").arg(a);
        xlsx.write(A,arr2.at(i));
        xlsx.write(B,"ok!");
        qDebug()<<A<<" "<<arr2.at(i);
        //qDebug()<<B;
        a++;
    }
    xlsx.saveAs(fileName);

=)

Tamonius commented 11 years ago

What about the Russian language? windows-1251 The output is:??? ????? ?????????

dbzhang800 commented 11 years ago

Hi, if your code satisfies your need, it's ok to use. But you should know that, your code doesn't work in normal cases, such as for one xlsx file which contains data only in "A2" and "A4", or only in "D4".

For non-ascii support, I will try to find the reason when I have time. Maybe you can close this issue and create a new one.