QWord.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  1. #include "QWord.h"
  2. #ifdef Q_OS_WIN
  3. #include <QDateTime>
  4. #include <QFileDialog>
  5. #include <QFile>
  6. #include <QTextStream>
  7. #include <ActiveQt/QAxObject>
  8. #include <ActiveQt/QAxWidget>
  9. #include <QTextStream>
  10. #include <ActiveQt/QAxBase>
  11. #include <QTextFormat>
  12. QWord::QWord(QObject *parent)
  13. {
  14. CoInitializeEx(NULL, COINIT_MULTITHREADED); //解决非主线程无法调用word问题
  15. m_word = new QAxObject(parent);
  16. m_documents = NULL;
  17. m_document = NULL;
  18. m_bOpened = false;
  19. }
  20. QWord::~QWord()
  21. {
  22. close();
  23. }
  24. bool QWord::createWord(QString reportname) //创建一个新的word
  25. {
  26. QString ReportName = reportname;
  27. QString defaultFileName = QString("%1").arg(ReportName);
  28. m_saveName = QFileDialog::getSaveFileName(0, tr("Report Information"), defaultFileName, tr("*.doc"));
  29. // CGlobalAppData *pAppData = CAppDataSingleton::instance();
  30. QString SavePath = /*pAppData->m_strAppDirPath + */ "/ReportWord" + m_saveName;
  31. QFile file(m_saveName);
  32. if (file.exists()) {
  33. m_strError = tr("abnormal:the file already exists!");
  34. // m_strError = QString::fromLocal8Bit("错误:目标文件已存在!");
  35. return false;
  36. }
  37. if (!m_saveName.isEmpty()) {
  38. if (!m_word->setControl("Word.Application")) {
  39. m_strError = tr("abnormal:failed to get the word component,please make sure the word is installed!");
  40. // m_strError = QString::fromLocal8Bit("错误:获取word组件失败,请确定是否安装了word!");
  41. return false;
  42. }
  43. m_word->setProperty("Visible", false);
  44. m_word->setProperty("DisplayAlerts",
  45. false); //不显示任何警告信息。如果为true那么在关闭是会出现类似“文件已修改,是否保存”的提示
  46. m_documents = m_word->querySubObject("Documents");
  47. m_documents->dynamicCall("Add (void)");
  48. m_document = m_word->querySubObject("ActiveDocument"); //获取当前激活的文档
  49. return true;
  50. } else {
  51. m_strError = tr("abnormal:the file name is empty!");
  52. // m_strError = QString::fromLocal8Bit("错误:文件名为空");
  53. return false;
  54. }
  55. }
  56. bool QWord::createNewWord(const QString &filePath)
  57. {
  58. m_saveName = filePath;
  59. QFile file(m_saveName);
  60. if (file.exists()) {
  61. file.remove(m_saveName);
  62. // m_strError = tr("error:the file already exists!");
  63. // m_strError = QString::fromLocal8Bit("错误:目标文件已存在!");
  64. // return false;
  65. }
  66. if (!m_saveName.isEmpty()) {
  67. if (!m_word->setControl("Word.Application")) {
  68. m_strError = tr("abnormal:failed to get the word component,please make sure the word is installed!");
  69. // m_strError = QString::fromLocal8Bit("错误:获取word组件失败,请确定是否安装了word!\n");
  70. return false;
  71. }
  72. m_word->setProperty("Visible", false);
  73. m_word->setProperty("DisplayAlerts",
  74. false); //不显示任何警告信息。如果为true那么在关闭是会出现类似“文件已修改,是否保存”的提示
  75. m_documents = m_word->querySubObject("Documents");
  76. if (!m_documents) {
  77. m_strError = tr("abnormal:failed to get the documents!");
  78. // m_strError = QString::fromLocal8Bit("获取文档失败!\n");
  79. return false;
  80. }
  81. m_documents->dynamicCall("Add (void)");
  82. m_document = m_word->querySubObject("ActiveDocument"); //获取当前激活的文档
  83. return true;
  84. } else {
  85. m_strError = tr("abnormal:the file name is empty!");
  86. // m_strError = QString::fromLocal8Bit("错误:文件名为空");
  87. return false;
  88. }
  89. }
  90. bool QWord::openword(bool bVisable)
  91. {
  92. m_word = new QAxObject();
  93. bool bFlag = m_word->setControl("word.Application");
  94. if (!bFlag) { bFlag = m_word->setControl("kwps.Application"); }
  95. if (!bFlag) { return false; }
  96. m_word->setProperty("Visible", bVisable);
  97. QAxObject *documents = m_word->querySubObject("Documents");
  98. if (!documents) { return false; }
  99. documents->dynamicCall("Add(QString)", m_strFilePath);
  100. m_bOpened = true;
  101. return m_bOpened;
  102. }
  103. bool QWord::open(const QString &strFilePath, bool bVisable)
  104. {
  105. m_strFilePath = strFilePath;
  106. // close();
  107. return openword(bVisable);
  108. }
  109. bool QWord::isOpen()
  110. {
  111. return m_bOpened;
  112. }
  113. void QWord::save()
  114. {
  115. if (m_document)
  116. m_document->dynamicCall("Save()");
  117. else
  118. return;
  119. }
  120. void QWord::close() //关闭 退出 析构时候也会自动调用一次
  121. {
  122. if (!m_saveName.isEmpty()) //如果不为空 则为新建
  123. {
  124. saveAs();
  125. m_saveName = "";
  126. }
  127. // if(m_document)
  128. // m_document->dynamicCall("Close (boolean)",false);
  129. // if(m_word)
  130. // m_word->dynamicCall("Quit (void)");
  131. if (m_documents) delete m_documents;
  132. if (m_word) delete m_word;
  133. m_document = NULL;
  134. m_documents = NULL;
  135. m_word = NULL;
  136. }
  137. void QWord::saveAs()
  138. {
  139. if (m_document)
  140. m_document->dynamicCall("SaveAs(const QString&)", QDir::toNativeSeparators(m_saveName));
  141. else
  142. return;
  143. }
  144. void QWord::setPageOrientation(int flag) //设置页面1 横向 还是 0竖向
  145. {
  146. QAxObject *selection = m_word->querySubObject("Selection");
  147. if (nullptr == selection) { return; }
  148. QString page;
  149. switch (flag) {
  150. case 0:
  151. page = "wdOrientPortrait";
  152. break;
  153. case 1:
  154. page = "wdOrientLandscape";
  155. break;
  156. }
  157. selection->querySubObject("PageSetUp")->setProperty("Orientation", page);
  158. }
  159. void QWord::setWordPageView(int flag)
  160. {
  161. QAxObject *viewPage = m_word->querySubObject("ActiveWindow");
  162. if (nullptr == viewPage) { return; }
  163. QString view;
  164. switch (flag) {
  165. case 1:
  166. view = "wdNormalView";
  167. break;
  168. case 2:
  169. view = "wdOutlineView";
  170. break;
  171. case 3:
  172. view = "wdPrintView";
  173. break;
  174. case 4:
  175. view = "wdPrintPreview";
  176. break;
  177. case 5:
  178. view = "wdMasterView";
  179. break;
  180. case 6:
  181. view = "wdWebView";
  182. break;
  183. case 7:
  184. view = "wdReadingView";
  185. break;
  186. case 8:
  187. view = "wdConflictView";
  188. break;
  189. }
  190. viewPage->querySubObject("View")->setProperty("Type", view);
  191. }
  192. void QWord::insertMoveDown() //插入回车
  193. {
  194. QAxObject *selection = m_word->querySubObject("Selection");
  195. if (nullptr == selection) { return; }
  196. selection->dynamicCall("TypeParagraph(void)");
  197. }
  198. void QWord::insertText(const QString &text)
  199. {
  200. QAxObject *selection = m_word->querySubObject("Selection");
  201. if (nullptr == selection) { return; }
  202. selection->dynamicCall("TypeText(const QString&)", text);
  203. }
  204. QString QWord::GetText()
  205. {
  206. QAxObject *selection = m_word->querySubObject("Selection");
  207. QString str;
  208. if (nullptr != selection) { str = selection->dynamicCall("GetText(void)").toString(); }
  209. return str;
  210. }
  211. //设置选中位置文字居中 0 ,居左 1,居右 2
  212. void QWord::setParagraphAlignment(int flag)
  213. {
  214. QAxObject *selection = m_word->querySubObject("Selection");
  215. if (nullptr == selection) { return; }
  216. if (flag == 0) {
  217. selection->querySubObject("ParagraphFormat")->setProperty("Alignment", "wdAlignParagraphCenter");
  218. } else if (flag == 1) {
  219. selection->querySubObject("ParagraphFormat")->setProperty("Alignment", "wdAlignParagraphJustify");
  220. } else if (flag == 2) {
  221. selection->querySubObject("ParagraphFormat")->setProperty("Alignment", "wdAlignParagraphRight");
  222. }
  223. }
  224. void QWord::setRowAlignment(int tableIndex, int row, int flag)
  225. {
  226. QAxObject *tables = m_document->querySubObject("Tables");
  227. if (nullptr == tables) { return; }
  228. QAxObject *table = tables->querySubObject("Item(int)", tableIndex);
  229. if (nullptr == table) { return; }
  230. QAxObject *Row = table->querySubObject("Rows(int)", row);
  231. if (nullptr == Row) { return; }
  232. QAxObject *range = Row->querySubObject("Range");
  233. if (nullptr == range) { return; }
  234. Row->querySubObject("Alignment(int)", flag);
  235. if (flag == 0) {
  236. range->querySubObject("ParagraphFormat")->setProperty("Alignment", "wdAlignParagraphCenter");
  237. } else if (flag == 1) {
  238. range->querySubObject("ParagraphFormat")->setProperty("Alignment", "wdAlignParagraphJustify");
  239. } else if (flag == 2) {
  240. range->querySubObject("ParagraphFormat")->setProperty("Alignment", "wdAlignParagraphRight");
  241. } else if (flag == 3) {
  242. range->querySubObject("ParagraphFormat")->setProperty("Alignment", "wdAlignParagraphLeft");
  243. }
  244. }
  245. void QWord::setFontSize(int fontsize) //设置字体大小
  246. {
  247. QAxObject *selection = m_word->querySubObject("Selection");
  248. if (nullptr == selection) { return; }
  249. selection->querySubObject("Font")->setProperty("Size", fontsize);
  250. }
  251. void QWord::setFontBold(bool flag)
  252. {
  253. QAxObject *selection = m_word->querySubObject("Selection");
  254. if (nullptr == selection) { return; }
  255. selection->querySubObject("Font")->setProperty("Bold", flag);
  256. }
  257. void QWord::setFontName(const QString &fontName)
  258. {
  259. QAxObject *selection = m_word->querySubObject("Selection");
  260. if (nullptr == selection) { return; }
  261. selection->querySubObject("Font")->setProperty("Name", fontName);
  262. }
  263. void QWord::setSelectionRange(int start, int end)
  264. {
  265. QAxObject *selection = m_word->querySubObject("Selection");
  266. if (nullptr == selection) { return; }
  267. selection->dynamicCall("SetRange(int, int)", start, end); //第1个字符后开始,到第9个字符结束范围
  268. }
  269. void QWord::getUsedRange(int *topLeftRow, int *topLeftColumn, int *bottomRightRow, int *bottomRightColumn)
  270. {
  271. QAxObject *range = m_document->querySubObject("Range");
  272. if (nullptr == range) { return; }
  273. *topLeftRow = range->property("Row").toInt();
  274. if (nullptr == topLeftRow) { return; }
  275. *topLeftColumn = range->property("Column").toInt();
  276. if (nullptr == topLeftColumn) { return; }
  277. QAxObject *rows = range->querySubObject("Rows");
  278. if (nullptr == rows) { return; }
  279. *bottomRightRow = *topLeftRow + rows->property("Count").toInt() - 1;
  280. if (nullptr == bottomRightRow) { return; }
  281. QAxObject *columns = range->querySubObject("Columns");
  282. if (nullptr == columns) { return; }
  283. *bottomRightColumn = *topLeftColumn + columns->property("Count").toInt() - 1;
  284. if (nullptr == bottomRightColumn) { return; }
  285. }
  286. void QWord::intsertTable(int row, int column)
  287. {
  288. QAxObject *tables = m_document->querySubObject("Tables");
  289. if (nullptr == tables) { return; }
  290. QAxObject *selection = m_word->querySubObject("Selection");
  291. if (nullptr == selection) { return; }
  292. QAxObject *range = selection->querySubObject("Range");
  293. if (nullptr == range) { return; }
  294. QVariantList params;
  295. params.append(range->asVariant());
  296. params.append(row);
  297. params.append(column);
  298. tables->querySubObject("Add(QAxObject*, int, int, QVariant&, QVariant&)", params);
  299. QAxObject *table = selection->querySubObject("Tables(int)", 1);
  300. if (nullptr == table) { return; }
  301. table->setProperty("Style", "网格型");
  302. QAxObject *Borders = table->querySubObject("Borders");
  303. if (nullptr == Borders) { return; }
  304. Borders->setProperty("InsideLineStyle", 1);
  305. Borders->setProperty("OutsideLineStyle", 1);
  306. /*QString doc = Borders->generateDocumentation();
  307. QFile outFile("D:\\360Downloads\\Picutres\\Borders.html");
  308. outFile.open(QIODevice::WriteOnly|QIODevice::Append);
  309. QTextStream ts(&outFile);
  310. ts<<doc<<endl;*/
  311. }
  312. void QWord::intsertTable(int tableIndex, int row, int column)
  313. {
  314. QAxObject *tables = m_document->querySubObject("Tables");
  315. if (nullptr == tables) { return; }
  316. QAxObject *selection = m_word->querySubObject("Selection");
  317. if (nullptr == selection) { return; }
  318. QAxObject *range = selection->querySubObject("Range");
  319. if (nullptr == range) { return; }
  320. QVariantList params;
  321. params.append(range->asVariant());
  322. params.append(row);
  323. params.append(column);
  324. tables->querySubObject("Add(QAxObject*, int, int, QVariant&, QVariant&)", params);
  325. QAxObject *table = selection->querySubObject("Tables(int)", tableIndex);
  326. if (nullptr == table) { return; }
  327. table->setProperty("Style", "网格型");
  328. QAxObject *Borders = table->querySubObject("Borders");
  329. if (nullptr == Borders) { return; }
  330. Borders->setProperty("InsideLineStyle", 1);
  331. Borders->setProperty("OutsideLineStyle", 1);
  332. /*QString doc = Borders->generateDocumentation();
  333. QFile outFile("D:\\360Downloads\\Picutres\\Borders.html");
  334. outFile.open(QIODevice::WriteOnly|QIODevice::Append);
  335. QTextStream ts(&outFile);
  336. ts<<doc<<endl;*/
  337. }
  338. void QWord::setColumnWidth(int column, int width) //设置列宽
  339. {
  340. QAxObject *selection = m_word->querySubObject("Selection");
  341. if (nullptr == selection) { return; }
  342. QAxObject *table = selection->querySubObject("Tables(1)");
  343. if (nullptr == table) { return; }
  344. table->querySubObject("Columns(int)", column)->setProperty("Width", width);
  345. }
  346. void QWord::setCellString(int nTable, int row, int column, const QString &text)
  347. {
  348. QAxObject *pTables = m_document->querySubObject("Tables");
  349. if (nullptr == pTables) { return; }
  350. QAxObject *table = pTables->querySubObject("Item(int)", nTable);
  351. if (table) {
  352. table->querySubObject("Cell(int,int)", row, column)
  353. ->querySubObject("Range")
  354. ->dynamicCall("SetText(QString)", text);
  355. }
  356. }
  357. void QWord::MergeCells(int tableIndex, int nStartRow, int nStartCol, int nEndRow, int nEndCol) //合并单元格
  358. {
  359. QAxObject *tables = m_document->querySubObject("Tables");
  360. if (nullptr == tables) { return; }
  361. QAxObject *table = tables->querySubObject("Item(int)", tableIndex);
  362. if (nullptr == table) { return; }
  363. if (table) {
  364. QAxObject *StartCell = table->querySubObject("Cell(int, int)", nStartRow, nStartCol);
  365. QAxObject *EndCell = table->querySubObject("Cell(int, int)", nEndRow, nEndCol);
  366. if (nullptr == StartCell) { return; }
  367. if (nullptr == EndCell) { return; }
  368. StartCell->querySubObject("Merge(QAxObject *)", EndCell->asVariant());
  369. }
  370. }
  371. //第二种方法调用
  372. // void QWord::MergeCells(int tableIndex, int nStartRow,int nStartCol,int nEndRow,int nEndCol)//合并单元格
  373. // {
  374. // QAxObject* tables = m_document->querySubObject("Tables");
  375. // QAxObject* table = tables->querySubObject("Item(int)",tableIndex);
  376. // QAxObject* StartCell =table->querySubObject("Cell(int, int)",nStartRow,nStartCol);
  377. // QAxObject* EndCell = table->querySubObject("Cell(int, int)",nEndRow,nEndCol);
  378. // StartCell->dynamicCall("Merge(LPDISPATCH)",EndCell->asVariant());
  379. // }
  380. void QWord::setColumnHeight(int nTable, int column, int height)
  381. {
  382. QAxObject *pTables = m_document->querySubObject("Tables");
  383. if (nullptr == pTables) { return; }
  384. QAxObject *table = pTables->querySubObject("Item(int)", nTable);
  385. if (table) { table->querySubObject("Columns(int)", column)->setProperty("Hight", height); }
  386. }
  387. void QWord::setRowHeight(int nTable, int Row, int height)
  388. {
  389. QAxObject *pTables = m_document->querySubObject("Tables");
  390. if (nullptr == pTables) { return; }
  391. QAxObject *table = pTables->querySubObject("Item(int)", nTable);
  392. if (table) { table->querySubObject("Rows(int)", Row)->setProperty("Hight", height); }
  393. }
  394. void QWord::setColumnHeight(int column, int height) //设置列高
  395. {
  396. QAxObject *selection = m_word->querySubObject("Selection");
  397. if (nullptr == selection) { return; }
  398. QAxObject *table = selection->querySubObject("Tables(1)");
  399. if (table) { table->querySubObject("Columns(int)", column)->setProperty("Hight", height); }
  400. }
  401. void QWord::setCellString(int row, int column, const QString &text)
  402. {
  403. QAxObject *selection = m_word->querySubObject("Selection");
  404. if (nullptr == selection) { return; }
  405. QAxObject *table = selection->querySubObject("Tables(1)");
  406. if (nullptr == table) { return; }
  407. QAxObject *rows = table->querySubObject("Rows");
  408. if (rows) { return; }
  409. int Count = rows->dynamicCall("Count").toInt();
  410. table->querySubObject("Cell(int, int)", row, column)
  411. ->querySubObject("Range")
  412. ->dynamicCall("SetText(QString)", text);
  413. }
  414. void QWord::setCellFontBold(int row, int column, bool isBold) //设置内容粗体 isBold控制是否粗体
  415. {
  416. QAxObject *selection = m_word->querySubObject("Selection");
  417. if (nullptr == selection) { return; }
  418. QAxObject *table = selection->querySubObject("Tables(1)");
  419. if (nullptr == table) { return; }
  420. table->querySubObject("Cell(int, int)", row, column)->querySubObject("Range")->dynamicCall("SetBold(int)", isBold);
  421. }
  422. void QWord::setCellFontSize(int row, int column, int size) //设置文字大小
  423. {
  424. QAxObject *selection = m_word->querySubObject("Selection");
  425. if (nullptr == selection) { return; }
  426. QAxObject *table = selection->querySubObject("Tables(1)");
  427. if (nullptr == table) { return; }
  428. table->querySubObject("Cell(int, int)", row, column)
  429. ->querySubObject("Range")
  430. ->querySubObject("Font")
  431. ->setProperty("Size", size);
  432. }
  433. QVariant QWord::getCellValue(int row, int column) //获取单元格内容 此处对于Excel来说列和行从1开始最少
  434. {
  435. QAxObject *selection = m_word->querySubObject("Selection");
  436. QAxObject *table = selection->querySubObject("Tables(1)");
  437. if (nullptr != selection && nullptr != table)
  438. return table->querySubObject("Cell(int, int)", row, column)->querySubObject("Range")->property("Text");
  439. }
  440. int QWord::getTableCount()
  441. {
  442. QAxObject *tables = m_document->querySubObject("Tables");
  443. int val;
  444. if (nullptr != tables) { val = tables->property("Count").toInt(); }
  445. return val;
  446. }
  447. void QWord::moveForEnd()
  448. {
  449. QAxObject *selection = m_word->querySubObject("Selection");
  450. if (nullptr == selection) { return; }
  451. QVariantList params;
  452. params.append(6);
  453. params.append(0);
  454. selection->dynamicCall("EndOf(QVariant&, QVariant&)", params).toInt();
  455. }
  456. void QWord::insertCellPic(int row, int column, const QString &picPath)
  457. {
  458. QAxObject *selection = m_word->querySubObject("Selection");
  459. if (nullptr == selection) { return; }
  460. QAxObject *table = selection->querySubObject("Tables(1)");
  461. if (nullptr == table) { return; }
  462. QAxObject *range = table->querySubObject("Cell(int, int)", row, column)->querySubObject("Range");
  463. if (nullptr == range) { return; }
  464. range->querySubObject("InlineShapes")->dynamicCall("AddPicture(const QString&)", picPath);
  465. }
  466. void QWord::setTableAutoFitBehavior(int flag)
  467. {
  468. QAxObject *selection = m_word->querySubObject("Selection");
  469. if (nullptr == selection) { return; }
  470. QAxObject *table = selection->querySubObject("Tables(1)");
  471. if (nullptr == table) { return; }
  472. if (0 <= flag && flag <= 2) table->dynamicCall("AutoFitBehavior(WdAutoFitBehavior)", flag);
  473. }
  474. void QWord::deleteSelectColumn(int column)
  475. {
  476. QAxObject *selection = m_word->querySubObject("Selection");
  477. if (nullptr == selection) { return; }
  478. QAxObject *table = selection->querySubObject("Tables(1)");
  479. if (nullptr == table) { return; }
  480. QAxObject *columns = table->querySubObject("Columns(int)", column);
  481. if (nullptr == columns) { return; }
  482. columns->dynamicCall("Delete()");
  483. }
  484. void QWord::setOptionCheckSpell(bool flags)
  485. {
  486. QAxObject *opetions = m_word->querySubObject("Options");
  487. if (!opetions) return;
  488. opetions->setProperty("CheckGrammarAsYouType", flags);
  489. opetions->setProperty("CheckGrammarWithSpelling", flags);
  490. opetions->setProperty("ContextualSpeller", flags);
  491. opetions->setProperty("CheckSpellingAsYouType", flags);
  492. }
  493. void QWord::addTableRow(int tableIndex, int nRow, int rowCount)
  494. {
  495. QAxObject *tables = m_document->querySubObject("Tables");
  496. if (nullptr == tables) { return; }
  497. QAxObject *table = tables->querySubObject("Item(int)", tableIndex);
  498. if (nullptr == table) { return; }
  499. QAxObject *rows = table->querySubObject("Rows");
  500. if (nullptr == rows) { return; }
  501. int Count = rows->dynamicCall("Count").toInt();
  502. if (0 < nRow && nRow <= Count) {
  503. for (int i = 0; i < rowCount; ++i) {
  504. QString sPos = QString("Item(%1)").arg(nRow + i);
  505. QAxObject *row = rows->querySubObject(sPos.toStdString().c_str());
  506. QAxObject *row1 = rows->querySubObject("Last");
  507. QAxObject *row2 = rows->querySubObject("Row(int)", nRow + i);
  508. QAxObject *row3 = rows->querySubObject("Item(int)", nRow + i);
  509. if (nullptr != row) {
  510. QVariant param = row->asVariant();
  511. /*rows->dynamicCall("Add(Variant)",param);*/
  512. QAxObject *NewRow = rows->querySubObject("Add(Variant)", param);
  513. }
  514. }
  515. }
  516. }
  517. ////创建表格
  518. void QWord::insertTable(int tableIndex, int row, int column)
  519. {
  520. QAxObject *tables = m_document->querySubObject("Tables");
  521. if (nullptr == tables) { return; }
  522. QAxObject *table = tables->querySubObject("Item(int)", tableIndex);
  523. if (nullptr == table) { return; }
  524. // QAxObject* rows =table->querySubObject("Rows");
  525. QAxObject *selection = m_word->querySubObject("Selection");
  526. if (nullptr == selection) { return; }
  527. QAxObject *range = selection->querySubObject("Range");
  528. if (nullptr == range) { return; }
  529. QVariantList params;
  530. params.append(range->asVariant());
  531. params.append(row);
  532. params.append(column);
  533. tables->querySubObject("Add(QAxObject*, int, int, QVariant&, QVariant&)", params);
  534. table = selection->querySubObject("Tables(int)", 1);
  535. table->setProperty("Style", "网格型");
  536. QAxObject *Borders = table->querySubObject("Borders");
  537. if (nullptr == Borders) { return; }
  538. Borders->setProperty("InsideLineStyle", 1);
  539. Borders->setProperty("OutsideLineStyle", 1);
  540. }
  541. ////设置表格列宽
  542. void QWord::setColumnWidth(int nTable, int column, int width)
  543. {
  544. QAxObject *pTables = m_document->querySubObject("Tables");
  545. if (nullptr == pTables) { return; }
  546. QAxObject *table = pTables->querySubObject("Item(int)", nTable);
  547. if (table) { table->querySubObject("Columns(int)", column)->setProperty("width", width); }
  548. }
  549. //在表格中插入图片
  550. void QWord::insertCellPic(int nTable, int row, int column, const QString &picPath)
  551. {
  552. QAxObject *pTables = m_document->querySubObject("Tables");
  553. if (nullptr == pTables) { return; }
  554. QAxObject *table = pTables->querySubObject("Item(int)", nTable);
  555. if (nullptr == table) { return; }
  556. QAxObject *range = table->querySubObject("Cell(int,int )", row, column)->querySubObject("Range");
  557. if (nullptr == range) { return; }
  558. range->querySubObject("InlineShapes")->dynamicCall("AddPicture(const QString&)", picPath);
  559. }
  560. //设置内容粗体
  561. void QWord::setCellFontBold(int nTable, int row, int column, bool isBold)
  562. {
  563. QAxObject *pTables = m_document->querySubObject("Tables");
  564. if (nullptr == pTables) { return; }
  565. QAxObject *table = pTables->querySubObject("Item(int)", nTable);
  566. if (nullptr == table) { return; }
  567. table->querySubObject("Cell(int,int )", row, column)->querySubObject("Range")->dynamicCall("SetBold(int)", isBold);
  568. }
  569. //设置文字大小
  570. void QWord::setCellFontSize(int nTable, int row, int column, int size)
  571. {
  572. QAxObject *pTables = m_document->querySubObject("Tables");
  573. if (nullptr == pTables) { return; }
  574. QAxObject *table = pTables->querySubObject("Item(int)", nTable);
  575. if (nullptr == table) { return; }
  576. table->querySubObject("Cell(int,int)", row, column)
  577. ->querySubObject("Range")
  578. ->querySubObject("Font")
  579. ->setProperty("Size", size);
  580. }
  581. void QWord::setVisible(bool isVisible)
  582. {
  583. if (m_word != nullptr) { m_word->setProperty("Visible", isVisible); }
  584. }
  585. #endif