├── .gitattributes ├── .gitignore ├── LICENSE ├── README.md ├── excelengine.cpp ├── excelengine.h ├── excelnode.cpp └── excelnode.h /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | 7 | # Standard to msysgit 8 | *.doc diff=astextplain 9 | *.DOC diff=astextplain 10 | *.docx diff=astextplain 11 | *.DOCX diff=astextplain 12 | *.dot diff=astextplain 13 | *.DOT diff=astextplain 14 | *.pdf diff=astextplain 15 | *.PDF diff=astextplain 16 | *.rtf diff=astextplain 17 | *.RTF diff=astextplain 18 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Windows image file caches 2 | Thumbs.db 3 | ehthumbs.db 4 | 5 | # Folder config file 6 | Desktop.ini 7 | 8 | # Recycle Bin used on file shares 9 | $RECYCLE.BIN/ 10 | 11 | # Windows Installer files 12 | *.cab 13 | *.msi 14 | *.msm 15 | *.msp 16 | 17 | # Windows shortcuts 18 | *.lnk 19 | 20 | # ========================= 21 | # Operating System Files 22 | # ========================= 23 | 24 | # OSX 25 | # ========================= 26 | 27 | .DS_Store 28 | .AppleDouble 29 | .LSOverride 30 | 31 | # Thumbnails 32 | ._* 33 | 34 | # Files that might appear in the root of a volume 35 | .DocumentRevisions-V100 36 | .fseventsd 37 | .Spotlight-V100 38 | .TemporaryItems 39 | .Trashes 40 | .VolumeIcon.icns 41 | 42 | # Directories potentially created on remote AFP share 43 | .AppleDB 44 | .AppleDesktop 45 | Network Trash Folder 46 | Temporary Items 47 | .apdisk 48 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Copyright (c) 2017 ThreeDog 3 | 4 | "THE Sprite-WARE LICENSE" (Revision 1): 5 | ->https://www.github.com/TheThreeDog wrote this file. 6 | As long as you retain this notice you can do whatever you want with this stuff. 7 | If we meet some day, and you think this stuff is worth it, you can buy me a super bottles of Sprite in return. 8 | fork from:https://en.wikipedia.org/wiki/Beerware & https://github.com/bitdust/WamaCry/blob/master/LICENSE.txt 9 | of course, you can copy this and modify it again. 10 | 11 | “雪碧软件协议”(第一版): 12 | ->https://www.github.com/TheThreeDog 编写了此文件。 13 | 只要你还保留本协议文本,你可以以使用此软件做任何事。 14 | 如果我们在某一天相遇了,而且你认为此软件很有价值,你可以为我买一大瓶雪碧来答谢。 15 | 此协议抄袭自:https://en.wikipedia.org/wiki/Beerware 和 https://github.com/bitdust/WamaCry/blob/master/LICENSE.txt 16 | 当然,你可以按照你的喜好来修改此协议. 17 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | ## 名称: 3 | - ExcelEngine 4 | 5 | ## 软件简介 6 | - 一个基于Qt的Excel操作引擎,封装了操作Excel文件的部分接口,相比直接使用QAxObject更加方便,代码可读性也更好。 7 | 8 | ## 说明: 9 | - 开发平台: Windows 7 X64. 10 | - 开发环境: Qt 5.7 11 | - 第一次在项目中需要时需要读写Excel文件,第一版直接使用QAxObject,类的封装结构不够好,代码也很乱,所以封装了一个单例的ExcelEngine类,可以通过ExcelEngine的外部接口读写操作Excel的内容。QAxObject中还提供了更多的更丰富的功能,我这里并没能全部封装,只有最基本的单元格的读写操作接口。 12 | 13 | ## 用法: 14 | - static ExcelEngine * getInstance(QWidget* parent = 0); //获取单例模式下的对象 15 | - int getRow_start() const; //获取起始行 16 | - void setRow_start(int value); //设置起始行 17 | 18 | - int getColumn_start() const; //获取起始列 19 | - void setColumn_start(int value); //设置起始列 20 | 21 | - int getRow_count() const; //获取行数 22 | - void setRow_count(int value); //设置行数 23 | 24 | - int getColumn_count() const; //获取列数 25 | - void setColumn_count(int value); //设置列数 26 | 27 | - bool createExcelFile(const QString &fileName); //创建一个Excel文件 28 | - void setExcelVisible(const bool visible); //设置Excel是否可见 29 | - void setSheetName(const QString &name); //设置工作表名称 30 | - bool writeText(int x, int y ,const QString &text); //向单元格写入文字 31 | - bool readText(); //读取单元格中的数据 32 | - void setRowHeight(const int i); //设置行高 33 | - void setColumnWidth(const int i); //设置列宽 34 | - void setWarpText(const bool b); //设置自动换行 35 | - void setBorderColor(const QColor &color); //设置边框颜色 36 | - void setFontFamily(const QString &family); //设置字体 37 | - void setFontSize(const int s); //设置字体大小 38 | - void setFontItalic(const bool b); //设置是否倾斜 39 | - void setFontBold(const bool b); //设置字体加粗 40 | - void setBackgroundColor(const QColor & color); //设置单元格背景颜色 41 | - void clearCell(); //清空单元格内容 42 | - void setActiveExcel(const QString &fileName); //设置当前操作的Excel文件 43 | - void closeActiveExcel(); //关闭当前正在操作的Excel文件 44 | - void saveExcel(); //保存 45 | - void saveAsExcel(const QString &fileName); //另存为 46 | - QString getExcelTitle(); //获取文件的标题 47 | - QString getExcelValue(int x,int y); //获取单元格中的数据 48 | 49 | 50 | ## 开源协议: 51 | - 雪碧软件协议 52 | -------------------------------------------------------------------------------- /excelengine.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************** 2 | * File Name : excelengine.cpp 3 | * Author : ThreeDog 4 | * Date : Tue Jul 18 13:36:13 2017 5 | * Description : 操作Excel的类的封装。 6 | * 7 | **************************************************************/ 8 | 9 | #include "excelengine.h" 10 | #include 11 | 12 | ExcelEngine* ExcelEngine::s_pExcelEngine = NULL; 13 | 14 | ExcelEngine::ExcelEngine(QWidget *parent) 15 | :QWidget(parent) 16 | { 17 | m_pExcel = new QAxObject("Excel.Application"); 18 | m_bIsVisible = false; 19 | m_pExcel->setProperty("Visible",m_bIsVisible); 20 | m_pWorkBooks = m_pExcel->querySubObject("WorkBooks"); 21 | m_pExcel->setProperty("Caption", "Qt Excel"); 22 | m_iRowHeight = 30; 23 | m_iColumnWidth = 10; 24 | m_bWarpText = true; 25 | m_cBorderColor = QColor(0,0,0); 26 | m_sFontFamily = "微软雅黑"; 27 | m_iFontSize = 10; 28 | m_bFontItalic = false; 29 | m_bFontBold = false; 30 | m_cBackgroundColor = QColor(255,255,255); 31 | } 32 | 33 | ExcelEngine *ExcelEngine::getInstance(QWidget *parent) 34 | { 35 | if(s_pExcelEngine == NULL){ 36 | s_pExcelEngine = new ExcelEngine(parent); 37 | return s_pExcelEngine; 38 | } 39 | else 40 | return s_pExcelEngine; 41 | } 42 | 43 | bool ExcelEngine::createExcelFile(const QString &fileName) 44 | { 45 | //qDebug()<<"in createExcelFile filename:"<setControl("Excel.Application"); 52 | p->dynamicCall("SetVisible(bool)",false); 53 | p->setProperty("DisplayAlerts",false); 54 | QAxObject *pp = p->querySubObject("Workbooks"); 55 | //qDebug()<<"Create Excel File Success"; 56 | QFile file(fileName); 57 | QAxObject * ppp; 58 | if(!file.exists()){ 59 | pp->dynamicCall("Add"); 60 | ppp = p->querySubObject("ActiveWorkBook"); 61 | ppp->dynamicCall("SaveAs(const QString &)",fileName); 62 | 63 | ppp->dynamicCall("Close(Boolean)",false); 64 | p->dynamicCall("Quit(void)"); 65 | } 66 | //qDebug()<<"Create Excel File Success"; 67 | return true; 68 | } 69 | 70 | void ExcelEngine::setExcelVisible(const bool visible) 71 | { 72 | m_bIsVisible = visible; 73 | //m_pExcel->setProperty("Visible",visible); 74 | } 75 | 76 | void ExcelEngine::setSheetName(const QString &name) 77 | { 78 | m_pWorkSheet->setProperty("Name",name); 79 | } 80 | 81 | //第x行,第y列 82 | bool ExcelEngine::writeText(int x, int y, const QString &text) 83 | { 84 | m_pCell = m_pWorkSheet->querySubObject("Cells(int,int)", x, y); 85 | this->applyCellSettings(); 86 | m_pCell->setProperty("Value", text); //设置单元格文字 87 | return true; 88 | } 89 | 90 | bool ExcelEngine::readText() 91 | { 92 | int sheet_count = m_pWorkSheets->property("Count").toInt(); 93 | if(sheet_count > 0) 94 | { 95 | QAxObject *work_sheet = m_pWorkBook->querySubObject("Sheets(int)", 1); 96 | QAxObject *used_range = work_sheet->querySubObject("UsedRange"); 97 | QAxObject *rows = used_range->querySubObject("Rows"); 98 | QAxObject *columns = used_range->querySubObject("Columns"); 99 | row_start = used_range->property("Row").toInt(); //获取起始行 100 | column_start = used_range->property("Column").toInt(); //获取起始列 101 | row_count = rows->property("Count").toInt(); //获取行数 102 | column_count = columns->property("Count").toInt(); //获取列数 103 | for(int i=row_start; i <= row_count; i++) 104 | { 105 | for(int j=column_start; j <= column_count;j++) 106 | { 107 | QAxObject *cell = work_sheet->querySubObject("Cells(int,int)", i, j); 108 | QString value = cell->property("Value").toString(); 109 | ExcelNode * new_node = new ExcelNode(i,j,value); 110 | m_vExcleNodeList.append(new_node); 111 | 112 | } 113 | } 114 | return true; 115 | }else 116 | return false; 117 | } 118 | 119 | void ExcelEngine::setRowHeight(const int i) 120 | { 121 | m_pCell->setProperty("RowHeight", i); //设置单元格行高 122 | this->m_iRowHeight = i; 123 | } 124 | 125 | void ExcelEngine::setColumnWidth(const int i) 126 | { 127 | m_pCell->setProperty("ColumnWidth", i); //设置单元格列宽 128 | this->m_iColumnWidth = i; 129 | } 130 | 131 | void ExcelEngine::setWarpText(const bool b) 132 | { 133 | m_pCell->setProperty("WrapText", b); 134 | this->m_bWarpText = b; 135 | } 136 | 137 | void ExcelEngine::setBorderColor(const QColor &color) 138 | { 139 | this->m_cBorderColor = color; 140 | } 141 | 142 | void ExcelEngine::setFontFamily(const QString &family) 143 | { 144 | this->m_sFontFamily = family; 145 | } 146 | 147 | void ExcelEngine::setFontSize(const int s) 148 | { 149 | this->m_iFontSize = s; 150 | } 151 | 152 | void ExcelEngine::setFontItalic(const bool b) 153 | { 154 | 155 | this->m_bFontItalic = b; 156 | } 157 | 158 | void ExcelEngine::setFontBold(const bool b) 159 | { 160 | this->m_bFontBold = b; 161 | } 162 | 163 | void ExcelEngine::setBackgroundColor(const QColor &color) 164 | { 165 | this->m_cBackgroundColor = color; 166 | } 167 | 168 | void ExcelEngine::clearCell() 169 | { 170 | m_pCell->dynamicCall("ClearContents()"); 171 | } 172 | 173 | void ExcelEngine::setActiveExcel(const QString &fileName) 174 | { 175 | m_pWorkBooks->dynamicCall("Open(const QString&)", fileName); 176 | m_pWorkBook = m_pExcel->querySubObject("ActiveWorkBook"); 177 | 178 | m_pWorkSheets = m_pWorkBook->querySubObject("Sheets"); 179 | m_pLastSheet = m_pWorkSheets->querySubObject("Item(int)", 1); 180 | m_pWorkSheet = m_pWorkBook->querySubObject("Sheets(int)", 1); 181 | m_pLastSheet->dynamicCall("Move(QVariant)", m_pWorkSheet->asVariant()); 182 | m_pWorkSheet->setProperty("Name","sheet"); 183 | 184 | } 185 | 186 | void ExcelEngine::closeActiveExcel() 187 | { 188 | m_pWorkBook->dynamicCall("Close(Boolean)", false); //关闭文件 189 | m_pExcel->dynamicCall("Quit(void)"); //退出 190 | } 191 | 192 | void ExcelEngine::saveExcel() 193 | { 194 | m_pWorkBook->dynamicCall("Save()"); 195 | } 196 | 197 | void ExcelEngine::saveAsExcel(const QString &fileName) 198 | { 199 | m_pWorkBook->dynamicCall("SaveAs(const QString&)", fileName); //另存为另一个文件 200 | } 201 | 202 | QString ExcelEngine::getExcelTitle() 203 | { 204 | QVariant title = m_pExcel->property("Caption"); 205 | return title.toString(); 206 | } 207 | 208 | QString ExcelEngine::getExcelValue(int x, int y) 209 | { 210 | if(m_vExcleNodeList.count()!=0){ 211 | for(int i = 0; i < m_vExcleNodeList.count();i++){ 212 | ExcelNode *en = m_vExcleNodeList.at(i); 213 | if(en->getX() == x && en->getY() == y) 214 | return en->getValue(); 215 | } 216 | } 217 | return NULL; 218 | } 219 | 220 | void ExcelEngine::applyCellSettings() 221 | { 222 | //设置单元格一些属性 223 | this->setRowHeight(m_iRowHeight); 224 | //m_pCell->setProperty("") 225 | this->setColumnWidth(m_iColumnWidth); 226 | this->setWarpText(m_bWarpText); 227 | m_pCell->setProperty("HorizontalAlignment", -4108); 228 | //左对齐(xlLeft):-4131 居中(xlCenter):-4108 右对齐(xlRight):-4152 229 | m_pCell->setProperty("VerticalAlignment", -4108); 230 | //上对齐(xlTop)-4160 居中(xlCenter):-4108 下对齐(xlBottom):-4107 231 | m_pInterior = m_pCell->querySubObject("Interior"); 232 | m_pInterior->setProperty("Color", m_cBackgroundColor); 233 | m_pBorder = m_pCell->querySubObject("Borders"); 234 | m_pBorder->setProperty("Color", m_cBorderColor); 235 | m_pFont = m_pCell->querySubObject("Font"); 236 | m_pFont->setProperty("Name",m_sFontFamily); 237 | m_pFont->setProperty("Italic", m_bFontItalic); 238 | m_pFont->setProperty("Size", m_iFontSize); 239 | m_pFont->setProperty("Bold", m_bFontBold); 240 | 241 | } 242 | 243 | int ExcelEngine::getColumn_count() const 244 | { 245 | return column_count; 246 | } 247 | 248 | void ExcelEngine::setColumn_count(int value) 249 | { 250 | column_count = value; 251 | } 252 | 253 | int ExcelEngine::getRow_count() const 254 | { 255 | return row_count; 256 | } 257 | 258 | void ExcelEngine::setRow_count(int value) 259 | { 260 | row_count = value; 261 | } 262 | 263 | int ExcelEngine::getColumn_start() const 264 | { 265 | return column_start; 266 | } 267 | 268 | void ExcelEngine::setColumn_start(int value) 269 | { 270 | column_start = value; 271 | } 272 | 273 | int ExcelEngine::getRow_start() const 274 | { 275 | return row_start; 276 | } 277 | 278 | void ExcelEngine::setRow_start(int value) 279 | { 280 | row_start = value; 281 | } 282 | 283 | ExcelEngine::~ExcelEngine() 284 | { 285 | 286 | } 287 | -------------------------------------------------------------------------------- /excelengine.h: -------------------------------------------------------------------------------- 1 | /************************************************************** 2 | * File Name : excelengine.h 3 | * Author : ThreeDog 4 | * Date : Tue Jul 18 13:36:13 2017 5 | * Description : 操作Excel的类的封装。 6 | * 7 | **************************************************************/ 8 | #ifndef _EXCELENGINE_H_ 9 | #define _EXCELENGINE_H_ 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include "excelnode.h" 16 | class ExcelEngine:public QWidget 17 | { 18 | Q_OBJECT 19 | public: 20 | static ExcelEngine * getInstance(QWidget* parent = 0); 21 | static ExcelEngine * s_pExcelEngine; 22 | ~ExcelEngine(); 23 | int getRow_start() const; 24 | void setRow_start(int value); 25 | 26 | int getColumn_start() const; 27 | void setColumn_start(int value); 28 | 29 | int getRow_count() const; 30 | void setRow_count(int value); 31 | 32 | int getColumn_count() const; 33 | void setColumn_count(int value); 34 | 35 | public slots: 36 | bool createExcelFile(const QString &fileName); //创建一个Excel文件 37 | void setExcelVisible(const bool visible); //设置Excel是否可见 38 | void setSheetName(const QString &name); //设置工作表名称 39 | bool writeText(int x, int y ,const QString &text); //向单元格写入文字 40 | bool readText(); //读取单元格中的数据 41 | void setRowHeight(const int i); //设置行高 42 | void setColumnWidth(const int i); //设置列宽 43 | void setWarpText(const bool b); //设置自动换行 44 | void setBorderColor(const QColor &color); //设置边框颜色 45 | void setFontFamily(const QString &family); //设置字体 46 | void setFontSize(const int s); //设置字体大小 47 | void setFontItalic(const bool b); //设置是否倾斜 48 | void setFontBold(const bool b); //设置字体加粗 49 | void setBackgroundColor(const QColor & color); //设置单元格背景颜色 50 | void clearCell(); //清空单元格内容 51 | void setActiveExcel(const QString &fileName); //设置当前操作的Excel文件 52 | void closeActiveExcel(); //关闭当前正在操作的Excel文件 53 | void saveExcel(); //保存 54 | void saveAsExcel(const QString &fileName); //另存为 55 | QString getExcelTitle(); //获取文件的标题 56 | QString getExcelValue(int x,int y); //获取单元格中的数据 57 | private: 58 | QAxObject *m_pExcel; 59 | QAxObject *m_pWorkBooks; 60 | QAxObject *m_pWorkBook; 61 | QAxObject *m_pWorkSheets; 62 | QAxObject *m_pWorkSheet; 63 | QAxObject *m_pLastSheet; 64 | QAxObject *m_pCell; //单元格 65 | QAxObject *m_pBorder; //单元格边框 66 | QAxObject *m_pFont; //单元格文字 67 | QAxObject *m_pInterior; //单元格背景色 68 | bool m_bIsVisible; 69 | 70 | int m_iRowHeight; //行高 71 | int m_iColumnWidth; //列宽 72 | bool m_bWarpText; //自动换行 73 | QColor m_cBorderColor; //边框颜色 74 | QString m_sFontFamily; //字体 75 | int m_iFontSize; //字体大小 76 | bool m_bFontItalic; //字体倾斜 77 | bool m_bFontBold; //字体加粗 78 | QColor m_cBackgroundColor; //背景颜色 79 | QVector m_vExcleNodeList; //存放所有的Excel节点 80 | 81 | explicit ExcelEngine(QWidget *parent = 0); 82 | void applyCellSettings(); //应用单元格设置 83 | 84 | int row_start ; //起始行 85 | int column_start; //起始列 86 | int row_count ; //行总数 87 | int column_count; //列总数 88 | }; 89 | 90 | #endif //EXCELENGINE 91 | -------------------------------------------------------------------------------- /excelnode.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************** 2 | * File Name : excelnode.cpp 3 | * Author : ThreeDog 4 | * Date : Fri Jul 21 15:48:56 2017 5 | * Description : Excel的节点类,每一个对象存放Excel表格的坐标和表格中的数据 6 | * 7 | **************************************************************/ 8 | 9 | #include "excelnode.h" 10 | 11 | ExcelNode::ExcelNode() 12 | { 13 | 14 | } 15 | 16 | ExcelNode::ExcelNode(int _x, int _y, QString _value) 17 | { 18 | this->x = _x; 19 | this->y = _y; 20 | this->value = _value; 21 | } 22 | 23 | void ExcelNode::setX(const int _x) 24 | { 25 | this->x = _x; 26 | } 27 | 28 | void ExcelNode::setY(const int _y) 29 | { 30 | this->y = _y; 31 | } 32 | 33 | void ExcelNode::setValue(const QString &_value) 34 | { 35 | this->value = _value; 36 | } 37 | 38 | int ExcelNode::getX() 39 | { 40 | return this->x; 41 | } 42 | 43 | int ExcelNode::getY() 44 | { 45 | return this->y; 46 | } 47 | 48 | QString ExcelNode::getValue() 49 | { 50 | return this->value; 51 | } 52 | 53 | ExcelNode::~ExcelNode() 54 | { 55 | 56 | } 57 | -------------------------------------------------------------------------------- /excelnode.h: -------------------------------------------------------------------------------- 1 | /************************************************************** 2 | * File Name : excelnode.h 3 | * Author : ThreeDog 4 | * Date : Fri Jul 21 15:48:56 2017 5 | * Description : Excel的节点类,每一个对象存放Excel表格的坐标和表格中的数据 6 | * 7 | **************************************************************/ 8 | #ifndef _EXCELNODE_H_ 9 | #define _EXCELNODE_H_ 10 | #include 11 | 12 | class ExcelNode 13 | { 14 | public: 15 | ExcelNode(); 16 | ExcelNode(int _x, int _y, QString _value); 17 | void setX(const int _x); 18 | void setY(const int _y); 19 | void setValue(const QString &_value); 20 | int getX(); 21 | int getY(); 22 | QString getValue(); 23 | ~ExcelNode(); 24 | private: 25 | int x; 26 | int y; 27 | QString value; 28 | 29 | }; 30 | 31 | #endif //EXCELNODE 32 | --------------------------------------------------------------------------------