// For a solid fill, Excel reverses the role of foreground and background colours void StylesTest::testSolidFillBackgroundColor() { QXlsx::Styles styles; QXlsx::Format *format = styles.createFormat(); format->setPatternBackgroundColor(QColor(Qt::red)); styles.addFormat(format); QByteArray xmlData = styles.saveToXmlData(); QVERIFY(xmlData.contains("<patternFill patternType=\"solid\"><fgColor rgb=\"FFff0000\"/>")); }
void DBsearchsetting::on_seve_excel_btn_clicked() { QFileDialog filepath(this); filepath.setAcceptMode(QFileDialog::AcceptSave); filepath.setNameFilter("*.xlsx"); QStringList headeritem; for(int i=0;i<Setting_Query->columnCount();i++){ headeritem.insert(i,Setting_Query->headerData(i,Qt::Horizontal).toString()); } QString S_filepath; S_filepath = filepath.getSaveFileName(this,QString(""),QString(""),QString("*.xlsx")); if(S_filepath != ""){ QXlsx::Document xlsx; for(int i=1;i<=Setting_Query->columnCount();i++){ QXlsx::Format format; format.setHorizontalAlignment(QXlsx::Format::AlignHCenter); format.setPatternBackgroundColor(QColor("gray")); xlsx.setColumnWidth(i,20); xlsx.write(1,i,headeritem.at(i-1),format); } for(int i=0;i<Setting_Query->rowCount();i++){ for(int j=0;j<Setting_Query->columnCount();j++){ QXlsx::Format format; format.setHorizontalAlignment(QXlsx::Format::AlignHCenter); QString data =Setting_Query->index(i,j).data(0).toString(); xlsx.write(i+2,j+1,data,format); } } bool saveresult; saveresult = xlsx.saveAs(S_filepath); QMessageBox messagebox; if(saveresult){ messagebox.setText(tr("save ok")); }else { messagebox.setText(tr("save error")); } messagebox.exec(); } }
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)); }