qword.cpp 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272
  1. #include "qword.h"
  2. #include <QDateTime>
  3. #include <QFileDialog>
  4. #include <QFile>
  5. #include <QTextStream>
  6. #include <ActiveQt/QAxObject>
  7. #include <ActiveQt/QAxWidget>
  8. #include<QTextStream>
  9. #include <ActiveQt/QAxBase>
  10. #include <QTextFormat>
  11. QWord::QWord(QObject *parent)
  12. {
  13. CoInitializeEx(NULL,COINIT_MULTITHREADED);//解决非主线程无法调用word问题
  14. m_word = new QAxObject(parent);
  15. m_documents = NULL;
  16. m_document = NULL;
  17. m_bOpened=false;
  18. }
  19. QWord::~QWord()
  20. {
  21. close();
  22. }
  23. bool QWord::createWord(QString reportname) //创建一个新的word
  24. {
  25. QString ReportName= reportname;
  26. QString defaultFileName = QString("%1").arg(ReportName);
  27. m_saveName=QFileDialog::getSaveFileName(0,tr("Report Information"),defaultFileName,tr("*.doc"));
  28. //CGlobalAppData *pAppData = CAppDataSingleton::instance();
  29. QString SavePath = /*pAppData->m_strAppDirPath + */"/ReportWord"+m_saveName;
  30. QFile file(m_saveName);
  31. if(file.exists())
  32. {
  33. m_strError = tr("abnormal:the file already exists!");
  34. //m_strError = QString::fromLocal8Bit("错误:目标文件已存在!");
  35. return false;
  36. }
  37. if(!m_saveName.isEmpty())
  38. {
  39. if(!m_word->setControl("Word.Application"))
  40. {
  41. m_strError = tr("abnormal:failed to get the word component,please make sure the word is installed!");
  42. //m_strError = QString::fromLocal8Bit("错误:获取word组件失败,请确定是否安装了word!");
  43. return false;
  44. }
  45. m_word->setProperty("Visible",false);
  46. m_word->setProperty("DisplayAlerts", 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. {
  53. m_strError = tr("abnormal:the file name is empty!");
  54. //m_strError = QString::fromLocal8Bit("错误:文件名为空");
  55. return false;
  56. }
  57. }
  58. bool QWord::createNewWord(const QString& filePath )
  59. {
  60. m_saveName = filePath;
  61. QFile file(m_saveName);
  62. if(file.exists())
  63. {
  64. file.remove(m_saveName);
  65. //m_strError = tr("error:the file already exists!");
  66. //m_strError = QString::fromLocal8Bit("错误:目标文件已存在!");
  67. //return false;
  68. }
  69. if(!m_saveName.isEmpty())
  70. {
  71. if(!m_word->setControl("Word.Application"))
  72. {
  73. m_strError = tr("abnormal:failed to get the word component,please make sure the word is installed!");
  74. //m_strError = QString::fromLocal8Bit("错误:获取word组件失败,请确定是否安装了word!\n");
  75. return false;
  76. }
  77. m_word->setProperty("Visible",false);
  78. m_word->setProperty("DisplayAlerts", false);//不显示任何警告信息。如果为true那么在关闭是会出现类似“文件已修改,是否保存”的提示
  79. m_documents = m_word->querySubObject("Documents");
  80. if(!m_documents)
  81. {
  82. m_strError = tr("abnormal:failed to get the documents!");
  83. //m_strError = QString::fromLocal8Bit("获取文档失败!\n");
  84. return false;
  85. }
  86. m_documents->dynamicCall("Add (void)");
  87. m_document = m_word->querySubObject("ActiveDocument");//获取当前激活的文档
  88. return true;
  89. }else
  90. {
  91. m_strError = tr("abnormal:the file name is empty!");
  92. //m_strError = QString::fromLocal8Bit("错误:文件名为空");
  93. return false;
  94. }
  95. }
  96. bool QWord::openword(bool bVisable)
  97. {
  98. m_word = new QAxObject();
  99. bool bFlag = m_word->setControl("word.Application");
  100. if(!bFlag)
  101. {
  102. bFlag = m_word->setControl("kwps.Application");
  103. }
  104. if(!bFlag)
  105. {
  106. return false;
  107. }
  108. m_word->setProperty("Visible",bVisable);
  109. QAxObject* documents =m_word->querySubObject("Documents");
  110. if(!documents)
  111. {
  112. return false;
  113. }
  114. documents->dynamicCall("Add(QString)",m_strFilePath);
  115. m_bOpened = true;
  116. return m_bOpened;
  117. }
  118. bool QWord::open(const QString& strFilePath,bool bVisable)
  119. {
  120. m_strFilePath = strFilePath;
  121. //close();
  122. return openword(bVisable);
  123. }
  124. bool QWord::isOpen()
  125. {
  126. return m_bOpened;
  127. }
  128. void QWord::save()
  129. {
  130. if(m_document)
  131. m_document->dynamicCall("Save()");
  132. else
  133. return;
  134. }
  135. void QWord::close() //关闭 退出 析构时候也会自动调用一次
  136. {
  137. if(!m_saveName.isEmpty()) //如果不为空 则为新建
  138. {
  139. saveAs();
  140. m_saveName = "";
  141. }
  142. if(m_document)
  143. m_document->dynamicCall("Close (boolean)",false);
  144. if(m_word){
  145. m_word->dynamicCall("Quit (void)");
  146. }
  147. if(m_documents)
  148. delete m_documents;
  149. if(m_word)
  150. delete m_word;
  151. m_document = NULL;
  152. m_documents = NULL;
  153. m_word = NULL;
  154. }
  155. void QWord::saveAs()
  156. {
  157. if(m_document)
  158. m_document->dynamicCall("SaveAs(const QString&)",QDir::toNativeSeparators(m_saveName));
  159. else
  160. return;
  161. }
  162. void QWord::setPageOrientation(int flag) //设置页面1 横向 还是 0竖向
  163. {
  164. QAxObject* selection = m_word->querySubObject("Selection");
  165. if(NULL== selection)
  166. {
  167. return;
  168. }
  169. QString page;
  170. switch (flag)
  171. {
  172. case 0:
  173. page = "wdOrientPortrait";
  174. break;
  175. case 1:
  176. page = "wdOrientLandscape";
  177. break;
  178. }
  179. selection->querySubObject("PageSetUp")->setProperty("Orientation",page);
  180. delete selection;
  181. }
  182. void QWord::setWordPageView(int flag)
  183. {
  184. QAxObject* viewPage = m_word->querySubObject("ActiveWindow");
  185. if(NULL== viewPage)
  186. {
  187. return;
  188. }
  189. QString view;
  190. switch (flag)
  191. {
  192. case 1:
  193. view = "wdNormalView";
  194. break;
  195. case 2:
  196. view = "wdOutlineView";
  197. break;
  198. case 3:
  199. view = "wdPrintView";
  200. break;
  201. case 4:
  202. view = "wdPrintPreview";
  203. break;
  204. case 5:
  205. view = "wdMasterView";
  206. break;
  207. case 6:
  208. view = "wdWebView";
  209. break;
  210. case 7:
  211. view = "wdReadingView";
  212. break;
  213. case 8:
  214. view = "wdConflictView";
  215. break;
  216. }
  217. viewPage->querySubObject("View")->setProperty("Type",view);
  218. delete viewPage;
  219. }
  220. void QWord::insertMoveDown() //插入回车
  221. {
  222. QAxObject* selection = m_word->querySubObject("Selection");
  223. if(NULL== selection)
  224. {
  225. return;
  226. }
  227. selection->dynamicCall("TypeParagraph(void)");
  228. delete selection;
  229. }
  230. void QWord::insertText(const QString& text)
  231. {
  232. QAxObject* selection = m_word->querySubObject("Selection");
  233. if(NULL== selection)
  234. {
  235. return;
  236. }
  237. selection->dynamicCall("TypeText(const QString&)",text);
  238. delete selection;
  239. }
  240. QString QWord::GetText()
  241. {
  242. QAxObject* selection = m_word->querySubObject("Selection");
  243. QString str;
  244. if(NULL!= selection)
  245. {
  246. str = selection->dynamicCall("GetText(void)").toString();
  247. }
  248. delete selection;
  249. return str;
  250. }
  251. //设置选中位置文字居中 0 ,居左 1,居右 2
  252. void QWord::setParagraphAlignment(int flag)
  253. {
  254. QAxObject* selection = m_word->querySubObject("Selection");
  255. if(NULL== selection)
  256. {
  257. return;
  258. }
  259. if(flag == 0)
  260. {
  261. selection->querySubObject("ParagraphFormat")->setProperty("Alignment","wdAlignParagraphCenter");
  262. }else if(flag == 1)
  263. {
  264. selection->querySubObject("ParagraphFormat")->setProperty("Alignment","wdAlignParagraphJustify");
  265. }else if(flag == 2)
  266. {
  267. selection->querySubObject("ParagraphFormat")->setProperty("Alignment","wdAlignParagraphRight");
  268. }
  269. delete selection;
  270. }
  271. void QWord::setParagraphFormatOutlineLevel(QString level)
  272. {
  273. QAxObject* selection = m_word->querySubObject("Selection");
  274. if(NULL== selection)
  275. {
  276. return;
  277. }
  278. // QString wdOutlineLevel="wdOutlineLevel";
  279. selection->querySubObject("ParagraphFormat")->setProperty("OutlineLevel",level);
  280. delete selection;
  281. }
  282. void QWord::setRowAlignment(int tableIndex,int row,int flag)
  283. {
  284. QAxObject* tables = m_document->querySubObject("Tables");
  285. if(NULL== tables)
  286. {
  287. return;
  288. }
  289. QAxObject* table = tables->querySubObject("Item(int)",tableIndex);
  290. if(NULL== table )
  291. {
  292. return;
  293. }
  294. QAxObject* Row= table->querySubObject("Rows(int)",row);
  295. if(NULL== Row)
  296. {
  297. return;
  298. }
  299. QAxObject* range = Row->querySubObject("Range");
  300. if(NULL== range)
  301. {
  302. return;
  303. }
  304. Row->querySubObject("Alignment(int)",flag);
  305. if(flag == 0)
  306. {
  307. range->querySubObject("ParagraphFormat")->setProperty("Alignment","wdAlignParagraphCenter");
  308. }else if(flag == 1)
  309. {
  310. range->querySubObject("ParagraphFormat")->setProperty("Alignment","wdAlignParagraphJustify");
  311. }else if(flag == 2)
  312. {
  313. range->querySubObject("ParagraphFormat")->setProperty("Alignment","wdAlignParagraphRight");
  314. }else if(flag ==3)
  315. {
  316. range->querySubObject("ParagraphFormat")->setProperty("Alignment","wdAlignParagraphLeft");
  317. }
  318. }
  319. void QWord::setRowAlignment(int row,int flag)
  320. {
  321. QAxObject* selection = m_word->querySubObject("Selection");
  322. if(NULL== selection)
  323. {
  324. return;
  325. }
  326. QAxObject* table = selection->querySubObject("Tables(1)");
  327. if(NULL== table)
  328. {
  329. return;
  330. }
  331. // QAxObject* table = tables->querySubObject("Item(int)",tableIndex);
  332. // if(NULL== table )
  333. // {
  334. // return;
  335. // }
  336. QAxObject* Row= table->querySubObject("Rows(int)",row);
  337. if(NULL== Row)
  338. {
  339. return;
  340. }
  341. QAxObject* range = Row->querySubObject("Range");
  342. if(NULL== range)
  343. {
  344. return;
  345. }
  346. Row->querySubObject("Alignment(int)",flag);
  347. if(flag == 0)
  348. {
  349. range->querySubObject("ParagraphFormat")->setProperty("Alignment","wdAlignParagraphCenter");
  350. }else if(flag == 1)
  351. {
  352. range->querySubObject("ParagraphFormat")->setProperty("Alignment","wdAlignParagraphJustify");
  353. }else if(flag == 2)
  354. {
  355. range->querySubObject("ParagraphFormat")->setProperty("Alignment","wdAlignParagraphRight");
  356. }else if(flag ==3)
  357. {
  358. range->querySubObject("ParagraphFormat")->setProperty("Alignment","wdAlignParagraphLeft");
  359. }
  360. }
  361. void QWord::setFontSize(int fontsize) //设置字体大小
  362. {
  363. QAxObject* selection = m_word->querySubObject("Selection");
  364. if(NULL== selection)
  365. {
  366. return;
  367. }
  368. selection->querySubObject("Font")->setProperty("Size",fontsize);
  369. delete selection;
  370. }
  371. void QWord::setFontBold(bool flag)
  372. {
  373. QAxObject* selection = m_word->querySubObject("Selection");
  374. if(NULL== selection)
  375. {
  376. return;
  377. }
  378. selection->querySubObject("Font")->setProperty("Bold",flag);
  379. delete selection;
  380. }
  381. void QWord::setFontName(QString& fontName)
  382. {
  383. QAxObject* selection = m_word->querySubObject("Selection");
  384. if(NULL== selection)
  385. {
  386. return;
  387. }
  388. selection->querySubObject("Font")->setProperty("Name",fontName);
  389. delete selection;
  390. }
  391. void QWord::setSelectionRange(int start,int end)
  392. {
  393. QAxObject* selection = m_word->querySubObject("Selection");
  394. if(NULL== selection)
  395. {
  396. return;
  397. }
  398. selection->dynamicCall("SetRange(int, int)", start,end); //第1个字符后开始,到第9个字符结束范围
  399. }
  400. void QWord::getUsedRange(int *topLeftRow, int *topLeftColumn, int *bottomRightRow, int *bottomRightColumn)
  401. {
  402. QAxObject* range = m_document->querySubObject("Range");
  403. if(NULL== range)
  404. {
  405. return;
  406. }
  407. *topLeftRow = range->property("Row").toInt();
  408. if(NULL== topLeftRow)
  409. {
  410. return;
  411. }
  412. *topLeftColumn = range->property("Column").toInt();
  413. if(NULL== topLeftColumn)
  414. {
  415. return;
  416. }
  417. QAxObject *rows = range->querySubObject("Rows");
  418. if(NULL== rows)
  419. {
  420. return;
  421. }
  422. *bottomRightRow = *topLeftRow + rows->property("Count").toInt() - 1;
  423. if(NULL== bottomRightRow)
  424. {
  425. return;
  426. }
  427. QAxObject *columns = range->querySubObject("Columns");
  428. if(NULL== columns)
  429. {
  430. return;
  431. }
  432. *bottomRightColumn = *topLeftColumn + columns->property("Count").toInt() - 1;
  433. if(NULL== bottomRightColumn)
  434. {
  435. return;
  436. }
  437. }
  438. void QWord::intsertTable(int row,int column)
  439. {
  440. QAxObject* tables = m_document->querySubObject("Tables");
  441. if(NULL== tables)
  442. {
  443. return;
  444. }
  445. QAxObject* selection = m_word->querySubObject("Selection");
  446. if(NULL== selection)
  447. {
  448. return;
  449. }
  450. QAxObject* range = selection->querySubObject("Range");
  451. if(NULL== range)
  452. {
  453. return;
  454. }
  455. QVariantList params;
  456. params.append(range->asVariant());
  457. params.append(row);
  458. params.append(column);
  459. tables->querySubObject("Add(QAxObject*, int, int, QVariant&, QVariant&)", params);
  460. QAxObject* table = selection->querySubObject("Tables(int)",1);
  461. if(NULL== table)
  462. {
  463. return;
  464. }
  465. table->setProperty("Style","网格型");
  466. QAxObject* Borders = table->querySubObject("Borders");
  467. if(NULL== Borders)
  468. {
  469. return;
  470. }
  471. Borders->setProperty("InsideLineStyle",1);
  472. Borders->setProperty("OutsideLineStyle",1);
  473. /*QString doc = Borders->generateDocumentation();
  474. QFile outFile("D:\\360Downloads\\Picutres\\Borders.html");
  475. outFile.open(QIODevice::WriteOnly|QIODevice::Append);
  476. QTextStream ts(&outFile);
  477. ts<<doc<<endl;*/
  478. }
  479. void QWord::intsertTable(int tableIndex, int row,int column)
  480. {
  481. QAxObject* tables = m_document->querySubObject("Tables");
  482. if(NULL== tables)
  483. {
  484. return;
  485. }
  486. QAxObject* selection = m_word->querySubObject("Selection");
  487. if(NULL== selection)
  488. {
  489. return;
  490. }
  491. QAxObject* range = selection->querySubObject("Range");
  492. if(NULL== range)
  493. {
  494. return;
  495. }
  496. QVariantList params;
  497. params.append(range->asVariant());
  498. params.append(row);
  499. params.append(column);
  500. tables->querySubObject("Add(QAxObject*, int, int, QVariant&, QVariant&)", params);
  501. QAxObject* table = selection->querySubObject("Tables(int)",tableIndex);
  502. if(NULL== table)
  503. {
  504. return;
  505. }
  506. table->setProperty("Style","网格型");
  507. QAxObject* Borders = table->querySubObject("Borders");
  508. if(NULL== Borders)
  509. {
  510. return;
  511. }
  512. Borders->setProperty("InsideLineStyle",1);
  513. Borders->setProperty("OutsideLineStyle",1);
  514. /*QString doc = Borders->generateDocumentation();
  515. QFile outFile("D:\\360Downloads\\Picutres\\Borders.html");
  516. outFile.open(QIODevice::WriteOnly|QIODevice::Append);
  517. QTextStream ts(&outFile);
  518. ts<<doc<<endl;*/
  519. }
  520. void QWord::setColumnWidth(int column, int width) //设置列宽
  521. {
  522. QAxObject* selection = m_word->querySubObject("Selection");
  523. if(NULL== selection)
  524. {
  525. return;
  526. }
  527. QAxObject* table = selection->querySubObject("Tables(1)");
  528. if(NULL== table)
  529. {
  530. return;
  531. }
  532. table->querySubObject("Columns(int)",column)->setProperty("Width",width);
  533. }
  534. void QWord::setCellString(int nTable,int row,int column,const QString& text)
  535. {
  536. QAxObject* pTables =m_document->querySubObject("Tables");
  537. if(NULL==pTables)
  538. {
  539. return;
  540. }
  541. QAxObject* table=pTables->querySubObject("Item(int)",nTable);
  542. if(table)
  543. {
  544. table->querySubObject("Cell(int,int)",row,column)->querySubObject("Range")
  545. ->dynamicCall("SetText(QString)",text);
  546. }
  547. }
  548. void QWord::MergeCells(int tableIndex, int nStartRow,int nStartCol,int nEndRow,int nEndCol)//合并单元格
  549. {
  550. QAxObject* tables = m_document->querySubObject("Tables");
  551. if(NULL==tables)
  552. {
  553. return;
  554. }
  555. QAxObject* table = tables->querySubObject("Item(int)",tableIndex);
  556. if(NULL== table)
  557. {
  558. return;
  559. }
  560. if(table)
  561. {
  562. QAxObject* StartCell =table->querySubObject("Cell(int, int)",nStartRow,nStartCol);
  563. QAxObject* EndCell = table->querySubObject("Cell(int, int)",nEndRow,nEndCol);
  564. if(NULL==StartCell)
  565. {
  566. return;
  567. }
  568. if(NULL== EndCell)
  569. {
  570. return;
  571. }
  572. StartCell->querySubObject("Merge(QAxObject *)",EndCell->asVariant());
  573. }
  574. }
  575. //第二种方法调用
  576. // void QWord::MergeCells(int tableIndex, int nStartRow,int nStartCol,int nEndRow,int nEndCol)//合并单元格
  577. // {
  578. // QAxObject* tables = m_document->querySubObject("Tables");
  579. // QAxObject* table = tables->querySubObject("Item(int)",tableIndex);
  580. // QAxObject* StartCell =table->querySubObject("Cell(int, int)",nStartRow,nStartCol);
  581. // QAxObject* EndCell = table->querySubObject("Cell(int, int)",nEndRow,nEndCol);
  582. // StartCell->dynamicCall("Merge(LPDISPATCH)",EndCell->asVariant());
  583. // }
  584. void QWord::setColumnHeight(int nTable,int column, int height)
  585. {
  586. QAxObject* pTables =m_document->querySubObject("Tables");
  587. if(NULL==pTables)
  588. {
  589. return;
  590. }
  591. QAxObject* table=pTables->querySubObject("Item(int)",nTable);
  592. if(table)
  593. {
  594. table->querySubObject("Columns(int)",column)->setProperty("Hight",height);
  595. }
  596. }
  597. void QWord::setRowHeight(int nTable,int Row, int height)
  598. {
  599. QAxObject* pTables =m_document->querySubObject("Tables");
  600. if(NULL==pTables)
  601. {
  602. return;
  603. }
  604. QAxObject* table=pTables->querySubObject("Item(int)",nTable);
  605. if(table)
  606. {
  607. table->querySubObject("Rows(int)",Row)->setProperty("Hight",height);
  608. }
  609. }
  610. void QWord::setColumnHeight(int column, int height) //设置列高
  611. {
  612. QAxObject* selection = m_word->querySubObject("Selection");
  613. if(NULL== selection)
  614. {
  615. return;
  616. }
  617. QAxObject* table = selection->querySubObject("Tables(1)");
  618. if(table)
  619. {
  620. table->querySubObject("Columns(int)",column)->setProperty("Hight",height);
  621. }
  622. }
  623. void QWord::setCellString(int row, int column, const QString& text)
  624. {
  625. QAxObject* selection = m_word->querySubObject("Selection");
  626. if(NULL== selection)
  627. {
  628. return;
  629. }
  630. QAxObject* table = selection->querySubObject("Tables(1)");
  631. if(NULL== table)
  632. {
  633. return;
  634. }
  635. QAxObject* rows =table->querySubObject("Rows");
  636. if(rows)
  637. {
  638. return;
  639. }
  640. int Count =rows->dynamicCall("Count").toInt();
  641. table->querySubObject("Cell(int, int)",row,column)->querySubObject("Range")->dynamicCall("SetText(QString)", text);
  642. }
  643. void QWord::setCellFontBold(int row, int column, bool isBold) //设置内容粗体 isBold控制是否粗体
  644. {
  645. QAxObject* selection = m_word->querySubObject("Selection");
  646. if(NULL== selection)
  647. {
  648. return;
  649. }
  650. QAxObject* table = selection->querySubObject("Tables(1)");
  651. if(NULL== table)
  652. {
  653. return;
  654. }
  655. table->querySubObject("Cell(int, int)",row,column)->querySubObject("Range")->dynamicCall("SetBold(int)", isBold);
  656. }
  657. void QWord::setCellFontSize(int row, int column, int size) //设置文字大小
  658. {
  659. QAxObject* selection = m_word->querySubObject("Selection");
  660. if(NULL== selection)
  661. {
  662. return;
  663. }
  664. QAxObject* table = selection->querySubObject("Tables(1)");
  665. if(NULL== table)
  666. {
  667. return;
  668. }
  669. table->querySubObject("Cell(int, int)",row,column)->querySubObject("Range")->querySubObject("Font")->setProperty("Size", size);
  670. }
  671. QVariant QWord::getCellValue(int row, int column) //获取单元格内容 此处对于Excel来说列和行从1开始最少
  672. {
  673. QAxObject* selection = m_word->querySubObject("Selection");
  674. QAxObject* table = selection->querySubObject("Tables(1)");
  675. if(NULL!=selection&&NULL!=table)
  676. return table->querySubObject("Cell(int, int)",row,column)->querySubObject("Range")->property("Text");
  677. }
  678. int QWord::getTableCount()
  679. {
  680. QAxObject* tables = m_document->querySubObject("Tables");
  681. int val;
  682. if(NULL!= tables)
  683. {
  684. val = tables->property("Count").toInt();
  685. }
  686. return val;
  687. }
  688. void QWord::moveForEnd()
  689. {
  690. QAxObject* selection = m_word->querySubObject("Selection");
  691. if(NULL== selection)
  692. {
  693. return;
  694. }
  695. QVariantList params;
  696. params.append(6);
  697. params.append(0);
  698. selection->dynamicCall("EndOf(QVariant&, QVariant&)", params).toInt();
  699. }
  700. void QWord::insertCellPic(int row,int column,const QString& picPath)
  701. {
  702. QAxObject* selection = m_word->querySubObject("Selection");
  703. if(NULL== selection)
  704. {
  705. return;
  706. }
  707. QAxObject* table = selection->querySubObject("Tables(1)");
  708. if(NULL== table)
  709. {
  710. return;
  711. }
  712. QAxObject* range = table->querySubObject("Cell(int, int)",row,column)->querySubObject("Range");
  713. if(NULL== range)
  714. {
  715. return;
  716. }
  717. range->querySubObject("InlineShapes")->dynamicCall("AddPicture(const QString&)",picPath);
  718. }
  719. void QWord::setTableAutoFitBehavior(int flag)
  720. {
  721. QAxObject* selection = m_word->querySubObject("Selection");
  722. if(NULL== selection)
  723. {
  724. return;
  725. }
  726. QAxObject* table = selection->querySubObject("Tables(1)");
  727. if(NULL== table)
  728. {
  729. return;
  730. }
  731. if(0 <= flag &&flag <= 2 )
  732. table->dynamicCall("AutoFitBehavior(WdAutoFitBehavior)", flag);
  733. }
  734. void QWord::deleteSelectColumn(int column)
  735. {
  736. QAxObject* selection = m_word->querySubObject("Selection");
  737. if(NULL== selection)
  738. {
  739. return;
  740. }
  741. QAxObject* table = selection->querySubObject("Tables(1)");
  742. if(NULL== table)
  743. {
  744. return;
  745. }
  746. QAxObject* columns = table->querySubObject("Columns(int)",column);
  747. if(NULL== columns)
  748. {
  749. return;
  750. }
  751. columns->dynamicCall("Delete()");
  752. }
  753. void QWord::setOptionCheckSpell(bool flags)
  754. {
  755. QAxObject* opetions = m_word->querySubObject("Options");
  756. if(!opetions)
  757. return;
  758. opetions->setProperty("CheckGrammarAsYouType",flags);
  759. opetions->setProperty("CheckGrammarWithSpelling",flags);
  760. opetions->setProperty("ContextualSpeller",flags);
  761. opetions->setProperty("CheckSpellingAsYouType",flags);
  762. }
  763. void QWord::addTableRow(int tableIndex ,int nRow,int rowCount)
  764. {
  765. QAxObject* tables=m_document->querySubObject("Tables");
  766. if(NULL== tables)
  767. {
  768. return;
  769. }
  770. QAxObject* table = tables->querySubObject("Item(int)",tableIndex);
  771. if(NULL== table)
  772. {
  773. return;
  774. }
  775. QAxObject* rows =table->querySubObject("Rows");
  776. if(NULL== rows)
  777. {
  778. return;
  779. }
  780. int Count =rows->dynamicCall("Count").toInt();
  781. if(0<nRow && nRow <= Count )
  782. {
  783. for(int i =0; i<rowCount; ++i)
  784. {
  785. QString sPos = QString("Item(%1)").arg(nRow+i);
  786. QAxObject* row= rows->querySubObject(sPos.toStdString().c_str());
  787. QAxObject* row1= rows->querySubObject("Last");
  788. QAxObject* row2= rows->querySubObject("Row(int)", nRow+i);
  789. QAxObject* row3= rows->querySubObject("Item(int)",nRow+i);
  790. if (NULL != row)
  791. {
  792. QVariant param =row ->asVariant();
  793. /*rows->dynamicCall("Add(Variant)",param);*/
  794. QAxObject* NewRow = rows->querySubObject("Add(Variant)",param);
  795. }
  796. }
  797. }
  798. }
  799. ////创建表格
  800. void QWord::insertTable(int tableIndex,int row,int column)
  801. {
  802. QAxObject* tables=m_document->querySubObject("Tables");
  803. if(NULL==tables)
  804. {
  805. return;
  806. }
  807. QAxObject* table = tables->querySubObject("Item(int)",tableIndex);
  808. if(NULL==table)
  809. {
  810. return;
  811. }
  812. //QAxObject* rows =table->querySubObject("Rows");
  813. QAxObject* selection = m_word->querySubObject("Selection");
  814. if(NULL==selection)
  815. {
  816. return;
  817. }
  818. QAxObject* range = selection->querySubObject("Range");
  819. if(NULL==range)
  820. {
  821. return;
  822. }
  823. QVariantList params;
  824. params.append(range->asVariant());
  825. params.append(row);
  826. params.append(column);
  827. tables->querySubObject("Add(QAxObject*, int, int, QVariant&, QVariant&)", params);
  828. table = selection->querySubObject("Tables(int)",1);
  829. table->setProperty("Style","网格型");
  830. QAxObject* Borders = table->querySubObject("Borders");
  831. if(NULL==Borders)
  832. {
  833. return;
  834. }
  835. Borders->setProperty("InsideLineStyle",1);
  836. Borders->setProperty("OutsideLineStyle",1);
  837. }
  838. ////设置表格列宽
  839. void QWord::setColumnWidth(int nTable,int column,int width)
  840. {
  841. QAxObject* pTables =m_document->querySubObject("Tables");
  842. if(NULL== pTables)
  843. {
  844. return;
  845. }
  846. QAxObject* table=pTables->querySubObject("Item(int)",nTable);
  847. if(table)
  848. {
  849. table->querySubObject("Columns(int)",column)->setProperty("width",width);
  850. }
  851. }
  852. //在表格中插入图片
  853. void QWord::insertCellPic(int nTable,int row,int column,const QString& picPath)
  854. {
  855. QAxObject* pTables=m_document->querySubObject("Tables");
  856. if(NULL== pTables)
  857. {
  858. return;
  859. }
  860. QAxObject* table=pTables->querySubObject("Item(int)",nTable);
  861. if(NULL== table)
  862. {
  863. return;
  864. }
  865. QAxObject* range=table->querySubObject("Cell(int,int )",row,column)->querySubObject("Range");
  866. if(NULL== range)
  867. {
  868. return;
  869. }
  870. range->querySubObject("InlineShapes")->dynamicCall("AddPicture(const QString&)",picPath);
  871. }
  872. //设置内容粗体
  873. void QWord::setCellFontBold(int nTable,int row,int column,bool isBold)
  874. {
  875. QAxObject* pTables = m_document->querySubObject("Tables");
  876. if(NULL== pTables)
  877. {
  878. return;
  879. }
  880. QAxObject* table =pTables->querySubObject("Item(int)",nTable);
  881. if(NULL== table)
  882. {
  883. return;
  884. }
  885. table->querySubObject("Cell(int,int )",row,column)->querySubObject("Range")->dynamicCall("SetBold(int)",isBold);
  886. }
  887. //设置文字大小
  888. void QWord:: setCellFontSize(int nTable,int row,int column,int size)
  889. {
  890. QAxObject* pTables=m_document->querySubObject("Tables");
  891. if(NULL== pTables)
  892. {
  893. return;
  894. }
  895. QAxObject* table =pTables->querySubObject("Item(int)",nTable);
  896. if(NULL== table)
  897. {
  898. return;
  899. }
  900. table->querySubObject("Cell(int,int)",row,column)->querySubObject("Range")->querySubObject("Font")->setProperty("Size",size);
  901. }
  902. void QWord::setVisible(bool isVisible)
  903. {
  904. if(m_word != NULL)
  905. {
  906. m_word->setProperty("Visible",isVisible);
  907. }
  908. }
  909. void QWord::AddPicture(QString file)
  910. {
  911. QAxObject* selection = m_word->querySubObject("Selection");
  912. if(!selection)
  913. {
  914. return;
  915. }
  916. QString filename = file;
  917. filename.replace("/","\\");
  918. QAxObject *Inlineshapes = selection->querySubObject("InlineShapes");
  919. Inlineshapes->dynamicCall("AddPicture(const QString&)",filename);
  920. delete Inlineshapes;
  921. delete selection;
  922. }
  923. void QWord::insertEnter()
  924. {
  925. QAxObject* selection = m_word->querySubObject("Selection");
  926. if(!selection)
  927. {
  928. return;
  929. }
  930. selection->dynamicCall("TypeParagraph(void)");
  931. }
  932. QAxObject* QWord::createTableWithColor(int row, int column)
  933. {
  934. QAxObject* selection = m_word->querySubObject("Selection");
  935. if(!selection)
  936. {
  937. return NULL;
  938. }
  939. selection->dynamicCall("InsertAfter(QString&)", "\r\n");
  940. QAxObject *range = selection->querySubObject("Range");
  941. QAxObject *tables = m_document->querySubObject("Tables");
  942. QAxObject *table = tables->querySubObject("Add(QVariant,int,int)",range->asVariant(),row,column);
  943. table->setProperty("Style","网格型");
  944. QAxObject* Borders = table->querySubObject("Borders");
  945. if(NULL== Borders)
  946. {
  947. return NULL;
  948. }
  949. Borders->setProperty("InsideLineStyle",1);
  950. Borders->setProperty("OutsideLineStyle",1);
  951. table->querySubObject("Shading")->dynamicCall("SetBackgroundPatternColor", TABLE_BG_COLOR);
  952. //表格自动拉伸列 0固定 1根据内容调整 2 根据窗口调整
  953. table->dynamicCall("AutoFitBehavior(WdAutoFitBehavior)", wdAutoFitContent);
  954. // //设置表头
  955. // for(int i=0;i<headList.size();i++)
  956. // {
  957. // table->querySubObject("Cell(int,int)",1,i+1)->querySubObject("Range")->dynamicCall("SetText(QString)", headList.at(i));
  958. // //加粗
  959. // table->querySubObject("Cell(int,int)",1,i+1)->querySubObject("Range")->dynamicCall("SetBold(int)", true);
  960. // }
  961. // //表格自动拉伸列 0固定 1根据内容调整 2 根据窗口调整
  962. // table->dynamicCall("AutoFitBehavior(WdAutoFitBehavior)", 2);
  963. delete range;
  964. return table;
  965. }
  966. QAxObject* QWord::createTable(int row, int column)
  967. {
  968. QAxObject* selection = m_word->querySubObject("Selection");
  969. if(!selection)
  970. {
  971. return NULL;
  972. }
  973. selection->dynamicCall("InsertAfter(QString&)", "\r\n");
  974. QAxObject *range = selection->querySubObject("Range");
  975. QAxObject *tables = m_document->querySubObject("Tables");
  976. QAxObject *table = tables->querySubObject("Add(QVariant,int,int)",range->asVariant(),row,column);
  977. table->setProperty("Style","网格型");
  978. QAxObject* Borders = table->querySubObject("Borders");
  979. if(NULL== Borders)
  980. {
  981. return NULL;
  982. }
  983. Borders->setProperty("InsideLineStyle",1);
  984. Borders->setProperty("OutsideLineStyle",1);
  985. //table->querySubObject("Shading")->dynamicCall("SetBackgroundPatternColor", TABLE_BG_COLOR);
  986. //表格自动拉伸列 0固定 1根据内容调整 2 根据窗口调整
  987. table->dynamicCall("AutoFitBehavior(WdAutoFitBehavior)", wdAutoFitContent);
  988. // //设置表头
  989. // for(int i=0;i<headList.size();i++)
  990. // {
  991. // table->querySubObject("Cell(int,int)",1,i+1)->querySubObject("Range")->dynamicCall("SetText(QString)", headList.at(i));
  992. // //加粗
  993. // table->querySubObject("Cell(int,int)",1,i+1)->querySubObject("Range")->dynamicCall("SetBold(int)", true);
  994. // }
  995. // //表格自动拉伸列 0固定 1根据内容调整 2 根据窗口调整
  996. // table->dynamicCall("AutoFitBehavior(WdAutoFitBehavior)", 2);
  997. delete range;
  998. return table;
  999. }
  1000. void QWord::setCellText(QAxObject *table, int row, int column, QString text,bool bold)
  1001. {
  1002. table->querySubObject("Cell(int,int)",row,column)->querySubObject("Range")->dynamicCall("SetText(QString)", text);
  1003. table->querySubObject("Cell(int,int)",row,column)->querySubObject("Range")->dynamicCall("SetBold(int)", bold);
  1004. //table->querySubObject("Cell(int,int)",row,column)->querySubObject("Shading")->dynamicCall("SetBackgroundPatternColor", colorToInt(Qt::yellow));
  1005. }
  1006. void QWord::autoFitBehavior(QAxObject *table, int flag)
  1007. {
  1008. //表格自动拉伸列 0固定 1根据内容调整 2 根据窗口调整
  1009. table->dynamicCall("AutoFitBehavior(WdAutoFitBehavior)", flag);
  1010. }
  1011. void QWord::insertNewPage(void)
  1012. {
  1013. QAxObject* activeWindow = m_document->querySubObject( "ActiveWindow" );
  1014. QAxObject* selection = activeWindow->querySubObject( "Selection" );
  1015. selection->dynamicCall( "Collapse(int)", 0 );
  1016. selection->dynamicCall( "InsertNewPage()" );
  1017. selection->dynamicCall( "Collapse(int)", 0 );
  1018. delete selection;
  1019. delete activeWindow;
  1020. }
  1021. void QWord::setColor( QAxObject *obj,QColor color )
  1022. {
  1023. if (!obj) return;
  1024. obj->querySubObject("Range")
  1025. ->querySubObject("ParagraphFormat")
  1026. ->querySubObject("Shading")
  1027. ->setProperty("ForegroundPatternColor",colorToInt(color));
  1028. }
  1029. void QWord::setBgColor( QAxObject *obj,QColor color )
  1030. {
  1031. if (!obj) return;
  1032. obj->querySubObject("Range")
  1033. ->querySubObject("ParagraphFormat")
  1034. ->querySubObject("Shading")
  1035. ->setProperty("BackgroundPatternColor",colorToInt(color));
  1036. }
  1037. int QWord::colorToInt( QColor color )
  1038. {
  1039. int sum = 0;
  1040. int b = color.red() << 16;
  1041. int g = color.green() << 8;
  1042. int r = color.blue() ;
  1043. int al = color.alpha() <<24;
  1044. sum = al + r + g + b;
  1045. return sum;
  1046. }
  1047. /******************************************************************************
  1048. * 函数:addTableRow
  1049. * 功能:为表格添加行
  1050. * 参数:table 表格; nRow 插入行; rowCount 插入的行数
  1051. *****************************************************************************/
  1052. void QWord::addTableRow(QAxObject *table, int nRow, int rowCount)
  1053. {
  1054. QAxObject* rows = table->querySubObject("Rows");
  1055. int Count = rows->dynamicCall("Count").toInt();
  1056. if (nRow > Count)
  1057. {
  1058. nRow = Count;
  1059. }
  1060. QAxObject* row = table->querySubObject("Rows(int)",nRow);
  1061. if (row == NULL)
  1062. {
  1063. row = rows->querySubObject("Last");
  1064. }
  1065. if(0 <= nRow && nRow <=Count)
  1066. {
  1067. for(int i = 0; i < rowCount; ++i)
  1068. {
  1069. rows->dynamicCall("Add(QVariant)", row->asVariant());
  1070. }
  1071. }
  1072. }
  1073. void QWord::appendTableRow( QAxObject *table,int rowCount )
  1074. {
  1075. QAxObject* rows = table->querySubObject("Rows");
  1076. int Count = rows->dynamicCall("Count").toInt();
  1077. QAxObject* row = rows->querySubObject("Last");
  1078. for(int i = 0; i < rowCount; ++i)
  1079. {
  1080. QVariant param = row->asVariant();
  1081. rows->dynamicCall("Add(Variant)", param);
  1082. }
  1083. }
  1084. /******************************************************************************
  1085. * 函数:setColumnWidth
  1086. * 功能:设置表格列宽
  1087. * 参数:table 表格; column 列数; width 宽度
  1088. *****************************************************************************/
  1089. void QWord::setColumnWidth(QAxObject *table, int column, int width)
  1090. {
  1091. table->querySubObject("Columns(int)", column+1)->setProperty("Width", width);
  1092. }
  1093. void QWord::MergeCells(QAxObject *table, int nStartRow,int nStartCol,int nEndRow,int nEndCol)
  1094. {
  1095. QAxObject* StartCell =table->querySubObject("Cell(int, int)",nStartRow+1,nStartCol+1);
  1096. QAxObject* EndCell = table->querySubObject("Cell(int, int)",nEndRow+1,nEndCol+1);
  1097. StartCell->dynamicCall("Merge(LPDISPATCH)",EndCell->asVariant());
  1098. }
  1099. QString QWord::getTitleStr( TITLE_NUMBER number )
  1100. {
  1101. QString str;
  1102. switch (number)
  1103. {
  1104. case TITLE_ONE : str = "标题 1"; break;
  1105. case TITLE_TWO : str = "标题 2"; break;
  1106. case TITLE_THREE : str = "标题 3"; break;
  1107. default: str = "正文";break;
  1108. }
  1109. return str;
  1110. }
  1111. bool QWord::addText1( QString titlestr,TITLE_NUMBER number /*= NORMAL*/,WdParagraphAlignment alignment /*= AlignParLeft*/ )
  1112. {
  1113. if (!m_bOpened) return false;
  1114. QAxObject *selection = NULL;
  1115. selection = m_word->querySubObject("Selection");
  1116. if (selection)
  1117. {
  1118. selection->querySubObject("Range")->setProperty("Text", titlestr);
  1119. selection->querySubObject("Range")->dynamicCall("SetStyle(QVariant)", getTitleStr(number));
  1120. selection->querySubObject("ParagraphFormat")->setProperty("Alignment",alignment); //文本位置设置
  1121. //背景色
  1122. //selection->querySubObject("Range")
  1123. // ->querySubObject("ParagraphFormat")
  1124. // ->querySubObject("Shading")
  1125. // ->setProperty("BackgroundPatternColor",QColor("blue").value());
  1126. //moveToEnd();
  1127. return true;
  1128. }
  1129. return false;
  1130. }
  1131. bool QWord::addText( QString titlestr,QFont font,QColor fontcolor )
  1132. {
  1133. if (!m_bOpened) return false;
  1134. QAxObject *selection = NULL;
  1135. selection = m_word->querySubObject("Selection");
  1136. if (selection)
  1137. {
  1138. //selection->querySubObject("Range")->querySubObject("Font")->setProperty("Size", fo); //不好用
  1139. //selection->querySubObject("Range")->querySubObject("Font")->dynamicCall("Size", 20);
  1140. selection->querySubObject("Range")->querySubObject("Font")->setProperty("Size", QVariant(font.pointSize()));
  1141. selection->querySubObject("Range")->querySubObject("Font")->setProperty("Color", colorToInt(fontcolor));
  1142. if (font.weight() >= QFont::Bold)
  1143. {
  1144. selection->querySubObject("Range")->querySubObject("Font")->setProperty("Bold", true);
  1145. }
  1146. selection->querySubObject("Range")->setProperty("Text", titlestr); //方式1
  1147. //selection->dynamicCall("TypeText(const QString&)",titlestr); //方式2 使用方法2不能设置字体
  1148. // moveToEnd();
  1149. return true;
  1150. }
  1151. return false;
  1152. }
  1153. QAxObject* QWord::addText2( QString titlestr )
  1154. {
  1155. QAxObject *selection = NULL;
  1156. if (!m_bOpened) return selection;
  1157. selection = m_word->querySubObject("Selection");
  1158. if (selection)
  1159. {
  1160. selection->querySubObject("Range")->setProperty("Text", titlestr); //方式1
  1161. }
  1162. return selection;
  1163. }
  1164. void QWord::setTableBgColor(QAxObject *table, uint32_t color)
  1165. {
  1166. table->querySubObject("Shading")->dynamicCall("SetBackgroundPatternColor", TABLE_BG_COLOR);
  1167. }