└── QT WPS Table ├── CWPSTable.cpp ├── CWPSTable.h └── README.md /QT WPS Table/CWPSTable.cpp: -------------------------------------------------------------------------------- 1 | #include "CWPSTable.h" 2 | #include 3 | # pragma execution_character_set("utf-8") 4 | 5 | CWPSTable::CWPSTable(bool visible,bool alert) {//??? 6 | if(!newExcel()) return; 7 | setAlert(alert); 8 | setVisible(visible); 9 | //MyExcel.setCaption("t");// TITLE 10 | getAddWorkBook(); 11 | getSheets(); 12 | getSheet(1); 13 | //setScreenUpdating(true); 14 | } 15 | 16 | CWPSTable::CWPSTable(const QString& openPath,bool visible,bool alert) { 17 | if(!newExcel()) return; 18 | setAlert(alert); 19 | setVisible(visible); 20 | getOpenWorkBook(openPath); 21 | getSheets(); 22 | getSheet(1); 23 | //setScreenUpdating(true); 24 | } 25 | 26 | CWPSTable::~CWPSTable() { 27 | qDebug() << "release MyWPS"; 28 | } 29 | 30 | void CWPSTable::getAddWorkBook() { 31 | getWorkBooks(); 32 | addWorkBooks();//if create NOT open, got to add a new workBook 33 | activateCurrWorkBook(); 34 | } 35 | 36 | void CWPSTable::getOpenWorkBook(const QString& openPath) { 37 | getWorkBooks(); 38 | openWorkBooks(openPath); 39 | activateCurrWorkBook(); 40 | } 41 | 42 | bool CWPSTable::newExcel() { 43 | MyExcel = new QAxObject("Ket.Application"); 44 | if(MyExcel->isNull()) { 45 | return false; 46 | } 47 | return true; 48 | } 49 | 50 | void CWPSTable::deleteExcel() { 51 | delete MyExcel; 52 | MyExcel = nullptr; 53 | } 54 | 55 | void CWPSTable::save_Quit(const QString& path) { 56 | saveAs(path); 57 | quit(); 58 | deleteExcel(); 59 | } 60 | 61 | void CWPSTable::openWorkBooks(QString inOpenPath) { 62 | // workbooks->querySubObject("Open(QString,QVariant,QVariant)", fileName, 3, true); 63 | // MyWorkBook = MyWorkBooks->querySubObject("Open(QString,QVariant,QVariant)", inOpenPath, 3, true); 64 | // MyWorkBook->querySubObject("Open (const QString&)", inOpenPath); 65 | // 66 | //example:dynamicCall("Open (const QString&)", "C:/test.xls"); 67 | MyWorkBooks->dynamicCall("Open (const QString&)", inOpenPath); 68 | if(MyWorkBooks->isNull()) { 69 | qDebug() << "指定的excel文件不存在!"; 70 | } 71 | } 72 | 73 | void CWPSTable::setVisible(bool inBool) { 74 | MyExcel->setProperty("Visible", inBool); 75 | } 76 | 77 | void CWPSTable::setAlert(bool inBool) { 78 | MyExcel->setProperty("DisplayAlerts", inBool); 79 | } 80 | 81 | void CWPSTable::setCaption(QString inCaption) { 82 | MyExcel->setProperty("Caption", inCaption); 83 | } 84 | 85 | void CWPSTable::getWorkBooks() { 86 | MyWorkBooks = MyExcel->querySubObject("Workbooks"); 87 | if(MyWorkBooks->isNull()) {//??? 88 | qDebug() << "No WorkBooks found~"; 89 | } 90 | } 91 | 92 | int CWPSTable::countSheets() { 93 | return mySheets->property("Count").toInt();//获取表数量 94 | } 95 | 96 | void CWPSTable::getSheets() { 97 | //mySheets = MyWorkBook->querySubObject("Sheets"); workable 98 | mySheets = MyWorkBook->querySubObject("Worksheets"); 99 | } 100 | 101 | void CWPSTable::addSheet(bool addAtLast) {//the sheet is got after adding 102 | if(!addAtLast) { 103 | mySheets->dynamicCall("Add"); 104 | } else { 105 | int SheetsCnt = countSheets();//获取表数量 106 | QAxObject* pLastSheet = mySheets->querySubObject("Item(int)", SheetsCnt);//这步可以的 107 | mySheets->querySubObject("Add(QVariant)", pLastSheet->asVariant());//这步之后,新加表 处于倒数第二的位置; 108 | mySheet = mySheets->querySubObject("Item(int)", SheetsCnt); 109 | pLastSheet->dynamicCall("Move(QVariant)", mySheet->asVariant()); 110 | } 111 | } 112 | /** 113 | * \brief ActiveWindow.SelectedSheets.Copy After:=Sheets("Sheet1") 114 | * \param addAtLast 115 | */ 116 | void CWPSTable::copySheet() { 117 | // QAxObject* lastSheet = mySheets->querySubObject("Item(int)", countSheets());//workable 118 | // mySheet->dynamicCall("Copy(const QVariant&)", lastSheet->asVariant());//new sheet at last 2 pos 119 | mySheet->dynamicCall("Copy(const QVariant&)",mySheet->asVariant());//add sheet before itself,but how to add after? 120 | } 121 | void CWPSTable::CopyWorkBook() {//copyALlSheets 122 | mySheets->dynamicCall("Copy()"); 123 | } 124 | 125 | 126 | void CWPSTable::addWorkBooks() { 127 | MyWorkBooks->dynamicCall("Add"); 128 | } 129 | 130 | void CWPSTable::activateCurrWorkBook() { 131 | MyWorkBook = MyExcel->querySubObject("ActiveWorkBook"); 132 | } 133 | 134 | void CWPSTable::getSheet(int inIndex) { 135 | //mySheet = MyWorkBook->querySubObject("Worksheets(int)", inIndex);//workable 136 | // mySheet = MyWorkBook->querySubObject("Worksheets(const QVariant&)", "酒店");//workable 137 | mySheet = MyWorkBook->querySubObject("Sheets(int)", inIndex); 138 | if(mySheet->isNull()) { 139 | qDebug() << "No Sheet found~"; 140 | } 141 | mySheet->dynamicCall("activate()"); 142 | } 143 | void CWPSTable::setSheetName(const QString& name) { 144 | mySheet->setProperty("Name",name); 145 | } 146 | QString CWPSTable::getSheetName() { 147 | return mySheet->property("Name").toString(); 148 | } 149 | 150 | void CWPSTable::getRange(QString inRange) { 151 | range = mySheet->querySubObject("Range(const QVariant&)", QVariant(inRange)); 152 | } 153 | 154 | void CWPSTable::getRange(int inRow1,int inColumn1) { 155 | QAxObject* myCell1 = mySheet->querySubObject("Cells(int,int)", inRow1, inColumn1); 156 | range = mySheet->querySubObject 157 | ("Range(const QVariant&,const QVariant&)", myCell1->asVariant(), myCell1->asVariant()); 158 | range->dynamicCall("Select()"); 159 | } 160 | 161 | QAxObject* CWPSTable::getRange(int inRow1,int inColumn1,int inRow2,int inColumn2) { 162 | QAxObject* myCell1 = mySheet->querySubObject("Cells(int,int)", inRow1, inColumn1); 163 | QAxObject* myCell2 = mySheet->querySubObject("Cells(int,int)", inRow2, inColumn2); 164 | return range = mySheet->querySubObject 165 | ("Range(const QVariant&,const QVariant&)", myCell1->asVariant(), myCell2->asVariant()); 166 | } 167 | 168 | QString CWPSTable::getValue(int inRow1,int inColumn1) { 169 | getRange(inRow1, inColumn1); 170 | return range->property("Value2").toString(); 171 | } 172 | 173 | //void CWPSTable::getRange(int inRow1, int inColumn1, int inRow2, int inColumn2) 174 | //{ 175 | //} 176 | // 177 | //void CWPSTable::getCell(int inRow, int inColumn) 178 | //{ 179 | // MyCell = mySheet->querySubObject("Cells(int,int)", inRow, inColumn); 180 | //} 181 | 182 | QString CWPSTable::getValue() { 183 | return range->property("Value2").toString(); 184 | } 185 | 186 | void CWPSTable::setValue(const QVariant& inValue) { 187 | // inRange->dynamicCall("SetValue(const QVariant&)", QObject::tr("卡卡尼莫")); 188 | // getRange(inRange); 189 | range->dynamicCall("SetValue(const QVariant&)", inValue); 190 | } 191 | 192 | void CWPSTable::setValue(int row,int col,const QVariant& value) { 193 | getRange(row, col); 194 | setValue(value); 195 | } 196 | 197 | void CWPSTable::setComment(int row,int col,const QString& text) { 198 | getRange(row, col); 199 | range->dynamicCall("AddComment(const QVariant&)", text); 200 | } 201 | 202 | void CWPSTable::setRangeFormat(int row1,int col1,int row2,int col2,QString myFormat) { 203 | //"yyy年mm月dd";"@" '为文本 "G/通用格式" '为通用 204 | getRange(row1, col1, row2, col2); 205 | range->setProperty("NumberFormat", myFormat); 206 | } 207 | 208 | void CWPSTable::autoFit() { 209 | //selectRange.EntireColumn.AutoFit(); //全部列自适应宽度 210 | //selectRange.EntireRow.AutoFit(); //全部行自适应高度 211 | autoFitColumn(); 212 | autoFitRow(); 213 | } 214 | 215 | void CWPSTable::autoFitColumn() { 216 | mySheet->querySubObject("columns")->dynamicCall("AutoFit");//workable 217 | } 218 | 219 | void CWPSTable::autoFitRow() { 220 | mySheet->querySubObject("rows")->dynamicCall("AutoFit");//workable 221 | } 222 | 223 | //QString CWPSTable::getCellValue(int inRow, int inColumn) 224 | //{ 225 | // getCell(inRow, inColumn); 226 | // return MyCell->property("Value2").toString(); 227 | //} 228 | // 229 | //void CWPSTable::setCellValue(int inRow, int inColumn, QString inValue) 230 | //{ 231 | // getCell(inRow, inColumn); 232 | // MyCell->querySubObject("SetValue(const QVariant&)", inValue); 233 | //} 234 | void CWPSTable::setBackColor(int inRow1,int inColumn1,int inRow2,int inColumn2,int inFontColor) { 235 | getRange(inRow1, inColumn1, inRow2, inColumn2); 236 | range->querySubObject("interior")->setProperty("ColorIndex", inFontColor); 237 | } 238 | 239 | void CWPSTable::setFontColor(int inRow,int inColumn,int inFontColor) { 240 | getRange(inRow, inColumn); 241 | range->querySubObject("Font")->setProperty("ColorIndex", inFontColor); 242 | // MyRange->querySubObject("Font")->setProperty("Color", QColor(255, 0, 0)); 243 | } 244 | 245 | void CWPSTable::setFontColor(int inRow1,int inColumn1,int inRow2,int inColumn2,int inFontColor) { 246 | getRange(inRow1, inColumn1, inRow2, inColumn2); 247 | range->querySubObject("Font")->setProperty("ColorIndex", inFontColor); 248 | } 249 | 250 | void CWPSTable::copyPaste(int inRow1,int inColumn1,int inRow2,int inColumn2, 251 | int inPasteRow1,int inPasteColumn1,int inPasteRow2,int inPasteColumn2) { 252 | QAxObject* copyRange = getRange(inRow1, inColumn1, inRow2, inColumn2); 253 | getRange(inPasteRow1, inPasteColumn1, inPasteRow2, inPasteColumn2); 254 | copyRange->querySubObject("Copy(const QVariant&)", range->asVariant()); 255 | } 256 | 257 | void CWPSTable::copyPasteFromSheet(int copySheetPos,int pasteSheetPos, 258 | int inRow1,int inColumn1,int inRow2,int inColumn2, 259 | int inPasteRow1,int inPasteColumn1) { 260 | getSheet(copySheetPos); 261 | QAxObject* copyRange = getRange(inRow1, inColumn1, inRow2, inColumn2); 262 | getSheet(pasteSheetPos); 263 | getRange(inPasteRow1, inPasteColumn1, inPasteRow1, inPasteColumn1); 264 | copyRange->querySubObject("Copy(const QVariant&)", range->asVariant()); 265 | } 266 | 267 | 268 | 269 | /** 270 | * \brief 271 | ? Is there a faster way to do it? 272 | */ 273 | void CWPSTable::copyInsertRowTo(int inRow1,int inColumn1,int inRow2,int inColumn2, 274 | int inPasteRow,int inPasteColumn,bool inIsDown,int inPasteRowNum) { 275 | // QAxObject* myCopyRange = getRange(inRow1, inColumn1, inRow2, inColumn2); 276 | // myCopyRange->querySubObject("Select()"); 277 | // myCopyRange->querySubObject("Copy(const QVariant&)", NULL); 278 | getRange(inRow1, inColumn1, inRow2, inColumn2); 279 | // MyRange->querySubObject("Select()"); 280 | range->querySubObject("Copy(const QVariant&)", NULL); 281 | insertRow(inPasteRow, inPasteColumn, inIsDown, inPasteRowNum); 282 | } 283 | 284 | void CWPSTable::insertRow(int inRow,int inColumn,bool inIsDown,int inRowNum) { 285 | if(inIsDown) inRow++; 286 | getRange(inRow, inColumn, inRow + inRowNum - 1, inColumn); 287 | range->querySubObject("EntireRow")->querySubObject("Insert()"); 288 | // getRange(inRow, inColumn); 289 | // 290 | // for (size_t i = 0; i < inRowNum; ++i) 291 | // { 292 | // MyRange->querySubObject("EntireRow")->querySubObject("Insert()"); 293 | // } 294 | } 295 | 296 | void CWPSTable::setScreenUpdating(bool inBool) { 297 | MyExcel->dynamicCall("SetScreenUpdating(bool)", inBool); 298 | } 299 | 300 | void CWPSTable::saveAs(QString inSavePath) { 301 | MyWorkBook->querySubObject("SaveAs(const QString&)", inSavePath); 302 | } 303 | 304 | void CWPSTable::quit() { 305 | MyWorkBook->dynamicCall("Close (Boolean)", false); 306 | MyExcel->dynamicCall("Quit()"); 307 | } 308 | 309 | void CWPSTable::setColumnWidth(int col,float width) { 310 | mySheet->querySubObject("columns(int)", col)->setProperty("ColumnWidth", width);//workable 311 | } 312 | 313 | void CWPSTable::setColumnHidden(int col,bool isHidden) { 314 | mySheet->querySubObject("columns(int)", col)->setProperty("Hidden", isHidden);//workable 315 | } 316 | 317 | void CWPSTable::mergeRange(int row1,int col1,int row2,int col2) { 318 | this->getRange(row1, col1, row2, col2); 319 | range->querySubObject("Merge"); 320 | } 321 | 322 | void CWPSTable::setRangeFontSize(int row1,int col1,int row2,int col2,int myFontSize) { 323 | this->getRange(row1, col1, row2, col2); 324 | range->querySubObject("Font")->setProperty("Size", myFontSize); 325 | } 326 | 327 | void CWPSTable::setRangeAlign(int row1,int col1,int row2,int col2,int alignType) { 328 | this->getRange(row1, col1, row2, col2);//.HorizontalAlignment = xlCenter 329 | //.VerticalAlignment = xlCenter=-4108 330 | range->setProperty("VerticalAlignment", alignType); 331 | range->setProperty("HorizontalAlignment", alignType); 332 | } 333 | 334 | void CWPSTable::setRangeFontStyle(int row1,int col1,int row2,int col2,int myStyle,bool styleFlag) { 335 | this->getRange(row1, col1, row2, col2);//.HorizontalAlignment = xlCenter 336 | switch(myStyle) { 337 | case 0: 338 | range->querySubObject("Font")->setProperty("Bold", styleFlag); 339 | break; 340 | case 1: 341 | range->querySubObject("Font")->setProperty("Italic", styleFlag); 342 | break; 343 | case 2: 344 | range->querySubObject("Font")->setProperty("Underline", styleFlag); 345 | break; 346 | default: 347 | return; 348 | } 349 | } 350 | 351 | 352 | void CWPSTable::setRangeBorderStyle_Width(int row1, int col1, int row2, int col2,int borderStyle,int borderWidth ) { 353 | this->getRange(row1, col1, row2, col2); 354 | range->querySubObject("Borders")->setProperty("LineStyle", borderStyle); 355 | range->querySubObject("Borders")->setProperty("Weight", borderWidth); 356 | } 357 | 358 | void CWPSTable::multiSelectCopy() {//??? NOT WORKABLE 359 | //VBA: 360 | //Range("C4,E4,G4").Select 361 | //Range("G4").Activate 362 | //Selection.Copy 363 | //Range("C26").Select 364 | //ActiveSheet.Paste 365 | //------ 366 | //Range("F1").Select 367 | //Range(Selection, Selection.End(xlDown)).Select 368 | //Selection.Copy 369 | //Range("I29").Select 370 | //ActiveSheet.Paste 371 | //============== 372 | // QAxObject* pasteRange = getRange(2, 14, 2, 14); 373 | // getRange(1, 1, 1, 1); 374 | // range->dynamicCall("Select"); 375 | // range->dynamicCall("Copy"); 376 | // getRange(1, 2, 1, 2); 377 | // range->dynamicCall("Select"); 378 | // range->dynamicCall("Copy"); 379 | //// range->dynamicCall("Select"); 380 | //// range->dynamicCall("Paste"); 381 | // range->querySubObject("Copy(const QVariant&)", pasteRange->asVariant()); 382 | 383 | //getRange(2, 3); 384 | MyExcel->querySubObject("Range(QVariant&,QVariant&)","C4","D4")->dynamicCall("Select()"); 385 | QAxObject* selObj = MyExcel->querySubObject("Selection");//ok 386 | QVariant selObj2 = selObj->dynamicCall("End(int)", -4121); 387 | MyExcel->querySubObject("Range(QVariant&,QVariant&)", selObj->asVariant(), 388 | selObj2)->dynamicCall("Select()"); 389 | 390 | MyExcel->querySubObject("Selection")->dynamicCall("Copy()"); 391 | getRange(30, 10); 392 | range->dynamicCall("Select()"); 393 | MyExcel->querySubObject("ActiveSheet")->dynamicCall("Paste()"); 394 | } -------------------------------------------------------------------------------- /QT WPS Table/CWPSTable.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #define RED 3 4 | #define YELLOW 6 5 | #define GREEN 43 6 | #define ORANGE 44 7 | #define CENTER_ALIGN -4108 8 | #define LEFT_ALIGN -4131 9 | //-- 10 | #define BOLD 0 11 | #define ITALIC 1 12 | #define UNDERLINE 2 13 | //- 14 | #define LINE_STYLE_CONTINUE 1 15 | //XlBorderWeight 16 | #define xlMedium -4138 17 | #define xlThin 2 18 | #define xlThick 4 19 | #define xlHairline 1 20 | 21 | #include 22 | #include 23 | class CWPSTable : public QObject { 24 | Q_OBJECT 25 | 26 | public: 27 | CWPSTable() = default; 28 | //CWPSTable(QObject * parent = Q_NULLPTR); 29 | void getAddWorkBook(); 30 | CWPSTable(bool visible, bool alert);//new a table 31 | void getOpenWorkBook(const QString& openPath); 32 | CWPSTable(const QString& openPath, bool visible, bool alert);//open a existed table 33 | ~CWPSTable(); 34 | bool newExcel(); 35 | void deleteExcel(); 36 | void save_Quit(const QString& path); 37 | void openWorkBooks(QString inOpenPath); 38 | void getWorkBooks(); 39 | int countSheets(); 40 | void getSheets(); 41 | void addSheet(bool addAtLast = true); 42 | void copySheet(); 43 | void CopyWorkBook(); 44 | void addWorkBooks(); 45 | void setVisible(bool inBool); 46 | void setAlert(bool inBool); 47 | void setCaption(QString inCaption); 48 | 49 | void activateCurrWorkBook(); 50 | void getSheet(int inIndex); 51 | void setSheetName(const QString& name); 52 | QString getSheetName(); 53 | void getRange(QString inRange);//EXAMPLE:"A1:A1","A1","A1:B3","A:A","3:3"... 54 | void getRange(int inRow1, int inColumn1); 55 | QAxObject* getRange(int inRow1, int inColumn1, int inRow2, int inColumn2); 56 | QString getValue(int inRow1, int inColumn1); 57 | QString getValue(); 58 | void setValue(const QVariant& inValue); 59 | void setValue(int row, int col, const QVariant& value); 60 | void setComment(int row, int col,const QString& text);//enter="\n" 61 | void setRangeFormat(int row1, int col1, int row2, int col2, QString myFormat);//CELL RANGE FORMAT 62 | void autoFit(); 63 | void autoFitColumn(); 64 | void autoFitRow(); 65 | void setBackColor(int inRow1, int inColumn1, int inRow2, int inColumn2, int inFontColor); 66 | void setFontColor(int inRow, int inColumn, int inFontColor);//EXAMPLE: 3 = RED 67 | void setFontColor(int inRow1, int inColumn1, int inRow2, int inColumn2, int inFontColor);//EXAMPLE: 3 = RED 68 | void copyPaste(int inRow1, int inColumn1, int inRow2, int inColumn2, int inPasteRow1, int inPasteColumn1, int inPasteRow2, int inPasteColumn2); 69 | void copyPasteFromSheet(int copySheetPos,int inRow1,int inColumn1,int inRow2,int inColumn2,int pasteSheetPos,int inPasteRow1,int inPasteColumn1); 70 | void copyInsertRowTo(int inRow1, int inColumn1, int inRow2, int inColumn2, int inPasteRow, int inPasteColumn, bool inIsDown, int inPasteRowNum); 71 | void insertRow(int inRow, int inColumn, bool inIsDown, int inRowNum); 72 | // void getRange(int inRow1, int inColumn1, int inRow2, int inColumn2); 73 | // void getCell(int inRow, int inColumn);// 74 | // QString getValue(QString inRange); 75 | 76 | // void setValue(QString inRange, QString inValue); 77 | // QString getCellValue(int inRow, int inColumn); 78 | // void setCellValue(int inRow, int inColumn, QString inValue); 79 | void setScreenUpdating(bool inBool); 80 | void saveAs(QString inSavePath);//"C:\\Book1.xlsx" 81 | void quit(); 82 | void setColumnWidth(int col, float width); 83 | void setColumnHidden(int col, bool isHidden = true); 84 | void mergeRange(int row1,int col1,int row2,int col2); 85 | void setRangeFontSize(int row1,int col1,int row2,int col2,int myFontSize); 86 | void setRangeAlign(int row1,int col1,int row2,int col2,int alignType); 87 | void setRangeFontStyle(int row1,int col1,int row2,int col2,int myStyle,bool styleFlag=true); 88 | void setRangeBorderStyle_Width(int row1,int col1,int row2,int col2,int borderStyle,int borderWidth= xlThin); 89 | void multiSelectCopy(); 90 | 91 | QAxObject* MyExcel = nullptr; 92 | QAxObject* MyWorkBooks = nullptr; 93 | QAxObject* MyWorkBook = nullptr; 94 | QAxObject* mySheets = nullptr; 95 | QAxObject* mySheet = nullptr; 96 | QAxObject* range = nullptr; 97 | QAxObject* MyCell = nullptr; 98 | }; 99 | -------------------------------------------------------------------------------- /QT WPS Table/README.md: -------------------------------------------------------------------------------- 1 | # 关于WPS Table 2 | 3 | #### 内容: 4 | 5 | ​ 这文件夹下的C++文件是通过QT的QAxobject来用于操作WPS的et表格(类似于Office的Excel)的。而实践发现几乎所有写法一样适用于Office的EXCEL操作。 6 | 7 | ​ 该文件中的range都是通过int数字来实现操作的;我没有用cell,因为觉得range已经够用了。当然也可以通过诸如“A3”这类字符来操作range,但总觉得这种读取方式不太便与循环操作,所以没有实现。 8 | 9 | #### 实现和使用方法: 10 | 11 | ​ 我是在VS2015下结合QT5.8框架来实现的。使用的话,QT框架一定要安装,其次安装WPS个人版即可。 12 | 13 | #### 分享: 14 | 15 | ​ 本来写这个库是为了便与自己办公操作Excel表格。需要的自取,也欢迎各路高手添加完善本库。 16 | 17 | #### 待解决的问题: 18 | 19 | 1.如何进行多重非相邻单元格复制? 20 | 21 | 2.如何实现ctrl+shift+down来选取非空连续range呢? 22 | 23 | --------------------------------------------------------------------------------