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

update copycat example

This commit is contained in:
Jay Two 2018-09-28 21:20:29 +09:00
parent 85e5bde6d8
commit edf7078713
10 changed files with 68 additions and 40 deletions

View File

@ -20,23 +20,22 @@ MainWindow::MainWindow(QWidget *parent) :
tabWidget = new QTabWidget(this); tabWidget = new QTabWidget(this);
setCentralWidget(tabWidget); setCentralWidget(tabWidget);
this->setWindowTitle(QString("Copycat"));
} }
MainWindow::~MainWindow() MainWindow::~MainWindow()
{ {
// tabWidget->close();
delete ui; delete ui;
if ( NULL != xlsxDoc ) if ( NULL != xlsxDoc )
{ {
delete xlsxDoc; delete xlsxDoc;
} }
} }
void MainWindow::on_action_Quit_triggered() void MainWindow::on_action_Quit_triggered()
{ {
// quit
this->close(); this->close();
} }
@ -65,7 +64,7 @@ bool MainWindow::loadXlsx(QString fileName)
QXlsx::Document xlsxTmp(fileName); QXlsx::Document xlsxTmp(fileName);
if (!xlsxTmp.isLoadPackage()) if (!xlsxTmp.isLoadPackage())
{ {
return false; return false; // failed to load
} }
// clear xlsxDoc // clear xlsxDoc
@ -84,13 +83,10 @@ bool MainWindow::loadXlsx(QString fileName)
// Removes all the pages, but does not delete them. // Removes all the pages, but does not delete them.
// Calling this function is equivalent to calling removeTab() // Calling this function is equivalent to calling removeTab()
// until the tab widget is empty. // until the tab widget is empty.
//
// for ( int ic = 0 ; ic < tabWidget->count() ; ic++ ) {
// tabWidget->removeTab( ic );
// }
// clear sub-items of every tab // clear sub-items of every tab
foreach ( XlsxTab* ptrTab, xlsxTabList ) { foreach ( XlsxTab* ptrTab, xlsxTabList )
{
if ( NULL == ptrTab ) if ( NULL == ptrTab )
continue; continue;
delete ptrTab; delete ptrTab;
@ -118,6 +114,23 @@ bool MainWindow::loadXlsx(QString fileName)
void MainWindow::on_action_About_triggered() void MainWindow::on_action_About_triggered()
{ {
QMessageBox msgBox; QMessageBox msgBox;
msgBox.setText(QString("Copycat\nhttps://github.com/j2doll/QXlsx")); msgBox.setText( "QXlsx<br />"
"<a href=\"https://github.com/j2doll/QXlsx\">https://github.com/j2doll/QXlsx</a><br />"
"MIT License<br />" );
msgBox.exec(); msgBox.exec();
} }
void MainWindow::on_action_New_triggered()
{
QMessageBox msgBox;
msgBox.setText( "New" );
msgBox.exec();
}
void MainWindow::on_action_Save_triggered()
{
QMessageBox msgBox;
msgBox.setText( "Save" );
msgBox.exec();
}

View File

@ -31,6 +31,10 @@ private slots:
void on_action_About_triggered(); void on_action_About_triggered();
void on_action_New_triggered();
void on_action_Save_triggered();
private: private:
Ui::MainWindow *ui; Ui::MainWindow *ui;
QXlsx::Document* xlsxDoc; QXlsx::Document* xlsxDoc;

View File

@ -6,8 +6,8 @@
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>400</width> <width>480</width>
<height>300</height> <height>320</height>
</rect> </rect>
</property> </property>
<property name="windowTitle"> <property name="windowTitle">
@ -19,7 +19,7 @@
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>400</width> <width>480</width>
<height>21</height> <height>21</height>
</rect> </rect>
</property> </property>

View File

@ -27,7 +27,10 @@ XlsxTab::XlsxTab(QWidget* parent,
sheet = ptrSheet; // set sheet data sheet = ptrSheet; // set sheet data
sheetIndex = SheetIndex; // set shett index sheetIndex = SheetIndex; // set shett index
setSheet(); if ( ! setSheet() )
{
}
} }
} }
@ -48,40 +51,36 @@ bool XlsxTab::setSheet()
if ( NULL == table ) if ( NULL == table )
return false; return false;
// set active sheet
sheet->workbook()->setActiveSheet( sheetIndex ); sheet->workbook()->setActiveSheet( sheetIndex );
Worksheet* wsheet = (Worksheet*) sheet->workbook()->activeSheet(); Worksheet* wsheet = (Worksheet*) sheet->workbook()->activeSheet();
if ( NULL == wsheet ) if ( NULL == wsheet )
return false; return false;
qDebug() << wsheet->sheetName() << "----------"; // get full cells of sheet
// TODO: get max row count and column count of sheet
table->setRowCount(100);
table->setColumnCount(100);
int maxRow = -1; int maxRow = -1;
int maxCol = -1; int maxCol = -1;
QVector<CellLocation> clList = wsheet->getFullCells(); QVector<CellLocation> clList = wsheet->getFullCells( &maxRow, &maxCol );
for (int ic = 0; ic < clList.size(); ++ic) {
// set max count of row,col
table->setRowCount( maxRow );
table->setColumnCount( maxCol );
for ( int ic = 0; ic < clList.size(); ++ic )
{
CellLocation cl = clList.at(ic); CellLocation cl = clList.at(ic);
// First cell of tableWidget is 0.
// But first cell of Qxlsx document is 1.
int row = cl.row - 1; int row = cl.row - 1;
if ( row > maxRow ) maxRow = row;
int col = cl.col - 1; int col = cl.col - 1;
if ( col > maxCol ) maxCol = col;
QVariant var = cl.cell.data()->value(); QVariant var = cl.cell.data()->value();
QString str = var.toString(); QString str = var.toString();
qDebug() << " row:" << row << " col:" << col << " val:"<< str;
QTableWidgetItem *newItem = new QTableWidgetItem(str); QTableWidgetItem *newItem = new QTableWidgetItem(str);
table->setItem(row, col, newItem); table->setItem(row, col, newItem);
} }
table->setRowCount(maxRow + 1);
table->setColumnCount(maxCol + 1);
return true; return true;
} }

