天天看點

qt儲存html表格,Qt 将表格中的資料儲存到Excel

//第一個參數是頁面上的表格,第二個是導出檔案的表頭資料

void FaJianDialog::Table2ExcelByHtml(QTableWidget *table,QString title)

{

QString fileName= QFileDialog::getSaveFileName(table, "儲存",QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation),"Excel 檔案(*.xls *.xlsx)");if (fileName!="")

{

QAxObject*excel = newQAxObject;if (excel->setControl("Excel.Application")) //連接配接Excel控件

{

excel->dynamicCall("SetVisible (bool Visible)","false");//不顯示窗體

excel->setProperty("DisplayAlerts", false);//不顯示任何警告資訊。如果為true那麼在關閉是會出現類似“檔案已修改,是否儲存”的提示

QAxObject *workbooks = excel->querySubObject("WorkBooks");//擷取工作簿集合

workbooks->dynamicCall("Add");//建立一個工作簿

QAxObject *workbook = excel->querySubObject("ActiveWorkBook");//擷取目前工作簿

QAxObject *worksheet = workbook->querySubObject("Worksheets(int)", 1);inti,j;//QTablewidget 擷取資料的列數

int colcount=table->columnCount();//QTablewidget 擷取資料的行數

int rowscount=table->rowCount()//QTableView 擷取列數//int colount=ui->tableview->model->columnCount();//QTableView 擷取行數//int rowcount=ui->tableview->model->rowCount();

QAxObject*cell,*col;//标題行

cell=worksheet->querySubObject("Cells(int,int)", 1, 1);

cell->dynamicCall("SetValue(const QString&)", title);

cell->querySubObject("Font")->setProperty("Size", 18);//調整行高

worksheet->querySubObject("Range(const QString&)", "1:1")->setProperty("RowHeight", 30);//合并标題行

QString cellTitle;

cellTitle.append("A1:");

cellTitle.append(QChar(colcount- 1 + ‘A‘));

cellTitle.append(QString::number(1));

QAxObject*range = worksheet->querySubObject("Range(const QString&)", cellTitle);

range->setProperty("WrapText", true);

range->setProperty("MergeCells", true);

range->setProperty("HorizontalAlignment", -4108);//xlCenter

range->setProperty("VerticalAlignment", -4108);//xlCenter//列标題

for(i=0;i

{

QString columnName;

columnName.append(QChar(i+ ‘A‘));

columnName.append(":");

columnName.append(QChar(i+ ‘A‘));

col= worksheet->querySubObject("Columns(const QString&)", columnName);

col->setProperty("ColumnWidth", table->columnWidth(i)/6);

cell=worksheet->querySubObject("Cells(int,int)", 2, i+1);//QTableWidget 擷取表格頭部文字資訊

columnName=table->horizontalHeaderItem(i)->text();//QTableView 擷取表格頭部文字資訊//columnName=ui->tableView_right->model()->headerData(i,Qt::Horizontal,Qt::DisplayRole).toString();

cell->dynamicCall("SetValue(const QString&)", columnName);

cell->querySubObject("Font")->setProperty("Bold", true);

cell->querySubObject("Interior")->setProperty("Color",QColor(191, 191, 191));

cell->setProperty("HorizontalAlignment", -4108);//xlCenter

cell->setProperty("VerticalAlignment", -4108);//xlCenter

}//資料區//QTableWidget 擷取表格資料部分

for(i=0;i

{

worksheet->querySubObject("Cells(int,int)", i+3, j+1)->dynamicCall("SetValue(const QString&)", table->item(i,j)?table->item(i,j)->text():"");

}

}//QTableView 擷取表格資料部分//for(i=0;itableView_right->model()->index(i, j);//QString strdata=ui->tableView_right->model()->data(index).toString();//worksheet->querySubObject("Cells(int,int)", i+3, j+1)->dynamicCall("SetValue(const QString&)", strdata);//}//}//畫框線

QString lrange;

lrange.append("A2:");

lrange.append(colcount- 1 + ‘A‘);

lrange.append(QString::number(table->rowCount() + 2));

range= worksheet->querySubObject("Range(const QString&)", lrange);

range->querySubObject("Borders")->setProperty("LineStyle", QString::number(1));

range->querySubObject("Borders")->setProperty("Color", QColor(0, 0, 0));//調整資料區行高

QString rowsName;

rowsName.append("2:");

rowsName.append(QString::number(table->rowCount() + 2));

range= worksheet->querySubObject("Range(const QString&)", rowsName);

range->setProperty("RowHeight", 20);

workbook->dynamicCall("SaveAs(const QString&)",QDir::toNativeSeparators(fileName));//儲存至fileName

workbook->dynamicCall("Close()");//關閉工作簿

excel->dynamicCall("Quit()");//關閉excel

deleteexcel;

excel=NULL;

//QMessageBox::information(this,tr("提示"),tr("資料已儲存"));if (QMessageBox::question(NULL,"完成","檔案已經導出,是否現在打開?",QMessageBox::Yes|QMessageBox::No)==QMessageBox::Yes)

{

QDesktopServices::openUrl(QUrl("file:///" +QDir::toNativeSeparators(fileName)));

}

}else{

QMessageBox::warning(NULL,"錯誤","未能建立 Excel 對象,請安裝 Microsoft Excel。",QMessageBox::Apply);

}

}

}