Debao Zhang
11 years ago
6 changed files with 329 additions and 0 deletions
@ -0,0 +1,35 @@ |
|||||
|
#include <QtWidgets> |
||||
|
#include "xlsxdocument.h" |
||||
|
#include "xlsxsheetmodel.h" |
||||
|
|
||||
|
using namespace QXlsx; |
||||
|
|
||||
|
int main(int argc, char **argv) |
||||
|
{ |
||||
|
QApplication app(argc, argv); |
||||
|
|
||||
|
//![0]
|
||||
|
QString filePath = QFileDialog::getOpenFileName(0, "Open xlsx file", QString(), ".xlsx"); |
||||
|
if (filePath.isEmpty()) |
||||
|
return -1; |
||||
|
//![0]
|
||||
|
|
||||
|
//![1]
|
||||
|
QTabWidget tabWidget; |
||||
|
tabWidget.setWindowTitle(filePath + " - Qt Xlsx Demo"); |
||||
|
tabWidget.setTabPosition(QTabWidget::South); |
||||
|
//![1]
|
||||
|
|
||||
|
//![2]
|
||||
|
Document xlsx(filePath); |
||||
|
foreach (QString sheetName, xlsx.worksheetNames()) { |
||||
|
QTableView *view = new QTableView(&tabWidget); |
||||
|
SheetModel *model = new SheetModel(xlsx.worksheet(sheetName), view); |
||||
|
view->setModel(model); |
||||
|
tabWidget.addTab(view, sheetName); |
||||
|
} |
||||
|
//![2]
|
||||
|
|
||||
|
tabWidget.show(); |
||||
|
return app.exec(); |
||||
|
} |
@ -0,0 +1,162 @@ |
|||||
|
/****************************************************************************
|
||||
|
** Copyright (c) 2013-2014 Debao Zhang <hello@debao.me> |
||||
|
** All right reserved. |
||||
|
** |
||||
|
** Permission is hereby granted, free of charge, to any person obtaining |
||||
|
** a copy of this software and associated documentation files (the |
||||
|
** "Software"), to deal in the Software without restriction, including |
||||
|
** without limitation the rights to use, copy, modify, merge, publish, |
||||
|
** distribute, sublicense, and/or sell copies of the Software, and to |
||||
|
** permit persons to whom the Software is furnished to do so, subject to |
||||
|
** the following conditions: |
||||
|
** |
||||
|
** The above copyright notice and this permission notice shall be |
||||
|
** included in all copies or substantial portions of the Software. |
||||
|
** |
||||
|
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
||||
|
** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
||||
|
** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
||||
|
** NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
||||
|
** LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
||||
|
** OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
||||
|
** WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
||||
|
** |
||||
|
****************************************************************************/ |
||||
|
|
||||
|
#include "xlsxsheetmodel.h" |
||||
|
#include "xlsxsheetmodel_p.h" |
||||
|
#include "xlsxworksheet.h" |
||||
|
|
||||
|
QT_BEGIN_NAMESPACE_XLSX |
||||
|
|
||||
|
SheetModelPrivate::SheetModelPrivate(SheetModel *p) |
||||
|
:q_ptr(p) |
||||
|
{ |
||||
|
|
||||
|
} |
||||
|
|
||||
|
/*!
|
||||
|
* \class SheetModel |
||||
|
* |
||||
|
* Helper class for gui applicaiton user |
||||
|
* |
||||
|
* \note SheetModel indices start from 0, while Worksheet |
||||
|
* column/row indices start from 1. |
||||
|
*/ |
||||
|
|
||||
|
/*!
|
||||
|
* Creates a model object with the given \a sheet and \a parent. |
||||
|
*/ |
||||
|
SheetModel::SheetModel(Worksheet *sheet, QObject *parent) |
||||
|
:QAbstractTableModel(parent), d_ptr(new SheetModelPrivate(this)) |
||||
|
{ |
||||
|
d_ptr->sheet = sheet; |
||||
|
} |
||||
|
|
||||
|
/*!
|
||||
|
* Destroys the model. |
||||
|
*/ |
||||
|
SheetModel::~SheetModel() |
||||
|
{ |
||||
|
delete d_ptr; |
||||
|
} |
||||
|
|
||||
|
int SheetModel::rowCount(const QModelIndex &/*parent*/) const |
||||
|
{ |
||||
|
Q_D(const SheetModel); |
||||
|
return d->sheet->dimension().lastRow(); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
int SheetModel::columnCount(const QModelIndex &/*parent*/) const |
||||
|
{ |
||||
|
Q_D(const SheetModel); |
||||
|
return d->sheet->dimension().lastColumn(); |
||||
|
} |
||||
|
|
||||
|
Qt::ItemFlags SheetModel::flags(const QModelIndex &index) const |
||||
|
{ |
||||
|
if (!index.isValid()) |
||||
|
return Qt::NoItemFlags; |
||||
|
return Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable; |
||||
|
} |
||||
|
|
||||
|
QVariant SheetModel::data(const QModelIndex &index, int role) const |
||||
|
{ |
||||
|
Q_D(const SheetModel); |
||||
|
|
||||
|
if (!index.isValid()) |
||||
|
return QVariant(); |
||||
|
|
||||
|
Cell *cell = d->sheet->cellAt(index.row()+1, index.column()+1); |
||||
|
if (!cell) |
||||
|
return QVariant(); |
||||
|
QVariant userFriendlyValue = d->sheet->read(index.row()+1, index.column()+1); |
||||
|
|
||||
|
if (role == Qt::DisplayRole) { |
||||
|
if (cell->isDateTime()) |
||||
|
return userFriendlyValue; |
||||
|
return cell->value(); |
||||
|
} else if (role == Qt::EditRole) { |
||||
|
return userFriendlyValue; |
||||
|
} |
||||
|
|
||||
|
return QVariant(); |
||||
|
} |
||||
|
|
||||
|
/*
|
||||
|
* Copy from xlsxutility.cpp, so this example don't depend on the xlsx-private |
||||
|
* This function should be removed once this class moved to the xlsx library. |
||||
|
*/ |
||||
|
static QString col_to_name(int col_num) |
||||
|
{ |
||||
|
QString col_str; |
||||
|
|
||||
|
int remainder; |
||||
|
while (col_num) { |
||||
|
remainder = col_num % 26; |
||||
|
if (remainder == 0) |
||||
|
remainder = 26; |
||||
|
col_str.prepend(QChar('A'+remainder-1)); |
||||
|
col_num = (col_num - 1) / 26; |
||||
|
} |
||||
|
|
||||
|
return col_str; |
||||
|
} |
||||
|
|
||||
|
QVariant SheetModel::headerData(int section, Qt::Orientation orientation, int role) const |
||||
|
{ |
||||
|
if (role == Qt::DisplayRole) { |
||||
|
if (orientation == Qt::Horizontal) |
||||
|
return col_to_name(section + 1); |
||||
|
else |
||||
|
return QString::number(section + 1); |
||||
|
} |
||||
|
return QVariant(); |
||||
|
} |
||||
|
|
||||
|
bool SheetModel::setData(const QModelIndex &index, const QVariant &value, int role) |
||||
|
{ |
||||
|
Q_D(const SheetModel); |
||||
|
|
||||
|
if (!index.isValid()) |
||||
|
return false; |
||||
|
|
||||
|
if (role == Qt::EditRole) { |
||||
|
if (d->sheet->write(index.row()+1, index.column()+1, value) == 0) |
||||
|
return true; |
||||
|
} |
||||
|
|
||||
|
return false; |
||||
|
} |
||||
|
|
||||
|
/*!
|
||||
|
* Returns the sheet object. |
||||
|
*/ |
||||
|
Worksheet *SheetModel::sheet() const |
||||
|
{ |
||||
|
Q_D(const SheetModel); |
||||
|
return d->sheet; |
||||
|
} |
||||
|
|
||||
|
QT_END_NAMESPACE_XLSX |
@ -0,0 +1,63 @@ |
|||||
|
/****************************************************************************
|
||||
|
** Copyright (c) 2013-2014 Debao Zhang <hello@debao.me> |
||||
|
** All right reserved. |
||||
|
** |
||||
|
** Permission is hereby granted, free of charge, to any person obtaining |
||||
|
** a copy of this software and associated documentation files (the |
||||
|
** "Software"), to deal in the Software without restriction, including |
||||
|
** without limitation the rights to use, copy, modify, merge, publish, |
||||
|
** distribute, sublicense, and/or sell copies of the Software, and to |
||||
|
** permit persons to whom the Software is furnished to do so, subject to |
||||
|
** the following conditions: |
||||
|
** |
||||
|
** The above copyright notice and this permission notice shall be |
||||
|
** included in all copies or substantial portions of the Software. |
||||
|
** |
||||
|
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
||||
|
** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
||||
|
** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
||||
|
** NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
||||
|
** LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
||||
|
** OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
||||
|
** WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
||||
|
** |
||||
|
****************************************************************************/ |
||||
|
|
||||
|
#ifndef QXLSX_XLSXSHEETMODEL_H |
||||
|
#define QXLSX_XLSXSHEETMODEL_H |
||||
|
|
||||
|
#include "xlsxglobal.h" |
||||
|
#include <QAbstractTableModel> |
||||
|
|
||||
|
QT_BEGIN_NAMESPACE_XLSX |
||||
|
|
||||
|
class Worksheet; |
||||
|
class SheetModelPrivate; |
||||
|
|
||||
|
class SheetModel : public QAbstractTableModel |
||||
|
{ |
||||
|
Q_OBJECT |
||||
|
Q_DECLARE_PRIVATE(SheetModel) |
||||
|
public: |
||||
|
explicit SheetModel(Worksheet *sheet, QObject *parent = 0); |
||||
|
~SheetModel(); |
||||
|
|
||||
|
int rowCount(const QModelIndex &parent = QModelIndex()) const; |
||||
|
int columnCount(const QModelIndex &parent = QModelIndex()) const; |
||||
|
Qt::ItemFlags flags(const QModelIndex & index) const; |
||||
|
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const; |
||||
|
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const; |
||||
|
bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole); |
||||
|
|
||||
|
Worksheet *sheet() const; |
||||
|
signals: |
||||
|
|
||||
|
public slots: |
||||
|
|
||||
|
private: |
||||
|
SheetModelPrivate * const d_ptr; |
||||
|
}; |
||||
|
|
||||
|
QT_END_NAMESPACE_XLSX |
||||
|
|
||||
|
#endif // QXLSX_XLSXSHEETMODEL_H
|
@ -0,0 +1,56 @@ |
|||||
|
/****************************************************************************
|
||||
|
** Copyright (c) 2013-2014 Debao Zhang <hello@debao.me> |
||||
|
** All right reserved. |
||||
|
** |
||||
|
** Permission is hereby granted, free of charge, to any person obtaining |
||||
|
** a copy of this software and associated documentation files (the |
||||
|
** "Software"), to deal in the Software without restriction, including |
||||
|
** without limitation the rights to use, copy, modify, merge, publish, |
||||
|
** distribute, sublicense, and/or sell copies of the Software, and to |
||||
|
** permit persons to whom the Software is furnished to do so, subject to |
||||
|
** the following conditions: |
||||
|
** |
||||
|
** The above copyright notice and this permission notice shall be |
||||
|
** included in all copies or substantial portions of the Software. |
||||
|
** |
||||
|
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
||||
|
** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
||||
|
** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
||||
|
** NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
||||
|
** LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
||||
|
** OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
||||
|
** WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
||||
|
** |
||||
|
****************************************************************************/ |
||||
|
|
||||
|
#ifndef XLSXSHEETMODEL_P_H |
||||
|
#define XLSXSHEETMODEL_P_H |
||||
|
|
||||
|
//
|
||||
|
// W A R N I N G
|
||||
|
// -------------
|
||||
|
//
|
||||
|
// This file is not part of the Qt Xlsx API. It exists for the convenience
|
||||
|
// of the Qt Xlsx. This header file may change from
|
||||
|
// version to version without notice, or even be removed.
|
||||
|
//
|
||||
|
// We mean it.
|
||||
|
//
|
||||
|
|
||||
|
#include "xlsxsheetmodel.h" |
||||
|
|
||||
|
QT_BEGIN_NAMESPACE_XLSX |
||||
|
|
||||
|
class SheetModelPrivate |
||||
|
{ |
||||
|
Q_DECLARE_PUBLIC(SheetModel) |
||||
|
public: |
||||
|
SheetModelPrivate(SheetModel *p); |
||||
|
|
||||
|
Worksheet *sheet; |
||||
|
SheetModel *q_ptr; |
||||
|
}; |
||||
|
|
||||
|
QT_END_NAMESPACE_XLSX |
||||
|
|
||||
|
#endif // XLSXSHEETMODEL_P_H
|
@ -0,0 +1,11 @@ |
|||||
|
TARGET = xlsxwidget |
||||
|
|
||||
|
#include(../../../src/xlsx/qtxlsx.pri) |
||||
|
QT+= xlsx xlsx-private widgets |
||||
|
|
||||
|
SOURCES += main.cpp \ |
||||
|
xlsxsheetmodel.cpp |
||||
|
|
||||
|
HEADERS += \ |
||||
|
xlsxsheetmodel.h \ |
||||
|
xlsxsheetmodel_p.h |
Loading…
Reference in new issue