mirror of
https://github.com/QtExcel/QXlsx.git
synced 2025-01-16 04:42:53 +08:00
copycat is fixed (chartsheet)
This commit is contained in:
parent
342b885191
commit
e992a7e67f
@ -36,13 +36,13 @@ private slots:
|
||||
void on_action_Print_triggered();
|
||||
void print(QPrinter *printer);
|
||||
|
||||
private:
|
||||
protected:
|
||||
Ui::MainWindow *ui;
|
||||
QXlsx::Document* xlsxDoc;
|
||||
QTabWidget *tabWidget;
|
||||
QVector<XlsxTab*> xlsxTabList;
|
||||
|
||||
private:
|
||||
protected:
|
||||
bool loadXlsx(QString xlsxFilename);
|
||||
std::string convertFromNumberToExcelColumn(int n);
|
||||
};
|
||||
|
@ -33,9 +33,9 @@ XlsxTab::XlsxTab(QWidget* parent,
|
||||
Q_ASSERT( NULL != table );
|
||||
|
||||
// set layout
|
||||
QVBoxLayout *layout = new QVBoxLayout;
|
||||
layout->addWidget(table);
|
||||
this->setLayout(layout);
|
||||
vLayout = new QVBoxLayout;
|
||||
vLayout->addWidget(table);
|
||||
this->setLayout(vLayout);
|
||||
|
||||
document = ptrDoc; // set document
|
||||
sheet = ptrSheet; // set sheet data
|
||||
@ -48,10 +48,17 @@ XlsxTab::XlsxTab(QWidget* parent,
|
||||
|
||||
XlsxTab::~XlsxTab()
|
||||
{
|
||||
|
||||
if ( NULL != vLayout )
|
||||
{
|
||||
vLayout->deleteLater();
|
||||
}
|
||||
|
||||
if ( NULL != table )
|
||||
{
|
||||
table->deleteLater();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
bool XlsxTab::setSheet()
|
||||
@ -253,3 +260,40 @@ bool XlsxTab::setSheet()
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
@ -5,11 +5,14 @@
|
||||
|
||||
#include <cstdio>
|
||||
|
||||
#include <string>
|
||||
|
||||
#include <QtGlobal>
|
||||
#include <QObject>
|
||||
#include <QString>
|
||||
#include <QWidget>
|
||||
#include <QTableWidget>
|
||||
#include <QVBoxLayout>
|
||||
|
||||
#include "xlsx.h"
|
||||
#include "XlsxTableWidget.h"
|
||||
@ -31,13 +34,17 @@ public slots:
|
||||
signals:
|
||||
|
||||
protected:
|
||||
XlsxTableWidget* table;
|
||||
QXlsx::Document* document;
|
||||
QXlsx::AbstractSheet* sheet;
|
||||
int sheetIndex;
|
||||
|
||||
protected:
|
||||
XlsxTableWidget* table;
|
||||
QVBoxLayout *vLayout;
|
||||
|
||||
protected:
|
||||
bool setSheet();
|
||||
std::string convertFromNumberToExcelColumn(int n);
|
||||
|
||||
};
|
||||
|
||||
|
@ -11,7 +11,7 @@
|
||||
XlsxTableWidget::XlsxTableWidget(QWidget* parent)
|
||||
: QTableWidget(parent)
|
||||
{
|
||||
|
||||
//
|
||||
}
|
||||
|
||||
XlsxTableWidget::~XlsxTableWidget()
|
||||
@ -29,6 +29,8 @@ void XlsxTableWidget::mousePressEvent(QMouseEvent *event)
|
||||
|
||||
QList<QTableWidgetItem *> items = this->selectedItems();
|
||||
|
||||
// selected range
|
||||
|
||||
QList<QTableWidgetSelectionRange> ranges = this->selectedRanges();
|
||||
for (int ic = 0 ; ic < ranges.size(); ic++ )
|
||||
{
|
||||
@ -44,15 +46,18 @@ void XlsxTableWidget::mousePressEvent(QMouseEvent *event)
|
||||
int leftCol = range.leftColumn();
|
||||
int rightCol = range.rightColumn();
|
||||
|
||||
qDebug() << QTime::currentTime();
|
||||
qDebug()
|
||||
<< QTime::currentTime();
|
||||
|
||||
qDebug() << "row: " << rowCount << " : "
|
||||
<< " top:" << topRow
|
||||
<< ", bottom:" << bottomRow ;
|
||||
qDebug()
|
||||
<< "row: " << rowCount << " : "
|
||||
<< " top:" << topRow
|
||||
<< ", bottom:" << bottomRow ;
|
||||
|
||||
qDebug() << "col: " << colCount << " : "
|
||||
<< " left:" << leftCol
|
||||
<< " right:" << rightCol ;
|
||||
qDebug()
|
||||
<< "col: " << colCount << " : "
|
||||
<< " left:" << leftCol
|
||||
<< " right:" << rightCol ;
|
||||
|
||||
}
|
||||
|
||||
|
@ -36,19 +36,14 @@ class AbstractSheetPrivate;
|
||||
class AbstractSheet : public AbstractOOXmlFile
|
||||
{
|
||||
Q_DECLARE_PRIVATE(AbstractSheet)
|
||||
public:
|
||||
enum SheetType {
|
||||
ST_WorkSheet,
|
||||
ST_ChartSheet,
|
||||
ST_DialogSheet,
|
||||
ST_MacroSheet
|
||||
};
|
||||
|
||||
enum SheetState {
|
||||
SS_Visible,
|
||||
SS_Hidden,
|
||||
SS_VeryHidden
|
||||
};
|
||||
public:
|
||||
Workbook *workbook() const;
|
||||
|
||||
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;
|
||||
SheetType sheetType() const;
|
||||
@ -59,8 +54,6 @@ public:
|
||||
void setHidden(bool hidden);
|
||||
void setVisible(bool visible);
|
||||
|
||||
Workbook *workbook() const;
|
||||
|
||||
protected:
|
||||
friend class Workbook;
|
||||
AbstractSheet(const QString &sheetName, int sheetId, Workbook *book, AbstractSheetPrivate *d);
|
||||
|
@ -30,21 +30,24 @@
|
||||
#include <QSharedPointer>
|
||||
|
||||
QT_BEGIN_NAMESPACE_XLSX
|
||||
|
||||
class Workbook;
|
||||
class DocumentPrivate;
|
||||
class ChartsheetPrivate;
|
||||
class Chart;
|
||||
class Chartsheet : public AbstractSheet
|
||||
|
||||
class Chartsheet : public AbstractSheet
|
||||
{
|
||||
Q_DECLARE_PRIVATE(Chartsheet)
|
||||
public:
|
||||
|
||||
public:
|
||||
~Chartsheet();
|
||||
Chart *chart();
|
||||
|
||||
private:
|
||||
friend class DocumentPrivate;
|
||||
friend class Workbook;
|
||||
|
||||
Chartsheet(const QString &sheetName, int sheetId, Workbook *book, CreateFlag flag);
|
||||
Chartsheet *copy(const QString &distName, int distId) const;
|
||||
|
||||
|
@ -51,28 +51,46 @@ class Worksheet : public AbstractSheet
|
||||
{
|
||||
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(int row, int column, const QVariant &value, const Format &format=Format());
|
||||
|
||||
QVariant read(const CellReference &row_column) const;
|
||||
QVariant read(int row, int column) const;
|
||||
|
||||
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(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 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 writeNumeric(const CellReference &row_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(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(int row, int column, 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 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 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());
|
||||
|
||||
@ -98,6 +116,7 @@ public:
|
||||
bool setColumnWidth(int colFirst, int colLast, double width);
|
||||
bool setColumnFormat(int colFirst, int colLast, const Format &format);
|
||||
bool setColumnHidden(int colFirst, int colLast, bool hidden);
|
||||
|
||||
double columnWidth(int column);
|
||||
Format columnFormat(int column);
|
||||
bool isColumnHidden(int column);
|
||||
@ -137,18 +156,9 @@ public:
|
||||
void setWhiteSpaceVisible(bool visible);
|
||||
bool setStartPage(int spagen); //add by liufeijin20181028
|
||||
|
||||
~Worksheet();
|
||||
|
||||
QVector<CellLocation> getFullCells(int* maxRow, int* maxCol);
|
||||
|
||||
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;
|
||||
bool loadFromXmlFile(QIODevice *device);
|
||||
};
|
||||
|
@ -153,12 +153,18 @@ struct XlsxColumnInfo
|
||||
bool collapsed;
|
||||
};
|
||||
|
||||
class WorksheetPrivate : public AbstractSheetPrivate
|
||||
// #ifndef QMapIntSharedPointerCell
|
||||
// typedef QMap<int, QSharedPointer<Cell> > QMapIntSharedPointerCell;
|
||||
// #endif
|
||||
|
||||
class WorksheetPrivate : public AbstractSheetPrivate
|
||||
{
|
||||
Q_DECLARE_PUBLIC(Worksheet)
|
||||
|
||||
public:
|
||||
WorksheetPrivate(Worksheet *p, Worksheet::CreateFlag flag);
|
||||
~WorksheetPrivate();
|
||||
|
||||
int checkDimensions(int row, int col, bool ignore_row=false, bool ignore_col=false);
|
||||
Format cellFormat(int row, int col) const;
|
||||
QString generateDimensionString() const;
|
||||
@ -172,6 +178,7 @@ public:
|
||||
void saveXmlHyperlinks(QXmlStreamWriter &writer) const;
|
||||
void saveXmlDrawings(QXmlStreamWriter &writer) const;
|
||||
void saveXmlDataValidations(QXmlStreamWriter &writer) const;
|
||||
|
||||
int rowPixelsSize(int row) const;
|
||||
int colPixelsSize(int col) const;
|
||||
|
||||
@ -184,13 +191,15 @@ public:
|
||||
void loadXmlHyperlinks(QXmlStreamReader &reader);
|
||||
|
||||
QList<QSharedPointer<XlsxRowInfo> > getRowInfoList(int rowFirst, int rowLast);
|
||||
QList <QSharedPointer<XlsxColumnInfo> > getColumnInfoList(int colFirst, int colLast);
|
||||
QList<QSharedPointer<XlsxColumnInfo> > getColumnInfoList(int colFirst, int colLast);
|
||||
QList<int> getColumnIndexes(int colFirst, int colLast);
|
||||
bool isColumnRangeValid(int colFirst, int colLast);
|
||||
|
||||
SharedStrings *sharedStrings() const;
|
||||
|
||||
public:
|
||||
QMap<int, QMap<int, QSharedPointer<Cell> > > cellTable;
|
||||
|
||||
QMap<int, QMap<int, QString> > comments;
|
||||
QMap<int, QMap<int, QSharedPointer<XlsxHyperlinkData> > > urlTable;
|
||||
QList<CellRange> merges;
|
||||
@ -215,9 +224,7 @@ public:
|
||||
int default_row_height;
|
||||
bool default_row_zeroed;
|
||||
|
||||
// liufeijin {{
|
||||
|
||||
// pagesetup and print settings add by liufeijin 20181028
|
||||
// pagesetup and print settings add by liufeijin 20181028, liufeijin
|
||||
QString PpaperSize;
|
||||
QString Pscale;
|
||||
QString PfirstPageNumber;
|
||||
@ -228,7 +235,7 @@ public:
|
||||
QString Prid;
|
||||
QString Pcopies;
|
||||
|
||||
// pageMargins
|
||||
// pageMargins, liufeijin
|
||||
QString PMheader;
|
||||
QString PMfooter;
|
||||
QString PMtop;
|
||||
@ -236,12 +243,10 @@ public:
|
||||
QString PMleft;
|
||||
QString PMright;
|
||||
|
||||
// header footer
|
||||
// header footer, liufeijin
|
||||
QString MoodFooter;
|
||||
QString ModdHeader;
|
||||
|
||||
// liufeijin }}
|
||||
|
||||
XlsxSheetFormatProps sheetFormatProps;
|
||||
|
||||
bool windowProtection;
|
||||
@ -256,7 +261,9 @@ public:
|
||||
bool showWhiteSpace;
|
||||
|
||||
QRegularExpression urlPattern;
|
||||
|
||||
private:
|
||||
|
||||
static double calculateColWidth(int characters);
|
||||
};
|
||||
|
||||
|
@ -189,17 +189,27 @@ AbstractSheet *Workbook::addSheet(const QString &name, int sheetId, AbstractShee
|
||||
Q_D(Workbook);
|
||||
if (sheetId > d->last_sheet_id)
|
||||
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);
|
||||
} else if (type == AbstractSheet::ST_ChartSheet) {
|
||||
}
|
||||
else if (type == AbstractSheet::ST_ChartSheet)
|
||||
{
|
||||
// create chart sheet
|
||||
sheet = new Chartsheet(name, sheetId, this, F_LoadFromExists);
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
qWarning("unsupported sheet type.");
|
||||
Q_ASSERT(false);
|
||||
}
|
||||
|
||||
d->sheets.append(QSharedPointer<AbstractSheet>(sheet));
|
||||
d->sheetNames.append(name);
|
||||
|
||||
return sheet;
|
||||
}
|
||||
|
||||
@ -233,15 +243,26 @@ AbstractSheet *Workbook::insertSheet(int index, const QString &name, AbstractShe
|
||||
}
|
||||
|
||||
++d->last_sheet_id;
|
||||
AbstractSheet *sheet;
|
||||
if (type == AbstractSheet::ST_WorkSheet)
|
||||
|
||||
AbstractSheet *sheet = NULL;
|
||||
if ( type == AbstractSheet::ST_WorkSheet )
|
||||
{
|
||||
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);
|
||||
}
|
||||
else
|
||||
{
|
||||
qWarning("unsupported sheet type.");
|
||||
Q_ASSERT(false);
|
||||
}
|
||||
|
||||
d->sheets.insert(index, QSharedPointer<AbstractSheet>(sheet));
|
||||
d->sheetNames.insert(index, sheetName);
|
||||
d->activesheetIndex = index;
|
||||
|
||||
return sheet;
|
||||
}
|
||||
|
||||
|
@ -1,14 +1,4 @@
|
||||
//--------------------------------------------------------------------
|
||||
//
|
||||
// QXlsx
|
||||
// MIT License
|
||||
// https://github.com/j2doll/QXlsx
|
||||
//
|
||||
// QtXlsx
|
||||
// https://github.com/dbzhang800/QtXlsxWriter
|
||||
// http://qtxlsx.debao.me/
|
||||
// MIT License
|
||||
//--------------------------------------------------------------------
|
||||
// xlsxworksheet.cpp
|
||||
|
||||
#include <QtGlobal>
|
||||
#include <QVariant>
|
||||
@ -24,6 +14,7 @@
|
||||
#include <QTextDocument>
|
||||
#include <QDir>
|
||||
#include <QMapIterator>
|
||||
#include <QMap>
|
||||
|
||||
#include <cmath>
|
||||
|
||||
@ -2485,33 +2476,58 @@ QVector<CellLocation> Worksheet::getFullCells(int* maxRow, int* maxCol)
|
||||
{
|
||||
Q_D(const Worksheet);
|
||||
|
||||
// return values
|
||||
(*maxRow) = -1;
|
||||
(*maxCol) = -1;
|
||||
QVector<CellLocation> ret;
|
||||
|
||||
QMapIterator<int, QMap<int, QSharedPointer<Cell> > > _it(d->cellTable);
|
||||
// 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 );
|
||||
|
||||
while ( _it.hasNext() )
|
||||
{
|
||||
_it.next();
|
||||
|
||||
int keyI = _it.key(); // key
|
||||
int keyI = _it.key(); // key (cell row)
|
||||
QMapIterator<int, QSharedPointer<Cell> > _iit( _it.value() ); // value
|
||||
|
||||
while ( _iit.hasNext() )
|
||||
{
|
||||
_iit.next();
|
||||
|
||||
int keyII = _iit.key(); // key
|
||||
int keyII = _iit.key(); // key (cell column)
|
||||
QSharedPointer<Cell> ptrCell = _iit.value(); // value
|
||||
|
||||
CellLocation cl;
|
||||
|
||||
cl.row = keyI;
|
||||
if ( keyI > (*maxRow) )
|
||||
{
|
||||
(*maxRow) = keyI;
|
||||
}
|
||||
|
||||
cl.col = keyII;
|
||||
if ( keyII > (*maxCol) )
|
||||
{
|
||||
(*maxCol) = keyII;
|
||||
}
|
||||
|
||||
cl.cell = ptrCell;
|
||||
|
||||
|
@ -1,14 +1,8 @@
|
||||
##########################################################################
|
||||
# TestExcel.pro
|
||||
#
|
||||
# QXlsx
|
||||
# MIT License
|
||||
# https://github.com/j2doll/QXlsx
|
||||
#
|
||||
# QtXlsx
|
||||
# https://github.com/dbzhang800/QtXlsxWriter
|
||||
# http://qtxlsx.debao.me/
|
||||
# MIT License
|
||||
# QXlsx # MIT License # https://github.com/j2doll/QXlsx
|
||||
## QtXlsx # MIT License # https://github.com/dbzhang800/QtXlsxWriter # http://qtxlsx.debao.me/
|
||||
|
||||
TARGET = TestExcel
|
||||
TEMPLATE = app
|
||||
|
@ -44,14 +44,11 @@ int main(int argc, char *argv[])
|
||||
if ( argc == 2 )
|
||||
{
|
||||
QString strArg1 = argv[1];
|
||||
|
||||
if ( strArg1 == QString("hello") )
|
||||
{
|
||||
hello();
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
hello();
|
||||
|
Loading…
x
Reference in New Issue
Block a user