int main() { QXlsx::Document xlsx; xlsx.write(0, 2, "Row:0, Col:2 ==> (C1)"); //Set the height of the first row to 50.0(points) xlsx.setRow(0, 50.0); //Set the width of the third column to 40.0(chars) xlsx.setColumn(2, 3, 40.0); //Set style for the row 11th. QXlsx::Format *format1 = xlsx.createFormat(); format1->setFontBold(true); format1->setFontColor(QColor(Qt::blue)); format1->setFontSize(20); xlsx.write(10, 0, "Hello Row Style"); xlsx.write(10, 5, "Blue Color"); xlsx.setRow(10, 40, format1); //Set style for the col [9th, 16th) QXlsx::Format *format2 = xlsx.createFormat(); format2->setFontBold(true); format2->setFontColor(QColor(Qt::magenta)); for (int row=11; row<30; row++) for (int col=8; col<15; col++) xlsx.write(row, col, row+col); xlsx.setColumn(8, 15, 5.0, format2); xlsx.save(); return 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; }
/* * 导出计算结果 */ void MainWindow::on_dataDump_pushButton_clicked() { this->on_oneResult_pushButton_clicked (); //QString path = QFileDialog::getExistingDirectory(this, tr("Open Directory"),"/home",QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks); QString fileName = QFileDialog::getSaveFileName(this, tr("设置导出文件"), "", tr("*.xlsx")); if( fileName !="") { //QString fileName = path + "/计算所得指数.xlsx"; QXlsx::Document *xlsx = new QXlsx::Document(fileName); int rowC = (modelST->rowCount ()); int columnC = (modelST->columnCount ()); xlsx->write (1,2, "年份季度"); xlsx->write (1,3, "计算所得指标"); xlsx->write (1,4, "指标说明"); for (int i = 0; i < rowC; ++i) { for (int j = 0; j < columnC; ++j) { xlsx->write ((i+2),(j+1), modelST->item (i, j)->text ()); } } if (!fileName.isEmpty()) { if(xlsx->save()) QMessageBox::warning (this,"Correct","数据导出成功"); else QMessageBox::warning (this,"Error","数据导出失败"); } } }
void MainWindow::checkExcelFile(QString file_name) { bool debug = false; QXlsx::Document * xlsx = new QXlsx::Document(file_name); QSqlQuery query; if (!xlsx->selectSheet("Check")) { return; } QXlsx::CellRange range = xlsx->dimension(); int rowCount = range.rowCount(); // int columnCount = range.columnCount(); int columnCount = visible_columns.at(0); /* if (check_box_check->isChecked() && columnCount != visible_columns.at(0)) { QMessageBox::information(this,tr(""), tr("Check columns do not fit database")); } */ if (debug) { QMessageBox::information(this,tr("Rows * Columns"), QString::number(rowCount) + tr(" * ") + QString::number(columnCount)); } int newCount = 0; int insertCount = 0; for (int i = 1; i <= rowCount; ++i) { QString name = xlsx->read(i, 1).toString(); if (debug) { QMessageBox::information(this,tr("name"), name); } SearchResult result = searchCompany(name); if (debug) { QMessageBox::information(this,tr("Search result: "), result.information); } if (result.color != Qt::white) { for (int j = 1; j <= columnCount; ++j) { QVariant value = xlsx->read(i, j); if (!value.isValid()) { continue; } QXlsx::Format format = xlsx->cellAt(i, j)->format(); format.setPatternBackgroundColor(result.color); xlsx->write(i, j, value, format); } // Department; xlsx->write(i, columnCount + 1, result.department); QXlsx::Format format = xlsx->cellAt(i, columnCount + 1)->format(); format.setPatternBackgroundColor(result.color); xlsx->write(i, columnCount + 1, result.department, format); // Row(s) QVariant value(result.information); xlsx->write(i, columnCount + 2, value); format = xlsx->cellAt(i, columnCount + 2)->format(); format.setPatternBackgroundColor(result.color); xlsx->write(i, columnCount + 2, value, format); } if ((result.color == Qt::white) // add to database && check_box_check->isChecked()) { newCount++; QString q = "INSERT INTO " + table_names.at(0) + " ("; for (int k = 1; k < column_names.at(0).size(); ++k) { q += column_names.at(0).at(k).first; if (k < column_names.at(0).size() - 1) { q += ", "; } else { q += ") VALUES("; } } for (int k = 1; k < column_names.at(0).size(); ++k) { q += ":" + column_names.at(0).at(k).first; if (k < column_names.at(0).size() - 1) { q += ", "; } else { q += ");"; } } query.prepare(q); // qDebug() << q; QString name = ""; for (int k = 1; k <= visible_columns.at(0); ++k) { QVariant value = xlsx->read(i, k); query.bindValue(":" + column_names.at(0).at(k).first, value); // qDebug() << ":" + column_names.at(0).at(k).first << " = " << value.toString(); if (column_names.at(0).at(k).first == "Name") { name = value.toString(); } } QString name1 = refineString1(name); QString name2 = refineString2(name1); query.bindValue(":Name1", QVariant(name1)); query.bindValue(":Name2", QVariant(name2)); if (query.exec()) { insertCount++; } } } xlsx->save(); delete xlsx; QMessageBox::information(this,tr(""), tr("Check ok: new = ") + QString::number(newCount) + tr("; insert = ") + QString::number(insertCount)); }