Document Class

The Document class provides a API that is used to handle the contents of .xlsx files. More...

Header: #include <Document>
Inherits: QObject.

Public Functions

Document(QObject * parent = 0)
Document(const QString & name, QObject * parent = 0)
Document(QIODevice * device, QObject * parent = 0)
~Document()
bool addConditionalFormatting(const ConditionalFormatting & cf)
bool addDataValidation(const DataValidation & validation)
bool addSheet(const QString & name = QString(), AbstractSheet::SheetType type = AbstractSheet::ST_WorkSheet)
Cell * cellAt(int row, int col) const
Cell * cellAt(const CellReference & pos) const
Format columnFormat(int column)
double columnWidth(int column)
bool copySheet(const QString & srcName, const QString & distName = QString())
AbstractSheet * currentSheet() const
Worksheet * currentWorksheet() const
bool defineName(const QString & name, const QString & formula, const QString & comment = QString(), const QString & scope = QString())
bool deleteSheet(const QString & name)
CellRange dimension() const
QString documentProperty(const QString & key) const
QStringList documentPropertyNames() const
bool groupColumns(int colFirst, int colLast, bool collapsed = true)
bool groupRows(int rowFirst, int rowLast, bool collapsed = true)
Chart * insertChart(int row, int col, const QSize & size)
bool insertImage(int row, int column, const QImage & image)
bool insertSheet(int index, const QString & name = QString(), AbstractSheet::SheetType type = AbstractSheet::ST_WorkSheet)
bool isColumnHidden(int column)
bool isRowHidden(int row)
bool mergeCells(const CellRange & range, const Format & format = Format())
bool moveSheet(const QString & srcName, int distIndex)
QVariant read(int row, int col) const
QVariant read(const CellReference & cell) const
bool renameSheet(const QString & oldName, const QString & newName)
Format rowFormat(int row)
double rowHeight(int row)
bool save() const
bool saveAs(const QString & name) const
bool saveAs(QIODevice * device) const
bool selectSheet(const QString & name)
bool setColumnFormat(const CellRange & range, const Format & format)
bool setColumnFormat(int column, const Format & format)
bool setColumnFormat(int colFirst, int colLast, const Format & format)
bool setColumnHidden(const CellRange & range, bool hidden)
bool setColumnHidden(int column, bool hidden)
bool setColumnHidden(int colFirst, int colLast, bool hidden)
bool setColumnWidth(const CellRange & range, double width)
bool setColumnWidth(int column, double width)
bool setColumnWidth(int colFirst, int colLast, double width)
void setDocumentProperty(const QString & key, const QString & property)
bool setRowFormat(int row, const Format & format)
bool setRowFormat(int rowFirst, int rowLast, const Format & format)
bool setRowHeight(int row, double height)
bool setRowHeight(int rowFirst, int rowLast, double height)
bool setRowHidden(int row, bool hidden)
bool setRowHidden(int rowFirst, int rowLast, bool hidden)
AbstractSheet * sheet(const QString & sheetName) const
QStringList sheetNames() const
bool unmergeCells(const CellRange & range)
Workbook * workbook() const
bool write(int row, int col, const QVariant & value, const Format & format = Format())
bool write(const CellReference & row_column, const QVariant & value, const Format & format = Format())
  • 31 public functions inherited from QObject

Additional Inherited Members

  • 1 property inherited from QObject
  • 1 public slot inherited from QObject
  • 2 signals inherited from QObject
  • 1 public variable inherited from QObject
  • 10 static public members inherited from QObject
  • 9 protected functions inherited from QObject
  • 2 protected variables inherited from QObject

Detailed Description

The Document class provides a API that is used to handle the contents of .xlsx files.

Member Function Documentation

Document::Document(QObject * parent = 0)

Creates a new empty xlsx document. The parent argument is passed to QObject's constructor.

Document::Document(const QString & name, QObject * parent = 0)

This is an overloaded function.

Try to open an existing xlsx document named name. The parent argument is passed to QObject's constructor.

Document::Document(QIODevice * device, QObject * parent = 0)

This is an overloaded function.

Try to open an existing xlsx document from device. The parent argument is passed to QObject's constructor.

