QT operation on Windows platform Save Execel Table (via Qaxobject)

Source: Internet
Author: User

    • Rough operation
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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.