通常情況下,我們在開發過程中比較常用的方法是將表格的數據到處到excel文件。我也在這個點上頭疼了很長時間,不過功夫不負苦心人,最終還是勉強達到效果,為了后面再次用到時不手忙腳亂現在將方法寄存在此,如果有人需要也可以借鑒;
注意:由於在qt導出的過程中分為QTableWidget導出文件到excel和QTableView導出文件到excel兩個部分,所以在看這個筆記的的時候需要注意主方法程序中的注釋部分,已經具體說明了部分代碼的適用情況;
首先在 .pro文件中增加一個配置文件,如下:
1 CONFIG += qaxcontainer #導出excel
第二步,在實現導出功能方法的 .cpp 文件中引入如下類:
1 #include <QTableWidget> 2 #include <QFileDialog> 3 #include <QDesktopServices> 4 #include <QMessageBox> 5 #include <QAxObject>
第三步,是真正的實現方法,以上所做只是為這個方法做出的鋪墊,具體如下:
1 //第一個參數是頁面上的表格,第二個是導出文件的表頭數據 2 void FaJianDialog::Table2ExcelByHtml(QTableWidget *table,QString title) 3 { 4 QString fileName = QFileDialog::getSaveFileName(table, "保存",QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation),"Excel 文件(*.xls *.xlsx)"); 5 if (fileName!="") 6 { 7 QAxObject *excel = new QAxObject; 8 if (excel->setControl("Excel.Application")) //連接Excel控件 9 { 10 excel->dynamicCall("SetVisible (bool Visible)","false");//不顯示窗體 11 excel->setProperty("DisplayAlerts", false);//不顯示任何警告信息。如果為true那么在關閉是會出現類似“文件已修改,是否保存”的提示 12 QAxObject *workbooks = excel->querySubObject("WorkBooks");//獲取工作簿集合 13 workbooks->dynamicCall("Add");//新建一個工作簿 14 QAxObject *workbook = excel->querySubObject("ActiveWorkBook");//獲取當前工作簿 15 QAxObject *worksheet = workbook->querySubObject("Worksheets(int)", 1); 16 17 int i,j; 18 //QTablewidget 獲取數據的列數 19 int colcount=table->columnCount(); 20 //QTablewidget 獲取數據的行數 21 int rowscount=table->rowCount() 22 23 //QTableView 獲取列數 24 //int colount=ui->tableview->model->columnCount(); 25 //QTableView 獲取行數 26 //int rowcount=ui->tableview->model->rowCount(); 27 28 QAxObject *cell,*col; 29 //標題行 30 cell=worksheet->querySubObject("Cells(int,int)", 1, 1); 31 cell->dynamicCall("SetValue(const QString&)", title); 32 cell->querySubObject("Font")->setProperty("Size", 18); 33 //調整行高 34 worksheet->querySubObject("Range(const QString&)", "1:1")->setProperty("RowHeight", 30); 35 //合並標題行 36 QString cellTitle; 37 cellTitle.append("A1:"); 38 cellTitle.append(QChar(colcount - 1 + 'A')); 39 cellTitle.append(QString::number(1)); 40 QAxObject *range = worksheet->querySubObject("Range(const QString&)", cellTitle); 41 range->setProperty("WrapText", true); 42 range->setProperty("MergeCells", true); 43 range->setProperty("HorizontalAlignment", -4108);//xlCenter 44 range->setProperty("VerticalAlignment", -4108);//xlCenter 45 //列標題 46 for(i=0;i<colcount;i++) 47 { 48 QString columnName; 49 columnName.append(QChar(i + 'A')); 50 columnName.append(":"); 51 columnName.append(QChar(i + 'A')); 52 col = worksheet->querySubObject("Columns(const QString&)", columnName); 53 col->setProperty("ColumnWidth", table->columnWidth(i)/6); 54 cell=worksheet->querySubObject("Cells(int,int)", 2, i+1); 55 //QTableWidget 獲取表格頭部文字信息 56 columnName=table->horizontalHeaderItem(i)->text(); 57 //QTableView 獲取表格頭部文字信息 58 // columnName=ui->tableView_right->model()->headerData(i,Qt::Horizontal,Qt::DisplayRole).toString(); 59 cell->dynamicCall("SetValue(const QString&)", columnName); 60 cell->querySubObject("Font")->setProperty("Bold", true); 61 cell->querySubObject("Interior")->setProperty("Color",QColor(191, 191, 191)); 62 cell->setProperty("HorizontalAlignment", -4108);//xlCenter 63 cell->setProperty("VerticalAlignment", -4108);//xlCenter 64 } 65 66 //數據區 67 68 //QTableWidget 獲取表格數據部分 69 for(i=0;i<rowcount;i++){ 70 for (j=0;j<colcount;j++) 71 { 72 worksheet->querySubObject("Cells(int,int)", i+3, j+1)->dynamicCall("SetValue(const QString&)", table->item(i,j)?table->item(i,j)->text():""); 73 } 74 } 75 76 77 //QTableView 獲取表格數據部分 78 // for(i=0;i<rowcount;i++) //行數 79 // { 80 // for (j=0;j<colcount;j++) //列數 81 // { 82 // QModelIndex index = ui->tableView_right->model()->index(i, j); 83 // QString strdata=ui->tableView_right->model()->data(index).toString(); 84 // worksheet->querySubObject("Cells(int,int)", i+3, j+1)->dynamicCall("SetValue(const QString&)", strdata); 85 // } 86 // } 87 88 89 //畫框線 90 QString lrange; 91 lrange.append("A2:"); 92 lrange.append(colcount - 1 + 'A'); 93 lrange.append(QString::number(table->rowCount() + 2)); 94 range = worksheet->querySubObject("Range(const QString&)", lrange); 95 range->querySubObject("Borders")->setProperty("LineStyle", QString::number(1)); 96 range->querySubObject("Borders")->setProperty("Color", QColor(0, 0, 0)); 97 //調整數據區行高 98 QString rowsName; 99 rowsName.append("2:"); 100 rowsName.append(QString::number(table->rowCount() + 2)); 101 range = worksheet->querySubObject("Range(const QString&)", rowsName); 102 range->setProperty("RowHeight", 20); 103 workbook->dynamicCall("SaveAs(const QString&)",QDir::toNativeSeparators(fileName));//保存至fileName 104 workbook->dynamicCall("Close()");//關閉工作簿 105 excel->dynamicCall("Quit()");//關閉excel 106 delete excel; 107 excel=NULL; 108 if (QMessageBox::question(NULL,"完成","文件已經導出,是否現在打開?",QMessageBox::Yes|QMessageBox::No)==QMessageBox::Yes) 109 { 110 QDesktopServices::openUrl(QUrl("file:///" + QDir::toNativeSeparators(fileName))); 111 } 112 } 113 else 114 { 115 QMessageBox::warning(NULL,"錯誤","未能創建 Excel 對象,請安裝 Microsoft Excel。",QMessageBox::Apply); 116 } 117 } 118 }
到此,你的整個導出功能基本上就相當於完成了。
