Пример #1
0
int main(int argc, char** argv)
{
    QGuiApplication(argc, argv);

    QXlsx::Document xlsx;
    xlsx.setColumn(1, 4, 20.0);

    QXlsx::Format header;
    header.setFontBold(true);
    header.setFontSize(20);

    //Custom number formats
    QStringList numFormats;
    numFormats<<"Qt #"
             <<"yyyy-mmm-dd"
            <<"$ #,##0.00"
           <<"[red]0.00";
    xlsx.write(1, 1, "Raw data", header);
    xlsx.write(1, 2, "Format", header);
    xlsx.write(1, 3, "Shown value", header);
    for (int i=0; i<numFormats.size(); ++i) {
        int row = i+2;
        xlsx.write(row, 1, 100.0);
        xlsx.write(row, 2, numFormats[i]);
        QXlsx::Format format;
        format.setNumberFormat(numFormats[i]);
        xlsx.write(row, 3, 100.0, format);
    }

    //Builtin number formats
    xlsx.addSheet();
    xlsx.setColumn(1, 4, 20.0);
    xlsx.write(1, 1, "Raw data", header);
    xlsx.write(1, 2, "Builtin Format", header);
    xlsx.write(1, 3, "Shown value", header);
    for (int i=0; i<50; ++i) {
        int row = i+2;
        int numFmt = i;
        xlsx.write(row, 1, 100.0);
        xlsx.write(row, 2, numFmt);
        QXlsx::Format format;
        format.setNumberFormatIndex(numFmt);
        xlsx.write(row, 3, 100.0, format);
    }

    xlsx.save();
    return 0;
}
Пример #2
0
void IoslotValueExporter::toExcel()
{
    QXlsx::Document xlsx;

    // Title
    QXlsx::Format format;
    format.setFontSize(18);
    format.setFontBold(true);
    format.setHorizontalAlignment(QXlsx::Format::AlignLeft);
    xlsx.write(XLSX_PROGRAM_NAME_ROW, XLSX_KEY_COL, tr("Hydroponic system configurator"), format);

    format.setFontSize(10);
    format.setFontBold(false);
    format.setHorizontalAlignment(QXlsx::Format::AlignLeft);
    xlsx.write(XLSX_VERSION_ROW, XLSX_KEY_COL, tr("Version"), format);
    format.setHorizontalAlignment(QXlsx::Format::AlignHCenter);
    xlsx.write(XLSX_VERSION_ROW, XLSX_VALUE_COL, tr("1.0"), format);

    format.setFontSize(12);
    format.setFontBold(true);
    format.setHorizontalAlignment(QXlsx::Format::AlignLeft);
    xlsx.write(XLSX_DEVICE_ADDRESS_ROW, XLSX_KEY_COL, tr("Address"), format);
    format.setHorizontalAlignment(QXlsx::Format::AlignHCenter);
    xlsx.write(XLSX_DEVICE_ADDRESS_ROW, XLSX_VALUE_COL, d_address, format);

    QVector<bool> valueEmpty(d_ioslotManager->ioslotCount(), false);

    // Table header
    format.setFontSize(12);
    format.setFontBold(true);
    format.setHorizontalAlignment(QXlsx::Format::AlignHCenter);
    format.setTopBorderStyle(QXlsx::Format::BorderThick);
    format.setBottomBorderStyle(QXlsx::Format::BorderThick);
    format.setLeftBorderStyle(QXlsx::Format::BorderThin);
    format.setRightBorderStyle(QXlsx::Format::BorderThin);

    int col = 1;
    xlsx.write(XLSX_HEADER_ROW, col, tr("Timestamp"), format);
    ++col;

    for (int num = 0; num < d_ioslotManager->ioslotCount(); ++num) {
        if (d_ioslotManager->ioslot(num)->type() == UnknownIoslotType) {
            valueEmpty[num] = true;
            continue;
        }

        const QString &name = d_ioslotManager->ioslot(num)->name();
        xlsx.write(XLSX_HEADER_ROW, col, name, format);
        ++col;
    }

    // Table content
    format.setFontSize(12);
    format.setFontBold(false);
    format.setHorizontalAlignment(QXlsx::Format::AlignHCenter);
    format.setTopBorderStyle(QXlsx::Format::BorderThin);
    format.setBottomBorderStyle(QXlsx::Format::BorderThin);
    format.setLeftBorderStyle(QXlsx::Format::BorderThin);
    format.setRightBorderStyle(QXlsx::Format::BorderThin);

    int count = d_table->recordCount(d_from, d_to);
    const int limit = 100;

    int row = 0;
    for (int offset = 0; offset < count; offset += limit) {
        QList<IoslotValueRecord> records = d_table->records(d_from, d_to, limit, offset);
        QList<IoslotValueRecord>::iterator it = records.begin();

        for (; it != records.end(); ++it, ++row) {
            IoslotValueRecord &record = *it;

            int col = 1;
            xlsx.write(XLSX_DATA_START_ROW + row, col,
                       record.timestamp().toString("yyyy-MM-dd hh:mm:ss.zzz"),
                       format);
            ++col;

            const QList<IoslotValueRecord::RecordValue> &values = record.values();
            QList<IoslotValueRecord::RecordValue>::const_iterator it2 = values.begin();

            for (int num = 0; it2 != values.end(); ++it2, ++num) {
                if (valueEmpty[num]) continue;

                const IoslotValueRecord::RecordValue &value = *it2;
                xlsx.write(XLSX_DATA_START_ROW + row, col, value.second, format);
                ++col;
            }
        }

        Q_EMIT progress(offset, count);
    }

    (void)xlsx.saveAs(d_filename);
}