1
0
mirror of https://github.com/QtExcel/QXlsx.git synced 2025-02-06 05:08:22 +08:00

copycat is fixed (chartsheet)

This commit is contained in:
Jay Two 2019-02-13 15:53:15 +09:00
parent 342b885191
commit e992a7e67f
12 changed files with 178 additions and 81 deletions

View File

@ -36,13 +36,13 @@ private slots:
void on_action_Print_triggered(); void on_action_Print_triggered();
void print(QPrinter *printer); void print(QPrinter *printer);
private: protected:
Ui::MainWindow *ui; Ui::MainWindow *ui;
QXlsx::Document* xlsxDoc; QXlsx::Document* xlsxDoc;
QTabWidget *tabWidget; QTabWidget *tabWidget;
QVector<XlsxTab*> xlsxTabList; QVector<XlsxTab*> xlsxTabList;
private: protected:
bool loadXlsx(QString xlsxFilename); bool loadXlsx(QString xlsxFilename);
std::string convertFromNumberToExcelColumn(int n); std::string convertFromNumberToExcelColumn(int n);
}; };

View File

@ -33,9 +33,9 @@ XlsxTab::XlsxTab(QWidget* parent,
Q_ASSERT( NULL != table ); Q_ASSERT( NULL != table );
// set layout // set layout
QVBoxLayout *layout = new QVBoxLayout; vLayout = new QVBoxLayout;
layout->addWidget(table); vLayout->addWidget(table);
this->setLayout(layout); this->setLayout(vLayout);
document = ptrDoc; // set document document = ptrDoc; // set document
sheet = ptrSheet; // set sheet data sheet = ptrSheet; // set sheet data
@ -48,10 +48,17 @@ XlsxTab::XlsxTab(QWidget* parent,
XlsxTab::~XlsxTab() XlsxTab::~XlsxTab()
{ {
if ( NULL != vLayout )
{
vLayout->deleteLater();
}
if ( NULL != table ) if ( NULL != table )
{ {
table->deleteLater(); table->deleteLater();
} }
} }
bool XlsxTab::setSheet() bool XlsxTab::setSheet()
@ -253,3 +260,40 @@ bool XlsxTab::setSheet()
return true; return true;
} }
std::string XlsxTab::convertFromNumberToExcelColumn(int n)
{
// main code from https://www.geeksforgeeks.org/find-excel-column-name-given-number/
// Function to print Excel column name for a given column number
std::string stdString;
char str[1000]; // To store result (Excel column name)
int i = 0; // To store current index in str which is result
while ( n > 0 )
{
// Find remainder
int rem = n % 26;
// If remainder is 0, then a 'Z' must be there in output
if ( rem == 0 )
{
str[i++] = 'Z';
n = (n/26) - 1;
}
else // If remainder is non-zero
{
str[i++] = (rem-1) + 'A';
n = n / 26;
}
}
str[i] = '\0';
// Reverse the string and print result
std::reverse( str, str + strlen(str) );
stdString = str;
return stdString;
}

View File

@ -5,11 +5,14 @@
#include <cstdio> #include <cstdio>
#include <string>
#include <QtGlobal> #include <QtGlobal>
#include <QObject> #include <QObject>
#include <QString> #include <QString>
#include <QWidget> #include <QWidget>
#include <QTableWidget> #include <QTableWidget>
#include <QVBoxLayout>
#include "xlsx.h" #include "xlsx.h"
#include "XlsxTableWidget.h" #include "XlsxTableWidget.h"
@ -31,13 +34,17 @@ public slots:
signals: signals:
protected: protected:
XlsxTableWidget* table;
QXlsx::Document* document; QXlsx::Document* document;
QXlsx::AbstractSheet* sheet; QXlsx::AbstractSheet* sheet;
int sheetIndex; int sheetIndex;
protected:
XlsxTableWidget* table;
QVBoxLayout *vLayout;
protected: protected:
bool setSheet(); bool setSheet();
std::string convertFromNumberToExcelColumn(int n);
}; };

View File

