Qt讀取/寫入Excel資料--QAxObject
阿新 • • 發佈:2019-02-16
效果圖:
QT += core gui axcontainer
CONFIG += qaxcontainer
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = fastReadExcelInWindows
TEMPLATE = app
SOURCES += main.cpp\
MainWindow.cpp \
ExcelBase.cpp \
QVariantListListModel.cpp
HEADERS += MainWindow.h \
QVariantListListModel.h \
ExcelBase.h
FORMS += MainWindow.ui
//讀取
void MainWindow::getContents(QTableView **view , QStandardItemModel **model, int sheet_num, QString sheet_name)
{
QAxObject *work_sheet = work_book->querySubObject("Sheets(int)", sheet_num);
QAxObject *used_range = work_sheet->querySubObject("UsedRange" );
QAxObject *rows = used_range->querySubObject("Rows");
QAxObject *columns = used_range->querySubObject("Columns");
int row_start = used_range->property("Row").toInt(); //獲取起始行
int column_start = used_range->property("Column").toInt(); //獲取起始列
int row_count = rows->property("Count" ).toInt(); //獲取行數
int column_count = columns->property("Count").toInt(); //獲取列數
(*model) = new QStandardItemModel(row_count-1, column_count);
for(int i=row_start; i<=row_count; ++i)
{
for(int j=column_start; j<=column_count; ++j)
{
QAxObject *cell = work_sheet->querySubObject("Cells(int,int)", i, j);
//QVariant cell_value = cell->property("Value"); //獲取單元格內容
QString cell_value = cell->property("Value2").toString(); //獲取單元格內容
if( i == 1 )
(*model)->setHeaderData(j-1, Qt::Horizontal, cell_value);//將表的列名,放入model的列名中
else
(*model)->setData((*model)->index(i-2, j-1, QModelIndex()), cell_value);
}
}
(*view) = new QTableView;
(*view)->verticalHeader()->setVisible(false);
(*view)->setEditTriggers(QAbstractItemView::NoEditTriggers);
(*view)->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
(*view)->setAttribute(Qt::WA_DeleteOnClose);//設定在view控制元件close的時候自動delete
(*view)->setModel(*model);//將model與view關聯
(*view)->setWindowIcon(windowIcon());//設定view圖示
mdiArea->addSubWindow(*view);//將view加入mdiArea,以便view可以成為子視窗
(*view)->setWindowTitle(sheet_name);
(*view)->show();//將view顯示到螢幕
}
//寫入
void MainWindow::on_save_clicked()
{
if ( !result_order->text().isEmpty())
{
QString fileName = QFileDialog::getSaveFileName(this, "儲存評估結果", dirPath, "Excel 97-2003 工作簿(*.xls)");
fileName.replace("/","\\");
QAxObject excel("Excel.Application");
QAxObject * workbooks = excel.querySubObject("WorkBooks");
workbooks->dynamicCall("Add");
QAxObject * workbook = excel.querySubObject("ActiveWorkBook");
QAxObject * work_sheet = workbook->querySubObject("Sheets(int)", 1);
QAxObject *cell_1_1 = work_sheet->querySubObject("Cells(int,int)", 1, 1);
cell_1_1->setProperty("Value2", "流域綜合評價指數");
QAxObject *cell_2_1 = work_sheet->querySubObject("Cells(int,int)", 2, 1);
cell_2_1->setProperty("Value2", "流域綜合評價等級");
QAxObject *cell_3_1 = work_sheet->querySubObject("Cells(int,int)", 1, 4);
cell_3_1->setProperty("Value2", result_count->text().toStdString().data());
QAxObject *cell_2_2 = work_sheet->querySubObject("Cells(int,int)", 2, 4);
cell_2_2->setProperty("Value2", result_order->text().toStdString().data());
QAxObject *cell_5_1 = work_sheet->querySubObject("Cells(int,int)", 5, 1);
cell_5_1->setProperty("Value2", "生態效益指數");
QAxObject *cell_6_1 = work_sheet->querySubObject("Cells(int,int)", 6, 1);
cell_6_1->setProperty("Value2", "經濟效益指數");
QAxObject *cell_7_1 = work_sheet->querySubObject("Cells(int,int)", 7, 1);
cell_7_1->setProperty("Value2", "社會效益指數");
QAxObject *cell_5_3 = work_sheet->querySubObject("Cells(int,int)", 5, 3);
cell_5_3->setProperty("Value2", result_data[0]->text().toStdString().data());
QAxObject *cell_6_3 = work_sheet->querySubObject("Cells(int,int)", 6, 3);
cell_6_3->setProperty("Value2", result_data[2]->text().toStdString().data());
QAxObject *cell_7_3 = work_sheet->querySubObject("Cells(int,int)", 7, 3);
cell_7_3->setProperty("Value2", result_data[4]->text().toStdString().data());
QAxObject *cell_5_4 = work_sheet->querySubObject("Cells(int,int)", 5, 4);
cell_5_4->setProperty("Value2", result_data[1]->text().toStdString().data());
QAxObject *cell_6_4 = work_sheet->querySubObject("Cells(int,int)", 6, 4);
cell_6_4->setProperty("Value2", result_data[3]->text().toStdString().data());
QAxObject *cell_7_4 = work_sheet->querySubObject("Cells(int,int)", 7, 4);
cell_7_4->setProperty("Value2", result_data[5]->text().toStdString().data());
QAxObject *cell_9_1 = work_sheet->querySubObject("Cells(int,int)", 9, 1);
cell_9_1->setProperty("Value2", "植被恢復");
QAxObject *cell_10_1 = work_sheet->querySubObject("Cells(int,int)", 10, 1);
cell_10_1->setProperty("Value2", "水土保持");
QAxObject *cell_11_1 = work_sheet->querySubObject("Cells(int,int)", 11, 1);
cell_11_1->setProperty("Value2", "固碳釋氧");
QAxObject *cell_12_1 = work_sheet->querySubObject("Cells(int,int)", 12, 1);
cell_12_1->setProperty("Value2", "經濟收入");
QAxObject *cell_13_1 = work_sheet->querySubObject("Cells(int,int)", 13, 1);
cell_13_1->setProperty("Value2", "生產效率");
QAxObject *cell_14_1 = work_sheet->querySubObject("Cells(int,int)", 14, 1);
cell_14_1->setProperty("Value2", "糧食安全");
QAxObject *cell_15_1 = work_sheet->querySubObject("Cells(int,int)", 15, 1);
cell_15_1->setProperty("Value2", "社會影響");
QAxObject *cell_16_1 = work_sheet->querySubObject("Cells(int,int)", 16, 1);
cell_16_1->setProperty("Value2", "產業結構");
QAxObject *cell_9_4 = work_sheet->querySubObject("Cells(int,int)", 9, 4);
cell_9_4->setProperty("Value2", itemlabel[0]->text().toStdString().data());
QAxObject *cell_10_4 = work_sheet->querySubObject("Cells(int,int)", 10, 4);
cell_10_4->setProperty("Value2", itemlabel[1]->text().toStdString().data());
QAxObject *cell_11_4 = work_sheet->querySubObject("Cells(int,int)", 11, 4);
cell_11_4->setProperty("Value2", itemlabel[2]->text().toStdString().data());
QAxObject *cell_12_4 = work_sheet->querySubObject("Cells(int,int)", 12, 4);
cell_12_4->setProperty("Value2", itemlabel[3]->text().toStdString().data());
QAxObject *cell_13_4 = work_sheet->querySubObject("Cells(int,int)", 13, 4);
cell_13_4->setProperty("Value2", itemlabel[4]->text().toStdString().data());
QAxObject *cell_14_4 = work_sheet->querySubObject("Cells(int,int)", 14, 4);
cell_14_4->setProperty("Value2", itemlabel[5]->text().toStdString().data());
QAxObject *cell_15_4 = work_sheet->querySubObject("Cells(int,int)", 15, 4);
cell_15_4->setProperty("Value2", itemlabel[6]->text().toStdString().data());
QAxObject *cell_16_4 = work_sheet->querySubObject("Cells(int,int)", 16, 4);
cell_16_4->setProperty("Value2", itemlabel[7]->text().toStdString().data());
workbook->dynamicCall("SaveAs (const QString&,int,const QString&,const QString&,bool,bool)",
fileName,56,QString(""),QString(""),false,false);
workbook->dynamicCall("Close (Boolean)", false);
excel.dynamicCall("Quit (void)");
if (!fileName.isEmpty())
QMessageBox::information(NULL, "提示", "儲存成功!");
}else
{
QMessageBox::information(this, "提示","沒有報告結果,不可以匯出!");
}
}