qword.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #ifndef QWORD_H
  2. #define QWORD_H
  3. #define _WIN32_DCOM
  4. #include <QMainWindow>
  5. #include <qmainwindow.h>
  6. #include <QString>
  7. #include <QVariant>
  8. #include <ActiveQt/QAxObject>
  9. #include <ActiveQt/QAxWidget>
  10. #include <objbase.h>
  11. //#include "GlobalAppData.h"
  12. //http://www.cxyzjd.com/article/chyuanrufeng/98778379
  13. class QWord : public QObject
  14. {
  15. Q_OBJECT
  16. public:
  17. QWord(QObject *parent = 0);
  18. ~QWord();
  19. public:
  20. #define ALIGNMENT_MIDDLE 0
  21. #define ALIGNMENT_LEFT 1
  22. #define ALIGNMENT_RIGHT 2
  23. #define wdAutoFitFixed 0
  24. #define wdAutoFitContent 1
  25. #define wdAutoFitWindow 2
  26. #define TABLE_BG_COLOR 0xffffffcc //ABGR
  27. enum TITLE_NUMBER
  28. {
  29. TITLE_ONE = 0,
  30. TITLE_TWO,
  31. TITLE_THREE,
  32. NORMAL
  33. };
  34. //MOVEEND_INDEX来自于https://docs.microsoft.com/zh-cn/dotnet/api/microsoft.office.interop.word.wdunits?view=word-pia
  35. enum MOVEEND_INDEX
  36. {
  37. wdParagraph = 4, //段落。
  38. wdStory = 6 , //部分。
  39. wdRow = 10, //行
  40. wdParagraphFormatting = 14, //段落格式。
  41. wdTable = 15 //表格
  42. };
  43. //文本对齐方式
  44. enum WdParagraphAlignment
  45. {
  46. AlignParLeft = 0, //左对齐
  47. AlignParCenter = 1, //居中对齐。
  48. AlignParRight = 2, //右对齐。
  49. AlignParJustify = 3, //完全两端对齐。
  50. };
  51. QAxObject* getDocuments(){return m_documents;}
  52. QAxObject* getDocument(){return m_document;}
  53. QAxObject* getWordApp(){return m_word;}
  54. public:
  55. /**************************************************************************/
  56. /* 文件 操作 */
  57. /**************************************************************************/
  58. void save(); //保存操作内容
  59. void close(); //关闭 退出 析构时候也会自动调用一次
  60. void saveAs(); //新建word另存为
  61. bool createWord(QString reportname ); //创建一个新的word文档
  62. void openWord(); //打开一个现有的word文档
  63. bool createNewWord(const QString& filePath ); //创建一个新的word文档
  64. void setTableAutoFitBehavior(int flag);//表格自动拉伸列: 0 固定 1根据内容调整 2 根据窗口调整
  65. void setPageOrientation(int flag); //设置页面0为纵向wdOrientPortrait 1为横向wdOrientLandscape
  66. void setWordPageView(int flag); //设置页面视图,是web视图wdWebView 6还是页面视图wdPrintView 3 或者阅读视图wdReadingView 7
  67. void setFontSize(int fontsize); //设置字体大小
  68. void setFontName(QString& fontName); //设置字体 比如 “宋体”
  69. void setFontBold(bool flag); //字体加粗
  70. void setParagraphAlignment(int flag); //设置选中位置文字居左 0 ,居中 1 ,居右 2
  71. void setRowAlignment(int tableIndex,int row,int flag);
  72. void setRowAlignment(int row,int flag);
  73. //同时设置列宽和列高可以固定图片的大小
  74. void setColumnWidth(int column, int width); //设置列宽
  75. void setColumnHeight(int column, int height); //设置列高
  76. void setRowHeight(int nTable,int Row, int height);
  77. void setCellString(int row, int column, const QString& text);
  78. void setCellFontBold(int row, int column, bool isBold); //设置内容粗体 isBold控制是否粗体
  79. void setCellFontSize(int row, int column, int size); //设置文字大小
  80. void setOptionCheckSpell(bool flags); //设置审阅的拼写检查 true开启检查 false 取消检查
  81. QString GetText(); //获取内容
  82. void getUsedRange(int *topLeftRow, int *topLeftColumn, int *bottomRightRow, int *bottomRightColumn);
  83. void setSelectionRange(int start,int end); //"SetRange(1, 9)"第1个字符后开始,到第9个字符结束范围
  84. QVariant getCellValue(int row, int column); //获取单元格内容
  85. int getTableCount(); //获取word中表格总数
  86. QString getStrErrorInfo(){return m_strError;} //获取代码中出现的错误信息可以用QMessageBox::information打印 在cpp不用QMessageBox 是怕你们在线程中调导出报表
  87. void deleteSelectColumn(int column); //删除指定的列
  88. void moveForEnd(); //移动选定对象到文档末尾
  89. void insertCellPic(int row,int column,const QString& picPath); //单元格插入图片
  90. void intsertTable(int row,int column); //插入一个几行几列表格
  91. void insertMoveDown(); //插入回车
  92. void insertText(const QString& text); //插入文字
  93. void insertTable(int tableIndex,int row,int column);
  94. //插入巡检单位 巡检计划 巡检时间 巡 检 人 顺序传参
  95. //void insertTitle(const QString& str1,const QString& str2,const QString& str3,const QString& str4);
  96. void MergeCells(int tableIndex, int nStartRow,int nStartCol,int nEndRow,int nEndCol);//合并单元格
  97. bool open(const QString& strFilePath,bool bVisable);
  98. bool openword(bool bVisable);
  99. bool isOpen();
  100. void setColumnWidth(int nTable,int column,int width);
  101. void setCellString(int nTable,int row,int column,const QString& text);
  102. void insertCellPic(int nTable,int row,int column,const QString& picPath);
  103. void setCellFontBold(int nTable,int row,int column,bool isBold);
  104. void setCellFontSize(int nTable,int row,int column,int size);
  105. //void addTableRow(int nTable ,int nRow,int rowCount);
  106. void addTableRow(int tableIndex ,int nRow,int rowCount);
  107. void setColumnHeight(int nTable,int column, int height);
  108. void intsertTable(int tableIndex, int row,int column);
  109. void AddPicture(QString file);
  110. void insertEnter(void);
  111. QAxObject* createTable(int row, int column);
  112. QAxObject* createTableWithColor(int row, int column);
  113. void setCellText(QAxObject *table, int row, int column, QString text,bool bold);
  114. void autoFitBehavior(QAxObject *table, int flag);
  115. void insertNewPage(void);
  116. void setColor( QAxObject *obj,QColor color );
  117. void setBgColor( QAxObject *obj,QColor color );
  118. int colorToInt( QColor color );
  119. void addTableRow(QAxObject *table, int nRow, int rowCount);
  120. void appendTableRow( QAxObject *table,int rowCount );
  121. void setColumnWidth(QAxObject *table, int column, int width);
  122. void MergeCells(QAxObject *table, int nStartRow,int nStartCol,int nEndRow,int nEndCol);
  123. QString getTitleStr( TITLE_NUMBER number );
  124. bool addText1( QString titlestr,TITLE_NUMBER number /*= NORMAL*/,WdParagraphAlignment alignment /*= AlignParLeft*/ );
  125. bool addText( QString titlestr,QFont font,QColor fontcolor );
  126. QAxObject* addText2( QString titlestr );
  127. void setTableBgColor( QAxObject *table,uint32_t color );
  128. void setParagraphFormatOutlineLevel(QString level);
  129. public:
  130. void setVisible(bool isVisible);
  131. private:
  132. QAxObject* m_word;
  133. QAxObject* m_documents;
  134. QAxObject* m_document;
  135. QString m_fileName;
  136. QString m_saveName;
  137. QString m_strError;
  138. QString m_strFilePath;
  139. bool m_bOpened;
  140. };
  141. #endif // QWORD_H