123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- #ifndef QWORD_H
- #define QWORD_H
- #define _WIN32_DCOM
- #include <QMainWindow>
- #include <qmainwindow.h>
- #include <QString>
- #include <QVariant>
- #include <ActiveQt/QAxObject>
- #include <ActiveQt/QAxWidget>
- #include <objbase.h>
- //#include "GlobalAppData.h"
- //http://www.cxyzjd.com/article/chyuanrufeng/98778379
- class QWord : public QObject
- {
- Q_OBJECT
- public:
- QWord(QObject *parent = 0);
- ~QWord();
- public:
- #define ALIGNMENT_MIDDLE 0
- #define ALIGNMENT_LEFT 1
- #define ALIGNMENT_RIGHT 2
- #define wdAutoFitFixed 0
- #define wdAutoFitContent 1
- #define wdAutoFitWindow 2
- #define TABLE_BG_COLOR 0xffffffcc //ABGR
- enum TITLE_NUMBER
- {
- TITLE_ONE = 0,
- TITLE_TWO,
- TITLE_THREE,
- NORMAL
- };
- //MOVEEND_INDEX来自于https://docs.microsoft.com/zh-cn/dotnet/api/microsoft.office.interop.word.wdunits?view=word-pia
- enum MOVEEND_INDEX
- {
- wdParagraph = 4, //段落。
- wdStory = 6 , //部分。
- wdRow = 10, //行
- wdParagraphFormatting = 14, //段落格式。
- wdTable = 15 //表格
- };
- //文本对齐方式
- enum WdParagraphAlignment
- {
- AlignParLeft = 0, //左对齐
- AlignParCenter = 1, //居中对齐。
- AlignParRight = 2, //右对齐。
- AlignParJustify = 3, //完全两端对齐。
- };
- QAxObject* getDocuments(){return m_documents;}
- QAxObject* getDocument(){return m_document;}
- QAxObject* getWordApp(){return m_word;}
- public:
- /**************************************************************************/
- /* 文件 操作 */
- /**************************************************************************/
- void save(); //保存操作内容
- void close(); //关闭 退出 析构时候也会自动调用一次
- void saveAs(); //新建word另存为
- bool createWord(QString reportname ); //创建一个新的word文档
- void openWord(); //打开一个现有的word文档
- bool createNewWord(const QString& filePath ); //创建一个新的word文档
- void setTableAutoFitBehavior(int flag);//表格自动拉伸列: 0 固定 1根据内容调整 2 根据窗口调整
- void setPageOrientation(int flag); //设置页面0为纵向wdOrientPortrait 1为横向wdOrientLandscape
- void setWordPageView(int flag); //设置页面视图,是web视图wdWebView 6还是页面视图wdPrintView 3 或者阅读视图wdReadingView 7
- void setFontSize(int fontsize); //设置字体大小
- void setFontName(QString& fontName); //设置字体 比如 “宋体”
- void setFontBold(bool flag); //字体加粗
- void setParagraphAlignment(int flag); //设置选中位置文字居左 0 ,居中 1 ,居右 2
- void setRowAlignment(int tableIndex,int row,int flag);
- void setRowAlignment(int row,int flag);
- //同时设置列宽和列高可以固定图片的大小
- void setColumnWidth(int column, int width); //设置列宽
- void setColumnHeight(int column, int height); //设置列高
- void setRowHeight(int nTable,int Row, int height);
- void setCellString(int row, int column, const QString& text);
- void setCellFontBold(int row, int column, bool isBold); //设置内容粗体 isBold控制是否粗体
- void setCellFontSize(int row, int column, int size); //设置文字大小
- void setOptionCheckSpell(bool flags); //设置审阅的拼写检查 true开启检查 false 取消检查
- QString GetText(); //获取内容
- void getUsedRange(int *topLeftRow, int *topLeftColumn, int *bottomRightRow, int *bottomRightColumn);
- void setSelectionRange(int start,int end); //"SetRange(1, 9)"第1个字符后开始,到第9个字符结束范围
- QVariant getCellValue(int row, int column); //获取单元格内容
- int getTableCount(); //获取word中表格总数
- QString getStrErrorInfo(){return m_strError;} //获取代码中出现的错误信息可以用QMessageBox::information打印 在cpp不用QMessageBox 是怕你们在线程中调导出报表
- void deleteSelectColumn(int column); //删除指定的列
- void moveForEnd(); //移动选定对象到文档末尾
- void insertCellPic(int row,int column,const QString& picPath); //单元格插入图片
- void intsertTable(int row,int column); //插入一个几行几列表格
- void insertMoveDown(); //插入回车
- void insertText(const QString& text); //插入文字
- void insertTable(int tableIndex,int row,int column);
- //插入巡检单位 巡检计划 巡检时间 巡 检 人 顺序传参
- //void insertTitle(const QString& str1,const QString& str2,const QString& str3,const QString& str4);
- void MergeCells(int tableIndex, int nStartRow,int nStartCol,int nEndRow,int nEndCol);//合并单元格
- bool open(const QString& strFilePath,bool bVisable);
- bool openword(bool bVisable);
- bool isOpen();
- void setColumnWidth(int nTable,int column,int width);
- void setCellString(int nTable,int row,int column,const QString& text);
- void insertCellPic(int nTable,int row,int column,const QString& picPath);
- void setCellFontBold(int nTable,int row,int column,bool isBold);
- void setCellFontSize(int nTable,int row,int column,int size);
- //void addTableRow(int nTable ,int nRow,int rowCount);
- void addTableRow(int tableIndex ,int nRow,int rowCount);
- void setColumnHeight(int nTable,int column, int height);
- void intsertTable(int tableIndex, int row,int column);
- void AddPicture(QString file);
- void insertEnter(void);
- QAxObject* createTable(int row, int column);
- QAxObject* createTableWithColor(int row, int column);
- void setCellText(QAxObject *table, int row, int column, QString text,bool bold);
- void autoFitBehavior(QAxObject *table, int flag);
- void insertNewPage(void);
- void setColor( QAxObject *obj,QColor color );
- void setBgColor( QAxObject *obj,QColor color );
- int colorToInt( QColor color );
- void addTableRow(QAxObject *table, int nRow, int rowCount);
- void appendTableRow( QAxObject *table,int rowCount );
- void setColumnWidth(QAxObject *table, int column, int width);
- void MergeCells(QAxObject *table, int nStartRow,int nStartCol,int nEndRow,int nEndCol);
- QString getTitleStr( TITLE_NUMBER number );
- bool addText1( QString titlestr,TITLE_NUMBER number /*= NORMAL*/,WdParagraphAlignment alignment /*= AlignParLeft*/ );
- bool addText( QString titlestr,QFont font,QColor fontcolor );
- QAxObject* addText2( QString titlestr );
- void setTableBgColor( QAxObject *table,uint32_t color );
- void setParagraphFormatOutlineLevel(QString level);
- public:
- void setVisible(bool isVisible);
- private:
- QAxObject* m_word;
- QAxObject* m_documents;
- QAxObject* m_document;
- QString m_fileName;
- QString m_saveName;
- QString m_strError;
- QString m_strFilePath;
- bool m_bOpened;
- };
- #endif // QWORD_H
|