QT += core gui sql
CONFIG += qaxcontainer
greaterThan(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);
QSqlDatabase db = 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()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
QString filepath=QFileDialog::getSaveFileName(this,tr("Save orbit"),".",tr("Microsoft Office 2007 (*.xlsx)"));//获取保存路径
if(!filepath.isEmpty()){
QAxObject *excel = new QAxObject(this);
excel->setControl("Excel.Application");//连接Excel控件
excel->dynamicCall("SetVisible (bool Visible)","false");//不显示窗体
excel->setProperty("DisplayAlerts", false);//不显示任何警告信息。如果为true那么在关闭是会出现类似“文件已修改,是否保存”的提示
QAxObject *workbooks = excel->querySubObject("WorkBooks");//获取工作簿集合
workbooks->