QWord.cpp 24 KB

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