1、UI设计。
2、生成报表按钮的槽函数实现如下:
void OutWord::on_pushButton()
{
QAxObject *myword = new QAxObject("Word.Application");
myword->setProperty("Visible", false);
QAxObject *mydocs = myword->querySubObject("Documents");
mydocs->dynamicCall("Add(QString)", QString::fromLocal8Bit("F:/work/word2.dot"));
QAxObject *mydoc = myword->querySubObject("ActiveDocument");
QAxObject *bookmark_shebei = mydoc->querySubObject("Bookmarks(QVariant)", "shebei");
if (!bookmark_shebei->isNull())
{
QString str_shebie = ui.lineEdit->text();
bookmark_shebei->dynamicCall("Select(void)"); //选中要选中的区域
bookmark_shebei->querySubObject("Range")->setProperty("Text", str_shebie);
}
QAxObject *bookmark_wendu= mydoc->querySubObject("Bookmarks(QVariant)", "wendu");
if (!bookmark_wendu->isNull())
{
QString str = ui.lineEdit_2->text();
bookmark_wendu->dynamicCall("Select(void)"); //选中要选中的区域
bookmark_wendu->querySubObject("Range")->setProperty("Text", str);
}
QAxObject *bookmark_yiju = mydoc->querySubObject("Bookmarks(QVariant)", "yiju");
if (!bookmark_yiju->isNull())
{
QString str = ui.lineEdit_3->text();
bookmark_yiju->dynamicCall("Select(void)"); //选中要选中的区域
bookmark_yiju->querySubObject("Range")->setProperty("Text", str);
}
for (int i = 1;i < 4;i++)
{
QString s_xuhao = QString("xuhao%1").arg(i);
qDebug() << s_xuhao;
QString str = ui.tableWidget->item(i, 0)->text();
QAxObject *bookmark = mydoc->querySubObject("Bookmarks(QVariant)", s_xuhao);
bookmark->dynamicCall("Select(void)");
bookmark->querySubObject("Range")->setProperty("Text", str);
}
for (int i = 1;i < 4;i++)
{
for (int j = 1;j < 4;j++)
{
QString s_csdata = QString("%1%2%3").arg("csdata").arg(i).arg(j);
qDebug() << s_csdata;
QString str = ui.tableWidget->item(i, j)->text();
QAxObject *bookmark = mydoc->querySubObject("Bookmarks(QVariant)", s_csdata);
bookmark->dynamicCall("Select(void)");
bookmark->querySubObject("Range")->setProperty("Text", str);
}
}
mydoc->dynamicCall("SaveAs(const QString&))", "F:/work/word1.doc");
mydoc->dynamicCall("Close (boolean)", false);
myword->dynamicCall("Quit()");
}
3、右侧QtableWidget组件的初始化函数如下:
void OutWord::InitUI()
{
ui.tableWidget->setColumnCount(4);
ui.tableWidget->setRowCount(4);
QStringList qlist;
qlist << QStringLiteral("序号") << QStringLiteral("测试数据1") << QStringLiteral("测试数据2") << QStringLiteral("测试数据3");
ui.tableWidget->setHorizontalHeaderLabels(qlist);
QTableWidgetItem *item_11 = new QTableWidgetItem("1");
ui.tableWidget->setItem(1, 0, item_11);
QTableWidgetItem *item_12 = new QTableWidgetItem("2");
ui.tableWidget->setItem(2, 0, item_12);
QTableWidgetItem *item_13 = new QTableWidgetItem("3");
ui.tableWidget->setItem(3, 0, item_13);
QTableWidgetItem *item_21 = new QTableWidgetItem("56");
ui.tableWidget->setItem(1, 1, item_21);
QTableWidgetItem *item_22 = new QTableWidgetItem("89");
ui.tableWidget->setItem(2, 1, item_22);
QTableWidgetItem *item_23 = new QTableWidgetItem("46");
ui.tableWidget->setItem(3, 1, item_23);
QTableWidgetItem *item_31 = new QTableWidgetItem("19");
ui.tableWidget->setItem(1, 2, item_31);
QTableWidgetItem *item_32 = new QTableWidgetItem("76");
ui.tableWidget->setItem(2, 2, item_32);
QTableWidgetItem *item_33 = new QTableWidgetItem("36");
ui.tableWidget->setItem(3, 2, item_33);
QTableWidgetItem *item_41 = new QTableWidgetItem("76");
ui.tableWidget->setItem(1, 3, item_41);
QTableWidgetItem *item_42 = new QTableWidgetItem("72");
ui.tableWidget->setItem(2, 3, item_42);
QTableWidgetItem *item_43 = new QTableWidgetItem("49");
ui.tableWidget->setItem(3, 3, item_43);
}
4、运行效果