QTXLSX_USE_NAMESPACE int main() { Document xlsx; for (int i=1; i<=10; ++i) { xlsx.write(i, 1, i); xlsx.write(i, 2, i*i); xlsx.write(i, 3, i*i*i); } Worksheet* sheet = xlsx.currentWorksheet(); sheet->writeFormula(CellReference(11, 1), CellFormula(Formula::AVERAGE(CellRange(1, 1, 10, 1)), CellReference::toString(11, 1), CellFormula::SharedType)); sheet->writeFormula(CellReference(11, 2), CellFormula(Formula::SUM(CellRange(1, 2, 10, 2)), CellReference::toString(11, 2), CellFormula::SharedType)); sheet->writeFormula(CellReference(11, 3), CellFormula(Formula::COUNTIF(CellRange(1, 3, 10, 3), QString("\">50\"")), CellReference::toString(11, 3), CellFormula::SharedType)); xlsx.save(); return 0; }
/*! Convert the range to string notation, such as "A1:B5". */ QString CellRange::toString(bool row_abs, bool col_abs) const { if (!isValid()) return QString(); if (left == right && top == bottom) { //Single cell return CellReference(top, left).toString(row_abs, col_abs); } QString cell_1 = CellReference(top, left).toString(row_abs, col_abs); QString cell_2 = CellReference(bottom, right).toString(row_abs, col_abs); return cell_1 + QLatin1String(":") + cell_2; }
// Loads table content from a file void Table::load(std::string fname) { const std::vector<char> delims = {delim}; std::ifstream in(fname); if (in.fail()) throw FileOpenException(); // Delete old table data.clear(); int k = 0; while (!in.eof()) { k++; std::string line; // Load a line getline(in, line); // Split a line by cell delimiter std::vector<std::string> parsed = split_string(line, delims, false, true); int l = 0; for (auto it = parsed.begin(); it < parsed.end(); it++) { l++; // Remove spaces trim(*it, ' '); // Dont put empty cells if (*it == "") continue; set_cell(CellReference(k, l), *it); } } }
/*! Convert the range to string notation, such as "A1:B5". */ QString CellRange::toString(bool row_abs, bool col_abs) const { if (!isValid()) return QString(); if (left == right && top == bottom) { //Single cell return CellReference(top, left, _sheet).toString(row_abs, col_abs); } QString sheet_name; if (!_sheet.isEmpty()) sheet_name.append(QStringLiteral("'%1'!").arg(_sheet)); QString cell_1 = CellReference(top, left).toString(row_abs, col_abs); QString cell_2 = CellReference(bottom, right).toString(row_abs, col_abs); return sheet_name + cell_1 + QLatin1String(":") + cell_2; }
CellReference CellReference::fromString(const QString &cell) { return CellReference(cell); }
QString CellReference::toString(int row, int column, const QString &sheet) { return CellReference(row, column, sheet).toString(); }
Formula CellReference::toFormula(int row, int column, const QString &sheet) { return CellReference(row, column, sheet).toFormula(); }