利用QT生成Word文档.doc_第1页
利用QT生成Word文档.doc_第2页
利用QT生成Word文档.doc_第3页
利用QT生成Word文档.doc_第4页
利用QT生成Word文档.doc_第5页
已阅读5页,还剩4页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

前段时间因一个项目的需要,需要用Qt生成Word报告,网上查阅并借鉴了相关资料终于解决了基本的问题。本文档中主要是基本数据的填充、数据表格和图片几种类型,所以是用word模板(.dot)和书签进行操作。(网上看有种方法是利用宏来进行操作,表示不是很懂,哈哈)。一、主要方法1、根据报告要求,设计word模板,定义好需要插入数据的标签,调好其他固定内容的格式。(以保证数据插入后文档内容排列有序)2、编写word操作接口类,主要包括:打开、关闭、保存、插入文本、插入图片、插入表格、相关格式调整函数3、编写生成报告界面类,调用接口函数生成word文档。二、接口函数WordEngine.h文件cppview plaincopy1. #ifndefWORDENGINE_H2. #defineWORDENGINE_H3. #include4. #include5. #include6. classWordEngine:publicQObject7. 8. Q_OBJECT9. public:10. WordEngine();11. WordEngine();12. 13. /打开Word文件,如果sFile路径为空或错误,则打开新的Word文档14. boolOpen(QStringsFile,boolbVisible=true);15. 16. voidsave(QStringsSavePath);17. 18. voidclose(boolbSave=true);19. 20. boolreplaceText(QStringsLabel,QStringsText);21. boolreplacePic(QStringsLabel,QStringsFile);22. /插入一个几行几列表格23. QAxObject*insertTable(QStringsLabel,introw,intcolumn);24. /插入一个几行几列表格并设置表头25. QAxObject*insertTable(QStringsLabel,introw,intcolumn,QStringListheadList);26. /设置列宽27. voidsetColumnWidth(QAxObject*table,intcolumn,intwidth);28. voidSetTableCellString(QAxObject*table,introw,intcolumn,QStringtext);29. 30. private:31. 32. QAxObject*m_pWord;/指向整个Word应用程序33. QAxObject*m_pWorkDocuments;/指向文档集,Word有很多文档34. QAxObject*m_pWorkDocument;/指向m_sFile对应的文档,就是要操作的文档35. 36. QStringm_sFile;37. boolm_bIsOpen;38. boolm_bNewFile;39. ;40. 41. #endif/WORDENGINE_HWordEngine.cpp文件cppview plaincopy1. #includeWordEngine.h2. #includeqt_windows.h3. WordEngine:WordEngine()4. 5. m_pWord=NULL;6. m_pWorkDocuments=NULL;7. m_pWorkDocument=NULL;8. 9. m_bIsOpen=false;10. m_bNewFile=false;11. 12. HRESULTresult=OleInitialize(0);13. 14. if(result!=S_OK&result!=S_FALSE)15. 16. qDebug()setControl(word.Application);33. if(!bFlag)34. 35. returnfalse;36. 37. m_pWord-setProperty(Visible,bVisible);38. /获取所有的工作文档39. QAxObject*document=m_pWord-querySubObject(Documents);40. if(!document)41. 42. returnfalse;43. 44. /以文件template.dot为模版新建一个文档45. document-dynamicCall(Add(QString),sFile);46. /获取当前激活的文档47. m_pWorkDocument=m_pWord-querySubObject(ActiveDocument);48. if(m_pWorkDocument)49. m_bIsOpen=true;50. else51. m_bIsOpen=false;52. 53. returnm_bIsOpen;54. 55. 56. voidWordEngine:save(QStringsSavePath)57. 58. if(m_bIsOpen&m_pWorkDocument)59. 60. if(m_bNewFile)61. m_pWorkDocument-dynamicCall(Save();62. 63. else64. /m_pWorkDocument-dynamicCall(SaveAs(constQString&,int,constQString&,constQString&,bool,bool),65. /m_sFile,56,QString(),QString(),false,false);66. m_pWorkDocument-dynamicCall(SaveAs(constQString&),sSavePath);67. 68. 69. qDebug()setProperty(DisplayAlerts,true);79. 80. if(m_pWorkDocument)81. m_pWorkDocument-dynamicCall(Close(bool),true);82. 83. if(m_pWord)84. m_pWord-dynamicCall(Quit();85. 86. if(m_pWorkDocuments)87. 88. deletem_pWorkDocuments;89. 90. if(m_pWord)91. 92. deletem_pWord;93. 94. m_pWorkDocument=NULL;95. m_pWorkDocuments=NULL;96. m_pWord=NULL;97. 98. m_bIsOpen=false;99. m_bNewFile=false;100. 101. 102. 103. boolWordEngine:replaceText(QStringsLabel,QStringsText)104. 105. if(!m_pWorkDocument)106. returnfalse;107. 108. /获取文档中名字为sLabel的标签109. QAxObject*pBookmark=m_pWorkDocument-querySubObject(Bookmarks(QString),sLabel);110. if(pBookmark)111. 112. pBookmark-dynamicCall(Select(void);113. pBookmark-querySubObject(Range)-setProperty(Text,sText);114. deletepBookmark;115. 116. returntrue;117. 118. 119. boolWordEngine:replacePic(QStringsLabel,QStringsFile)120. 121. if(!m_pWorkDocument)122. returnfalse;123. 124. QAxObject*bookmark_pic=m_pWorkDocument-querySubObject(Bookmarks(QString),sLabel);125. if(bookmark_pic)126. 127. bookmark_pic-dynamicCall(Select(void);128. QAxObject*Inlineshapes=m_pWorkDocument-querySubObject(InlineShapes);129. Inlineshapes-dynamicCall(AddPicture(constQString&),sFile);130. deleteInlineshapes;131. 132. returntrue;133. 134. 135. QAxObject*WordEngine:insertTable(QStringsLabel,introw,intcolumn)136. 137. QAxObject*bookmark=m_pWorkDocument-querySubObject(Bookmarks(QVariant),sLabel);138. if(bookmark)139. 140. bookmark-dynamicCall(Select(void);141. QAxObject*selection=m_pWord-querySubObject(Selection);142. 143. selection-dynamicCall(InsertAfter(QString&),n);144. /selection-dynamicCall(MoveLeft(int),1);145. selection-querySubObject(ParagraphFormat)-dynamicCall(Alignment,wdAlignParagraphCenter);146. /selection-dynamicCall(TypeText(QString&),TableTest);/设置标题147. 148. QAxObject*range=selection-querySubObject(Range);149. QAxObject*tables=m_pWorkDocument-querySubObject(Tables);150. QAxObject*table=tables-querySubObject(Add(QVariant,int,int),range-asVariant(),row,column);151. 152. for(inti=1;iquerySubObject(str.toAscii().constData();156. borders-dynamicCall(SetLineStyle(int),1);157. 158. returntable;159. 160. 161. 162. QAxObject*WordEngine:insertTable(QStringsLabel,introw,intcolumn,QStringListheadList)163. 164. QAxObject*bookmark=m_pWorkDocument-querySubObject(Bookmarks(QVariant),sLabel);165. if(headList.size()!=column)166. returnNULL;167. 168. if(bookmark)169. 170. bookmark-dynamicCall(Select(void);171. QAxObject*selection=m_pWord-querySubObject(Selection);172. 173. selection-dynamicCall(InsertAfter(QString&),rn);174. /selection-dynamicCall(MoveLeft(int),1);175. selection-querySubObject(ParagraphFormat)-dynamicCall(Alignment,wdAlignParagraphCenter);176. /设置标题177. /selection-dynamicCall(TypeText(QString&),TableTest);178. 179. QAxObject*range=selection-querySubObject(Range);180. QAxObject*tables=m_pWorkDocument-querySubObject(Tables);181. QAxObject*table=tables-querySubObject(Add(QVariant,int,int),range-asVariant(),row,column);182. /表格自动拉伸列0固定1根据内容调整2根据窗口调整183. table-dynamicCall(AutoFitBehavior(WdAutoFitBehavior),2);184. 185. /设置表头186. for(inti=0;iquerySubObject(Cell(int,int),1,i+1)-querySubObject(Range)-dynamicCall(SetText(QString),headList.at(i);188. /加粗189. table-querySubObject(Cell(int,int),1,i+1)-querySubObject(Range)-dynamicCall(SetBold(int),true);190. 191. 192. for(inti=1;iquerySubObject(str.toAscii().constData();196. borders-dynamicCall(SetLineStyle(int),1

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论