-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainwindow.cpp
122 lines (100 loc) · 4.19 KB
/
mainwindow.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include "mainwindow.h"
#include "ui_mainwindow.h"
//#include "xlsxcellrange.h"
//#include "xlsxchart.h"
//#include "xlsxrichstring.h"
#include "xlsxworkbook.h"
//Подключаем библиотеку для Excel
#include "xlsxdocument.h"
//#include "xlsxchartsheet.h"
using namespace QXlsx;
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
//Устанавливаем название основного окна
setWindowTitle("Считывание EXCEL файлов в базу");
//Подключаемся к базе
//db = QSqlDatabase::addDatabase("QSQLITE");
//db.setDatabaseName("./../readExcelPeople/readexcel/readexcel.db");
//Создаем объект окна сообщений
msgBox = new QMessageBox(this);
msgBox->setIcon(QMessageBox::Information);
//Проверяем подключение и выводим сообщение
//if (db.open()) {
//ui->statusbar->showMessage("Вы успешно подключены к базе данных: " + db.databaseName());
//Выводим таблицу в окно приложения
/*modelT = new QSqlTableModel(this, db);
modelT->setTable("personnels");
modelT->select();
ui->tableView->setModel(modelT);
}else{
ui->statusbar->showMessage("При подключении к базе произошла ошибка: " + db.lastError().databaseText());*/
}
//}
MainWindow::~MainWindow()
{
delete ui;
}
//Закрыть приложение в меню
void MainWindow::on_exit_triggered(){close();}
//Открываем файл с компьютера через кнопку
//Читаем Excel файл
int sheetIndexNumber = 0;
void MainWindow::on_btnFOpen_clicked()
{
filePath = QFileDialog::getOpenFileName(this, "Выбрать файл", " ", "*.xls, *.xlsx");//Задаем название диалогового окна и параметры открываемого файла
if(filePath.isEmpty()) {//Проверяем на пустоту и выводим сообщение
msgBox->setWindowTitle("ВНИМАНИЕ!");
msgBox->setText("<b>Не выбран файл!</b>");
msgBox->exec();
}else{
ui->textLabel->setText("Открыт файл: " + filePath);
ui->statusbar->clearMessage();
ui->statusbar->showMessage(filePath);
}
QXlsx::Document xlsx;
Document doc(filePath);
foreach(QString currentSheetName, doc.sheetNames()){
AbstractSheet* currentSheet = doc.sheet(currentSheetName);
if(NULL == currentSheet)
continue;
int maxRow = -1;
int maxCol = -1;
currentSheet->workbook()->setActiveSheet(sheetIndexNumber);
Worksheet* wsheet = (Worksheet*) currentSheet->workbook()->activeSheet();
if(NULL == wsheet)
continue;
QString strSheetName = wsheet->sheetName();
qDebug() << strSheetName;
QVector<CellLocation> clList = wsheet->getFullCells(&maxRow, &maxCol);
QVector<QVector<QString>> cellValues;
for (int rc = 0; rc < maxRow; rc++){
QVector<QString> tempValue;
for(int cc = 0; cc < maxCol; cc++){
tempValue.push_back(QString(""));
}
cellValues.push_back(tempValue);
}
for (int ic = 0; ic < clList.size(); ++ic){
CellLocation cl = clList.at(ic);
int row = cl.row - 1;
int col = cl.col - 1;
// QSharedPointer<Cell> ptrCell = cl.cell;
QVariant var = cl.cell->dateTime();// .data()->value();
QString str = var.toString();
cellValues[row][col] = str;
}
for(int rc = 0; rc < maxRow; rc++){
for(int cc = 0; cc < maxCol; cc++){
QString strCell = cellValues[rc][cc];
qDebug() << "( row :" << rc
<< ", col :" << cc
<< ") " << strCell;
}
}
}
}
//Закрыть приложение кнопкой
void MainWindow::on_btnClose_clicked(){close();}