QT += core gui sqlCONFIG += qaxcontainergreaterThan(QT_MAJOR_VERSION, 4): QT += widgets
#include "Mainwindow.h"#include "Ui_mainwindow.h"MainWindow::MainWindow(Qwidget *Parent) : Qmainwindow(Parent),Ui(New Ui::MainWindow){Ui-Setupui(This); QsqldatabaseDb= Qsqldatabase::Adddatabase("Qsqlite");Db.Setdatabasename("./sql/contact.db"); If(!Db.Open()) {Qdebug() <<"Open SQL Failed!"; Return; }Model= New Qsqlquerymodel(Ui-TableView);Model-Setquery(QString("SELECT * from Phonebook;"));Model-Setheaderdata(0,Qt::Horizontal,Qobject::Tr("Name"));Model-Setheaderdata(1,Qt::Horizontal,Qobject::Tr("Tel"));Ui-TableView-Setmodel(Model);Db.Close();}MainWindow::~MainWindow(){ DeleteUi;}void MainWindow::On_pushbutton_clicked(){ QStringFilePath=Qfiledialog::GetSaveFileName(This,Tr("Save Orbit"),".",Tr(Microsoft Office page (*.xlsx)));Get Save Path If(!FilePath.IsEmpty()){ Qaxobject *Excel= New Qaxobject(This);Excel-Setcontrol("Excel.Application");To connect to an Excel controlExcel-Dynamiccall("SetVisible (bool Visible)","False");Do not show formExcel-SetProperty("DisplayAlerts", False);No warning message is displayed. If True then closing is a prompt similar to "file Modified, save" Qaxobject *Workbooks=Excel-Querysubobject("WorkBooks");Get Workbook CollectionWorkbooks-Dynamiccall("Add");Create a new workbook Qaxobject *Workbook=Excel-Querysubobject("ActiveWorkbook");Get the current workbook Qaxobject *Worksheets=Workbook-Querysubobject("Sheets");Get a collection of worksheets Qaxobject *Worksheet=Worksheets-Querysubobject("Item (int)",1);Get worksheet 1, which is a collection of worksheets, Sheet1 Qaxobject *Cellx,*Celly; For(IntI=0;I<Model-RowCount();I++){ QStringX=A+QString::Number(I+1);Set the cells you want to manipulate, such as A1 QStringY=B+QString::Number(I+1);Cellx=Worksheet-Querysubobject("Range (Qvariant, Qvariant)",X);Get cellCelly=Worksheet-Querysubobject("Range (Qvariant, Qvariant)",Y);Cellx-Dynamiccall("SetValue (const qvariant&)",Qvariant(Model-Record(I).Value("Name").Tostring()));Set the value of a cellCelly-Dynamiccall("SetValue (const qvariant&)",Qvariant(Model-Record(I).Value("Tel").Tostring())); }Workbook-Dynamiccall("SaveAs (const qstring&)",Qdir::tonativeseparators (filepath Save to FilePath, note that you must use Qdir::tonativeseparators to convert the "/" in the path to "\", or it must not be saved. Workbook->dynamiccall ( "Close ()" //close workbook Excel->dynamiccall "Quit ()" Close Excel delete Excel;=null;}} /span>
Http://www.heilqt.com/topic/56e90da0e498871267b374cf
QT operation on Windows platform Save Execel Table (via Qaxobject)