Document::~Document()

Destroys the document and cleans up.

bool Document::addConditionalFormatting(const ConditionalFormatting & cf)

Add a conditional formatting cf for current worksheet. Returns true if successful.

bool Document::addDataValidation(const DataValidation & validation)

Add a data validation rule for current worksheet. Returns true if successful.

bool Document::addSheet(const QString & name = QString(), AbstractSheet::SheetType type = AbstractSheet::ST_WorkSheet)

Creates and append an sheet with the given name and type. Return true if success.

Cell * Document::cellAt(int row, int col) const

Returns the cell at the given row and col. If there is no cell at the specified position, the function returns 0.

See also read().

Cell * Document::cellAt(const CellReference & pos) const

This is an overloaded function.

Returns the cell at the position pos. If there is no cell at the specified position, the function returns 0.

See also read().

Format Document::columnFormat(int column)

Returns formatting of the column. Columns are 1-indexed.

See also setColumnFormat().

double Document::columnWidth(int column)

Returns width of the column in characters of the normal font. Columns are 1-indexed. Returns true on success.

See also setColumnWidth().

bool Document::copySheet(const QString & srcName, const QString & distName = QString())

Make a copy of the worksheet srcName with the new name distName. Returns true if the success.

AbstractSheet * Document::currentSheet() const

Return pointer of current sheet.

Worksheet * Document::currentWorksheet() const

Return pointer of current worksheet. If the type of sheet is not AbstractSheet::ST_WorkSheet, then 0 will be returned.

bool Document::defineName(const QString & name, const QString & formula, const QString & comment = QString(), const QString & scope = QString())

Create a defined name in the workbook with the given name, formula, comment and scope.

name The defined name. formula The cell or range that the defined name refers to. scope The name of one worksheet, or empty which means golbal scope. Returns Return false if the name invalid.

bool Document::deleteSheet(const QString & name)

Delete the worksheet name. Returns true if current sheet was deleted successfully.

CellRange Document::dimension() const

Return the range that contains cell data.

QString Document::documentProperty(const QString & key) const

Returns the value of the document's key property.

See also setDocumentProperty().

QStringList Document::documentPropertyNames() const

Returns the names of all properties that were addedusing setDocumentProperty().

bool Document::groupColumns(int colFirst, int colLast, bool collapsed = true)

Groups columns from colFirst to colLast with the given collapsed. Returns false if error occurs.

bool Document::groupRows(int rowFirst, int rowLast, bool collapsed = true)

Groups rows from rowFirst to rowLast with the given collapsed. Returns false if error occurs.

Chart * Document::insertChart(int row, int col, const QSize & size)

Creates an chart with the given size and insert it to the current active worksheet at the position row, col. The chart will be returned.

bool Document::insertImage(int row, int column, const QImage & image)

Insert an image to current active worksheet at the position row, column Returns ture if success.

bool Document::insertSheet(int index, const QString & name = QString(), AbstractSheet::SheetType type = AbstractSheet::ST_WorkSheet)

Creates and inserts an document with the given name and type at the index. Returns false if the name already used.

bool Document::isColumnHidden(int column)

Returns true if column is hidden. Columns are 1-indexed.

bool Document::isRowHidden(int row)

Returns true if row is hidden.

bool Document::mergeCells(const CellRange & range, const Format & format = Format())

Merge a range of cells. The first cell should contain the data and the others should be blank. All cells will be applied the same style if a valid format is given. Returns true on success.

Note: All cells except the top-left one will be cleared.

bool Document::moveSheet(const QString & srcName, int distIndex)

Move the worksheet srcName to the new pos distIndex. Returns true if the success.

QVariant Document::read(int row, int col) const

Returns the contents of the cell (row, col).

See also cellAt().

QVariant Document::read(const CellReference & cell) const

This is an overloaded function.

Returns the contents of the cell cell.

See also cellAt().

bool Document::renameSheet(const QString & oldName, const QString & newName)

Rename the worksheet from oldName to newName. Returns true if the success.

Format Document::rowFormat(int row)

Returns format of row.

See also setRowFormat().

double Document::rowHeight(int row)

Returns height of row in points.

See also setRowHeight().

bool Document::save() const