View File

@ -12,5 +12,6 @@ int main(int argc, char *argv[])
MainWindow w; MainWindow w;
w.show(); w.show();
return a.exec(); int ret = a.exec();
return ret;
} }

View File

@ -138,7 +138,7 @@ public:
~Worksheet(); ~Worksheet();
QVector<CellLocation> getFullCells(); QVector<CellLocation> getFullCells(int* maxRow, int* maxCol);
private: private:

View File

@ -2373,10 +2373,12 @@ SharedStrings *WorksheetPrivate::sharedStrings() const
return workbook->sharedStrings(); return workbook->sharedStrings();
} }
QVector<CellLocation> Worksheet::getFullCells() QVector<CellLocation> Worksheet::getFullCells(int* maxRow, int* maxCol)
{ {
Q_D(const Worksheet); Q_D(const Worksheet);
(*maxRow) = -1;
(*maxCol) = -1;
QVector<CellLocation> ret; QVector<CellLocation> ret;
QMapIterator<int, QMap<int, QSharedPointer<Cell> > > _it(d->cellTable); QMapIterator<int, QMap<int, QSharedPointer<Cell> > > _it(d->cellTable);
@ -2394,8 +2396,15 @@ QVector<CellLocation> Worksheet::getFullCells()
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) )
(*maxRow) = keyI;
cl.col = keyII; cl.col = keyII;
if ( keyII > (*maxCol) )
(*maxCol) = keyII;
cl.cell = ptrCell; cl.cell = ptrCell;
ret.push_back( cl ); ret.push_back( cl );

View File

@ -14,9 +14,10 @@
## 활용하는 방법 ## 활용하는 방법
- 예제를 보세요. - 예제를 보세요.
- TestExcel : 기본 예제 (QtXlsx 예제 기반) - TestExcel : 기본 예제 (QtXlsx 예제 기반)
- HelloAndroid : 안드로이드에서 xlsx 파일 열기 - HelloAndroid : QML을 사용한 안드로이드에서 xlsx 파일 열기
![](markdown.data/android.jpg)
- Copycat : xlsx 파일을 읽어 위젯으로 표시 (할일: xlsx 파일로 저장) - Copycat : xlsx 파일을 읽어 위젯으로 표시 (할일: xlsx 파일로 저장)
- SpreadsheetExample : 간단한 예제 ![](markdown.data/copycat.png)
## 활용 예제 (Hello excel) ## 활용 예제 (Hello excel)

View File

@ -12,13 +12,14 @@
- You don't need to use static library or dynamic shared object using QXlsx. - You don't need to use static library or dynamic shared object using QXlsx.
## How to use ## How to use
- See samples - See examples
- TestExcel : basic samples based on QtXlsx samples - TestExcel : basic samples based on QtXlsx samples
- HelloAndroid : read xlsx on Android - HelloAndroid : read xlsx on Android using QML
![](markdown.data/android.jpg)
- Copycat : load xlsx file and display on widget (TODO: save xlsx) - Copycat : load xlsx file and display on widget (TODO: save xlsx)
- SpreadsheetExample : simple sample ![](markdown.data/copycat.png)
## Sample (Hello excel) ## Example (Hello excel)
### :one: Writing excel file(*.xlsx) ### :one: Writing excel file(*.xlsx)

BIN
markdown.data/copycat.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.1 KiB