@ -11,7 +11,7 @@
XlsxTableWidget::XlsxTableWidget(QWidget* parent) XlsxTableWidget::XlsxTableWidget(QWidget* parent)
: QTableWidget(parent) : QTableWidget(parent)
{ {
//
} }
XlsxTableWidget::~XlsxTableWidget() XlsxTableWidget::~XlsxTableWidget()
@ -29,6 +29,8 @@ void XlsxTableWidget::mousePressEvent(QMouseEvent *event)
QList<QTableWidgetItem *> items = this->selectedItems(); QList<QTableWidgetItem *> items = this->selectedItems();
// selected range
QList<QTableWidgetSelectionRange> ranges = this->selectedRanges(); QList<QTableWidgetSelectionRange> ranges = this->selectedRanges();
for (int ic = 0 ; ic < ranges.size(); ic++ ) for (int ic = 0 ; ic < ranges.size(); ic++ )
{ {
@ -44,13 +46,16 @@ void XlsxTableWidget::mousePressEvent(QMouseEvent *event)
int leftCol = range.leftColumn(); int leftCol = range.leftColumn();
int rightCol = range.rightColumn(); int rightCol = range.rightColumn();
qDebug() << QTime::currentTime(); qDebug()
<< QTime::currentTime();
qDebug() << "row: " << rowCount << " : " qDebug()
<< "row: " << rowCount << " : "
<< " top:" << topRow << " top:" << topRow
<< ", bottom:" << bottomRow ; << ", bottom:" << bottomRow ;
qDebug() << "col: " << colCount << " : " qDebug()
<< "col: " << colCount << " : "
<< " left:" << leftCol << " left:" << leftCol
<< " right:" << rightCol ; << " right:" << rightCol ;

View File

@ -36,19 +36,14 @@ class AbstractSheetPrivate;
class AbstractSheet : public AbstractOOXmlFile class AbstractSheet : public AbstractOOXmlFile
{ {
Q_DECLARE_PRIVATE(AbstractSheet) Q_DECLARE_PRIVATE(AbstractSheet)
public:
enum SheetType {
ST_WorkSheet,
ST_ChartSheet,
ST_DialogSheet,
ST_MacroSheet
};
enum SheetState { public:
SS_Visible, Workbook *workbook() const;
SS_Hidden,
SS_VeryHidden public:
}; // NOTE: If all Qt compiler supports C++1x, recommend to use a 'class enum'.
enum SheetType { ST_WorkSheet, ST_ChartSheet, ST_DialogSheet, ST_MacroSheet };
enum SheetState { SS_Visible,SS_Hidden, SS_VeryHidden };
QString sheetName() const; QString sheetName() const;
SheetType sheetType() const; SheetType sheetType() const;
@ -59,8 +54,6 @@ public:
void setHidden(bool hidden); void setHidden(bool hidden);
void setVisible(bool visible); void setVisible(bool visible);
Workbook *workbook() const;
protected: protected:
friend class Workbook; friend class Workbook;
AbstractSheet(const QString &sheetName, int sheetId, Workbook *book, AbstractSheetPrivate *d); AbstractSheet(const QString &sheetName, int sheetId, Workbook *book, AbstractSheetPrivate *d);

View File

@ -30,21 +30,24 @@
#include <QSharedPointer> #include <QSharedPointer>
QT_BEGIN_NAMESPACE_XLSX QT_BEGIN_NAMESPACE_XLSX
class Workbook; class Workbook;
class DocumentPrivate; class DocumentPrivate;
class ChartsheetPrivate; class ChartsheetPrivate;
class Chart; class Chart;
class Chartsheet : public AbstractSheet class Chartsheet : public AbstractSheet
{ {
Q_DECLARE_PRIVATE(Chartsheet) Q_DECLARE_PRIVATE(Chartsheet)
public:
public:
~Chartsheet(); ~Chartsheet();
Chart *chart(); Chart *chart();
private: private:
friend class DocumentPrivate; friend class DocumentPrivate;
friend class Workbook; friend class Workbook;
Chartsheet(const QString &sheetName, int sheetId, Workbook *book, CreateFlag flag); Chartsheet(const QString &sheetName, int sheetId, Workbook *book, CreateFlag flag);
Chartsheet *copy(const QString &distName, int distId) const; Chartsheet *copy(const QString &distName, int distId) const;

View File

@ -51,28 +51,46 @@ class Worksheet : public AbstractSheet
{ {
Q_DECLARE_PRIVATE(Worksheet) Q_DECLARE_PRIVATE(Worksheet)
public: private:
friend class DocumentPrivate;
friend class Workbook;
friend class ::WorksheetTest;
Worksheet(const QString &sheetName, int sheetId, Workbook *book, CreateFlag flag);
Worksheet *copy(const QString &distName, int distId) const;
public:
~Worksheet();
public:
bool write(const CellReference &row_column, const QVariant &value, const Format &format=Format()); bool write(const CellReference &row_column, const QVariant &value, const Format &format=Format());
bool write(int row, int column, const QVariant &value, const Format &format=Format()); bool write(int row, int column, const QVariant &value, const Format &format=Format());
QVariant read(const CellReference &row_column) const; QVariant read(const CellReference &row_column) const;
QVariant read(int row, int column) const; QVariant read(int row, int column) const;
bool writeString(const CellReference &row_column, const QString &value, const Format &format=Format()); bool writeString(const CellReference &row_column, const QString &value, const Format &format=Format());
bool writeString(int row, int column, const QString &value, const Format &format=Format()); bool writeString(int row, int column, const QString &value, const Format &format=Format());
bool writeString(const CellReference &row_column, const RichString &value, const Format &format=Format()); bool writeString(const CellReference &row_column, const RichString &value, const Format &format=Format());
bool writeString(int row, int column, const RichString &value, const Format &format=Format()); bool writeString(int row, int column, const RichString &value, const Format &format=Format());
bool writeInlineString(const CellReference &row_column, const QString &value, const Format &format=Format()); bool writeInlineString(const CellReference &row_column, const QString &value, const Format &format=Format());
bool writeInlineString(int row, int column, const QString &value, const Format &format=Format()); bool writeInlineString(int row, int column, const QString &value, const Format &format=Format());
bool writeNumeric(const CellReference &row_column, double value, const Format &format=Format()); bool writeNumeric(const CellReference &row_column, double value, const Format &format=Format());
bool writeNumeric(int row, int column, double value, const Format &format=Format()); bool writeNumeric(int row, int column, double value, const Format &format=Format());
bool writeFormula(const CellReference &row_column, const CellFormula &formula, const Format &format=Format(), double result=0); bool writeFormula(const CellReference &row_column, const CellFormula &formula, const Format &format=Format(), double result=0);
bool writeFormula(int row, int column, const CellFormula &formula, const Format &format=Format(), double result=0); bool writeFormula(int row, int column, const CellFormula &formula, const Format &format=Format(), double result=0);
bool writeBlank(const CellReference &row_column, const Format &format=Format()); bool writeBlank(const CellReference &row_column, const Format &format=Format());
bool writeBlank(int row, int column, const Format &format=Format()); bool writeBlank(int row, int column, const Format &format=Format());
bool writeBool(const CellReference &row_column, bool value, const Format &format=Format()); bool writeBool(const CellReference &row_column, bool value, const Format &format=Format());
bool writeBool(int row, int column, bool value, const Format &format=Format()); bool writeBool(int row, int column, bool value, const Format &format=Format());
bool writeDateTime(const CellReference &row_column, const QDateTime& dt, const Format &format=Format()); bool writeDateTime(const CellReference &row_column, const QDateTime& dt, const Format &format=Format());
bool writeDateTime(int row, int column, const QDateTime& dt, const Format &format=Format()); bool writeDateTime(int row, int column, const QDateTime& dt, const Format &format=Format());
bool writeTime(const CellReference &row_column, const QTime& t, const Format &format=Format()); bool writeTime(const CellReference &row_column, const QTime& t, const Format &format=Format());
bool writeTime(int row, int column, const QTime& t, const Format &format=Format()); bool writeTime(int row, int column, const QTime& t, const Format &format=Format());
@ -98,6 +116,7 @@ public:
bool setColumnWidth(int colFirst, int colLast, double width); bool setColumnWidth(int colFirst, int colLast, double width);
bool setColumnFormat(int colFirst, int colLast, const Format &format); bool setColumnFormat(int colFirst, int colLast, const Format &format);
bool setColumnHidden(int colFirst, int colLast, bool hidden); bool setColumnHidden(int colFirst, int colLast, bool hidden);
double columnWidth(int column); double columnWidth(int column);
Format columnFormat(int column); Format columnFormat(int column);
bool isColumnHidden(int column); bool isColumnHidden(int column);
@ -137,18 +156,9 @@ public:
void setWhiteSpaceVisible(bool visible); void setWhiteSpaceVisible(bool visible);
bool setStartPage(int spagen); //add by liufeijin20181028 bool setStartPage(int spagen); //add by liufeijin20181028
~Worksheet();
QVector<CellLocation> getFullCells(int* maxRow, int* maxCol); QVector<CellLocation> getFullCells(int* maxRow, int* maxCol);
private: private:
friend class DocumentPrivate;
friend class Workbook;
friend class ::WorksheetTest;
Worksheet(const QString &sheetName, int sheetId, Workbook *book, CreateFlag flag);
Worksheet *copy(const QString &distName, int distId) const;
void saveToXmlFile(QIODevice *device) const; void saveToXmlFile(QIODevice *device) const;
bool loadFromXmlFile(QIODevice *device); bool loadFromXmlFile(QIODevice *device);
}; };

View File

@ -153,12 +153,18 @@ struct XlsxColumnInfo
bool collapsed; bool collapsed;
}; };
// #ifndef QMapIntSharedPointerCell
// typedef QMap<int, QSharedPointer<Cell> > QMapIntSharedPointerCell;
// #endif
class WorksheetPrivate : public AbstractSheetPrivate class WorksheetPrivate : public AbstractSheetPrivate
{ {
Q_DECLARE_PUBLIC(Worksheet) Q_DECLARE_PUBLIC(Worksheet)
public: public:
WorksheetPrivate(Worksheet *p, Worksheet::CreateFlag flag); WorksheetPrivate(Worksheet *p, Worksheet::CreateFlag flag);
~WorksheetPrivate(); ~WorksheetPrivate();
int checkDimensions(int row, int col, bool ignore_row=false, bool ignore_col=false); int checkDimensions(int row, int col, bool ignore_row=false, bool ignore_col=false);
Format cellFormat(int row, int col) const; Format cellFormat(int row, int col) const;
QString generateDimensionString() const; QString generateDimensionString() const;
@ -172,6 +178,7 @@ public:
void saveXmlHyperlinks(QXmlStreamWriter &writer) const; void saveXmlHyperlinks(QXmlStreamWriter &writer) const;
void saveXmlDrawings(QXmlStreamWriter &writer) const; void saveXmlDrawings(QXmlStreamWriter &writer) const;
void saveXmlDataValidations(QXmlStreamWriter &writer) const; void saveXmlDataValidations(QXmlStreamWriter &writer) const;
int rowPixelsSize(int row) const; int rowPixelsSize(int row) const;
int colPixelsSize(int col) const; int colPixelsSize(int col) const;
@ -190,7 +197,9 @@ public:
SharedStrings *sharedStrings() const; SharedStrings *sharedStrings() const;
public:
QMap<int, QMap<int, QSharedPointer<Cell> > > cellTable; QMap<int, QMap<int, QSharedPointer<Cell> > > cellTable;
QMap<int, QMap<int, QString> > comments; QMap<int, QMap<int, QString> > comments;
QMap<int, QMap<int, QSharedPointer<XlsxHyperlinkData> > > urlTable; QMap<int, QMap<int, QSharedPointer<XlsxHyperlinkData> > > urlTable;
QList<CellRange> merges; QList<CellRange> merges;
@ -215,9 +224,7 @@ public:
int default_row_height; int default_row_height;
bool default_row_zeroed; bool default_row_zeroed;
// liufeijin {{ // pagesetup and print settings add by liufeijin 20181028, liufeijin
// pagesetup and print settings add by liufeijin 20181028
QString PpaperSize; QString PpaperSize;
QString Pscale; QString Pscale;
QString PfirstPageNumber; QString PfirstPageNumber;
@ -228,7 +235,7 @@ public:
QString Prid; QString Prid;
QString Pcopies; QString Pcopies;
// pageMargins // pageMargins, liufeijin
QString PMheader; QString PMheader;
QString PMfooter; QString PMfooter;
QString PMtop; QString PMtop;
@ -236,12 +243,10 @@ public:
QString PMleft; QString PMleft;
QString PMright; QString PMright;
// header footer // header footer, liufeijin
QString MoodFooter; QString MoodFooter;
QString ModdHeader; QString ModdHeader;
// liufeijin }}
XlsxSheetFormatProps sheetFormatProps; XlsxSheetFormatProps sheetFormatProps;
bool windowProtection; bool windowProtection;
@ -256,7 +261,9 @@ public:
bool showWhiteSpace; bool showWhiteSpace;
QRegularExpression urlPattern; QRegularExpression urlPattern;
private: private:
static double calculateColWidth(int characters); static double calculateColWidth(int characters);
}; };

View File

@ -189,17 +189,27 @@ AbstractSheet *Workbook::addSheet(const QString &name, int sheetId, AbstractShee
Q_D(Workbook); Q_D(Workbook);
if (sheetId > d->last_sheet_id) if (sheetId > d->last_sheet_id)
d->last_sheet_id = sheetId; d->last_sheet_id = sheetId;
AbstractSheet *sheet=0;
if (type == AbstractSheet::ST_WorkSheet) { AbstractSheet *sheet = NULL;
if (type == AbstractSheet::ST_WorkSheet)
{
// create work sheet (value sheet)
sheet = new Worksheet(name, sheetId, this, F_LoadFromExists); sheet = new Worksheet(name, sheetId, this, F_LoadFromExists);
} else if (type == AbstractSheet::ST_ChartSheet) { }
else if (type == AbstractSheet::ST_ChartSheet)
{
// create chart sheet
sheet = new Chartsheet(name, sheetId, this, F_LoadFromExists); sheet = new Chartsheet(name, sheetId, this, F_LoadFromExists);
} else { }
else
{
qWarning("unsupported sheet type."); qWarning("unsupported sheet type.");
Q_ASSERT(false); Q_ASSERT(false);
} }
d->sheets.append(QSharedPointer<AbstractSheet>(sheet)); d->sheets.append(QSharedPointer<AbstractSheet>(sheet));
d->sheetNames.append(name); d->sheetNames.append(name);
return sheet; return sheet;
} }
@ -233,15 +243,26 @@ AbstractSheet *Workbook::insertSheet(int index, const QString &name, AbstractShe
} }
++d->last_sheet_id; ++d->last_sheet_id;
AbstractSheet *sheet;
AbstractSheet *sheet = NULL;
if ( type == AbstractSheet::ST_WorkSheet ) if ( type == AbstractSheet::ST_WorkSheet )
{
sheet = new Worksheet(sheetName, d->last_sheet_id, this, F_NewFromScratch); sheet = new Worksheet(sheetName, d->last_sheet_id, this, F_NewFromScratch);
else }
else if ( type == AbstractSheet::ST_ChartSheet )
{
sheet = new Chartsheet(sheetName, d->last_sheet_id, this, F_NewFromScratch); sheet = new Chartsheet(sheetName, d->last_sheet_id, this, F_NewFromScratch);
}
else
{
qWarning("unsupported sheet type.");
Q_ASSERT(false);
}
d->sheets.insert(index, QSharedPointer<AbstractSheet>(sheet)); d->sheets.insert(index, QSharedPointer<AbstractSheet>(sheet));
d->sheetNames.insert(index, sheetName); d->sheetNames.insert(index, sheetName);
d->activesheetIndex = index; d->activesheetIndex = index;
return sheet; return sheet;
} }