Save current document to the filesystem. If no name specified when the document constructed, a default name "book1.xlsx" will be used. Returns true if saved successfully.

bool Document::saveAs(const QString & name) const

Saves the document to the file with the given name. Returns true if saved successfully.

bool Document::saveAs(QIODevice * device) const

This is an overloaded function.

This function writes a document to the given device.

Warning: The device will be closed when this function returned.

bool Document::selectSheet(const QString & name)

Set worksheet named name to be active sheet. Returns true if success.

bool Document::setColumnFormat(const CellRange & range, const Format & format)

Sets format property of columns with the gien range and format. Returns true on success.

See also columnFormat().

bool Document::setColumnFormat(int column, const Format & format)

Sets format property column to format. Columns are 1-indexed. Returns true on success.

bool Document::setColumnFormat(int colFirst, int colLast, const Format & format)

Sets format property of columns [colFirst, colLast] to format. Columns are 1-indexed. Returns true on success.

bool Document::setColumnHidden(const CellRange & range, bool hidden)

Sets hidden property of columns range to hidden. Columns are 1-indexed. Hidden columns are not visible. Returns true on success.

See also isColumnHidden().

bool Document::setColumnHidden(int column, bool hidden)

Sets hidden property of a column. Columns are 1-indexed. Returns true on success.

bool Document::setColumnHidden(int colFirst, int colLast, bool hidden)

Sets hidden property of columns [colFirst, colLast] to hidden. Columns are 1-indexed. Returns true on success.

bool Document::setColumnWidth(const CellRange & range, double width)

Sets width in characters of columns with the given range and width. Returns true on success.

See also columnWidth().

bool Document::setColumnWidth(int column, double width)

Sets width in characters column to width. Columns are 1-indexed. Returns true on success.

bool Document::setColumnWidth(int colFirst, int colLast, double width)

Sets width in characters for columns [colFirst, colLast]. Columns are 1-indexed. Returns true on success.

void Document::setDocumentProperty(const QString & key, const QString & property)

Set the document properties such as Title, Author etc.

The method can be used to set the document properties of the Excel file created by Qt Xlsx. These properties are visible when you use the Office Button -> Prepare -> Properties option in Excel and are also available to external applications that read or index windows files.

The property key that can be set are:

  • title
  • subject
  • creator
  • manager
  • company
  • category
  • keywords
  • description
  • status

See also documentProperty().

bool Document::setRowFormat(int row, const Format & format)

Sets the format of the row. Rows are 1-indexed.

Returns true if success.

See also rowFormat().

bool Document::setRowFormat(int rowFirst, int rowLast, const Format & format)

Sets the format of the rows including and between rowFirst and rowLast. Rows are 1-indexed.

Returns true if success.

bool Document::setRowHeight(int row, double height)

Sets the height of the row row. Row height measured in point size. Rows are 1-indexed.

Returns true if success.

See also rowHeight().

bool Document::setRowHeight(int rowFirst, int rowLast, double height)

Sets the height of the rows including and between rowFirst and rowLast. Row height measured in point size. Rows are 1-indexed.

Returns true if success.

bool Document::setRowHidden(int row, bool hidden)

Sets the hidden property of the row row. Rows are 1-indexed. If hidden is true rows will not be visible.

Returns true if success.

See also isRowHidden().

bool Document::setRowHidden(int rowFirst, int rowLast, bool hidden)

Sets the hidden property of the rows including and between rowFirst and rowLast. Rows are 1-indexed. If hidden is true rows will not be visible.

Returns true if success.

AbstractSheet * Document::sheet(const QString & sheetName) const

Returns the sheet object named sheetName.

QStringList Document::sheetNames() const

Returns the names of worksheets contained in current document.

bool Document::unmergeCells(const CellRange & range)

Unmerge the cells in the range. Returns true on success.

Workbook * Document::workbook() const

Return the internal Workbook object.

bool Document::write(int row, int col, const QVariant & value, const Format & format = Format())

Write value to cell (row, col) with the format. Returns true on success.

bool Document::write(const CellReference & row_column, const QVariant & value, const Format & format = Format())

This is an overloaded function.

Write value to cell row_column with the given format.