View File

@ -1,14 +1,4 @@
//-------------------------------------------------------------------- // xlsxworksheet.cpp
//
// QXlsx
// MIT License
// https://github.com/j2doll/QXlsx
//
// QtXlsx
// https://github.com/dbzhang800/QtXlsxWriter
// http://qtxlsx.debao.me/
// MIT License
//--------------------------------------------------------------------
#include <QtGlobal> #include <QtGlobal>
#include <QVariant> #include <QVariant>
@ -24,6 +14,7 @@
#include <QTextDocument> #include <QTextDocument>
#include <QDir> #include <QDir>
#include <QMapIterator> #include <QMapIterator>
#include <QMap>
#include <cmath> #include <cmath>
@ -2485,33 +2476,58 @@ QVector<CellLocation> Worksheet::getFullCells(int* maxRow, int* maxCol)
{ {
Q_D(const Worksheet); Q_D(const Worksheet);
// return values
(*maxRow) = -1; (*maxRow) = -1;
(*maxCol) = -1; (*maxCol) = -1;
QVector<CellLocation> ret; QVector<CellLocation> ret;
// QString privateName = d->name; // name of sheet (not object type)
// qDebug() << privateName ;
if ( d->type == AbstractSheet::ST_WorkSheet )
{
// use current sheet
}
else if ( d->type == AbstractSheet::ST_ChartSheet )
{
return ret;
}
else
{
qWarning("unsupported sheet type.");
Q_ASSERT(false);
return ret;
}
QMapIterator< int, QMap< int, QSharedPointer<Cell> > > _it( d->cellTable ); QMapIterator< int, QMap< int, QSharedPointer<Cell> > > _it( d->cellTable );
while ( _it.hasNext() ) while ( _it.hasNext() )
{ {
_it.next(); _it.next();
int keyI = _it.key(); // key int keyI = _it.key(); // key (cell row)
QMapIterator<int, QSharedPointer<Cell> > _iit( _it.value() ); // value QMapIterator<int, QSharedPointer<Cell> > _iit( _it.value() ); // value
while ( _iit.hasNext() ) while ( _iit.hasNext() )
{ {
_iit.next(); _iit.next();
int keyII = _iit.key(); // key int keyII = _iit.key(); // key (cell column)
QSharedPointer<Cell> ptrCell = _iit.value(); // value QSharedPointer<Cell> ptrCell = _iit.value(); // value
CellLocation cl; CellLocation cl;
cl.row = keyI; cl.row = keyI;
if ( keyI > (*maxRow) ) if ( keyI > (*maxRow) )
{
(*maxRow) = keyI; (*maxRow) = keyI;
}
cl.col = keyII; cl.col = keyII;
if ( keyII > (*maxCol) ) if ( keyII > (*maxCol) )
{
(*maxCol) = keyII; (*maxCol) = keyII;
}
cl.cell = ptrCell; cl.cell = ptrCell;

View File

@ -1,14 +1,8 @@
########################################################################## ##########################################################################
# TestExcel.pro # TestExcel.pro
# #
# QXlsx # QXlsx # MIT License # https://github.com/j2doll/QXlsx
# MIT License ## QtXlsx # MIT License # https://github.com/dbzhang800/QtXlsxWriter # http://qtxlsx.debao.me/
# https://github.com/j2doll/QXlsx
#
# QtXlsx
# https://github.com/dbzhang800/QtXlsxWriter
# http://qtxlsx.debao.me/
# MIT License
TARGET = TestExcel TARGET = TestExcel
TEMPLATE = app TEMPLATE = app

View File

@ -44,14 +44,11 @@ int main(int argc, char *argv[])
if ( argc == 2 ) if ( argc == 2 )
{ {
QString strArg1 = argv[1]; QString strArg1 = argv[1];
if ( strArg1 == QString("hello") ) if ( strArg1 == QString("hello") )
{ {
hello(); hello();
return 0; return 0;
} }
return 0;
} }
hello(); hello();