自己從網上下載了一個例子:https://files-cdn.cnblogs.com/files/warmlight/qword.rar
里面有一個完整例子(導出word的具體操作),一個單獨的類(根據模板導出word)。
如果wordhandler.h和wordhandler.cpp有問題,一般是文件編碼類型的問題。使用wordhandler類時,一定要聲明wordhandler為成員變量或全局變量,因為用到了線程,如果是局部變量,會報錯。使用時,調用StartExport函數傳入參數,再調用start函數啟動線程即可。當然,需要根據自己的情況更改要傳入什么。
聲明:使用例子時,如果產生不可預料的情況(刪庫跑路,服務器崩潰),本人概不負責。另外,如果調用不成功,也不關我的事。拜拜~
添加一些微操代碼。
具體查看微軟api(2020.05.21鏈接還可用):https://docs.microsoft.com/zh-cn/dotnet/api/microsoft.office.interop.word?view=word-pia
因為最近項中需要生成報表,所以網上查了資料,因為VB看不懂所以總結的也不多,在實現中沒有使用標簽只是以光標的位置來插入,包括:創建文件,排版方式,添加文字,添加圖片,添加表格,表格添加文字(圖片),光標移動到尾部,光標跳轉(類似tab)等。
1 pro文件中添加 QT += axcontainer 2 3 QAxObject *m_pWord; //指向整個Word應用程序 4 QAxObject *m_pWorkDocuments; //指向文檔集,Word有很多文檔 5 QAxObject *m_pWorkDocument; //指向m_sFile對應的文檔,就是要操作的文檔 6 7 /* 8 創建word 9 */ 10 11 void MainWindow::open() 12 { 13 m_pWord = new QAxObject(); 14 bool flag = m_pWord->setControl( "Word.Application" ); 15 if(!flag) 16 { 17 return; 18 } 19 m_pWord->setProperty("Visible", true); 20 21 QAxObject *document = m_pWord->querySubObject("Documents"); 22 if(!document) 23 { 24 return ; 25 } 26 27 //獲取當前激活的文檔 28 m_pWorkDocument = m_pWord->querySubObject("ActiveDocument"); 29 } 30 31 /* 32 * 排版方式 33 * 縱行、橫行 34 */ 35 36 void MainWindow::setype(bool type) 37 { 38 QAxObject* selection = m_pWord->querySubObject("Selection"); 39 if(!selection) 40 { 41 return; 42 } 43 if(type) 44 { 45 selection->querySubObject("PageSetup")->setProperty("Orientation","wdOrientLandscape"); 46 }else{ 47 selection->querySubObject("PageSetup")->setProperty("Orientation","wdOrientPortrait"); 48 } 49 } 50 51 /* 52 * 獲取頁寬 53 */ 54 55 int MainWindow::pageWidth() 56 { 57 int width; 58 QAxObject* selection = m_pWord->querySubObject("Selection"); 59 if(!selection) 60 { 61 return; 62 } 63 width = selection->querySubObject("PageSetup")->property("PageWidth").toInt();; 64 return width; 65 } 66 67 /* 68 *設置字號 69 */ 70 71 void MainWindow::setFontSize(int size) 72 { 73 QAxObject* selection = m_pWord->querySubObject("Selection"); 74 if(!selection) 75 { 76 return; 77 } 78 selection->querySubObject("Font")->setProperty("Size",size); 79 } 80 81 /* 82 設置對齊方式 83 */ 84 85 void MainWindow::setAlignment(int index) 86 { 87 QAxObject *selection = m_pWord->querySubObject("Selection"); 88 if(!selection) 89 { 90 return; 91 } 92 if(index == 0) 93 { 94 selection->querySubObject("ParagraphFormat")->setProperty("Alignment","wdAlignParagraphCenter"); 95 } 96 if(index == 1) 97 { 98 selection->querySubObject("ParagraphFormat")->setProperty("Alignment","wdAlignParagraphJustify"); 99 } 100 if(index == 2) 101 { 102 selection->querySubObject("ParagraphFormat")->setProperty("Alignment","wdAlignParagraphRight"); 103 } 104 if(index == 3) 105 { 106 selection->querySubObject("ParagraphFormat")->setProperty("Alignment","wdAlignParagraphLeft"); 107 } 108 } 109 110 111 /* 112 插入文字 113 */ 114 115 void MainWindow::typeText(QString text) 116 { 117 QAxObject* selection = m_pWord->querySubObject("Selection"); 118 if(!selection) 119 { 120 return; 121 } 122 selection->dynamicCall("TypeText(const QString&)",text); 123 } 124 /* 125 插入圖片 126 */ 127 128 void MainWindow::AddPicture(QString file) 129 { 130 QAxObject* selection = m_pWord->querySubObject("Selection"); 131 if(!selection) 132 { 133 return; 134 } 135 QString filename = file; 136 filename.replace("/","\\"); 137 QAxObject *Inlineshapes = selection->querySubObject("InlineShapes"); 138 Inlineshapes->dynamicCall("AddPicture(const QString&)",filename); 139 delete Inlineshapes; 140 } 141 142 /* 143 插入回車 144 */ 145 146 void MainWindow::insertEnter() 147 { 148 QAxObject* selection = m_pWord->querySubObject("Selection"); 149 if(!selection) 150 { 151 return; 152 } 153 selection->dynamicCall("TypeParagraph(void)"); 154 } 155 156 /* 157 * 光標移到末尾,跳出單元格 158 */ 159 void MainWindow::moveForEnd() 160 { 161 QAxObject* selection = m_pWord->querySubObject("Selection"); 162 QVariantList params; 163 params.append(6); 164 params.append(0); 165 selection->dynamicCall("EndOf(QVariant&, QVariant&)", params).toInt(); 166 } 167 168 /* 169 創建表格 170 QStringList headList 添加表頭 171 */ 172 173 QAxObject* MainWindow::createTable(int row, int column,QStringList headList) 174 { 175 QAxObject* selection = m_pWord->querySubObject("Selection"); 176 if(!selection) 177 { 178 return false; 179 } 180 selection->dynamicCall("InsertAfter(QString&)", "\r\n"); 181 182 QAxObject *range = selection->querySubObject("Range"); 183 QAxObject *tables = m_pWorkDocument->querySubObject("Tables"); 184 QAxObject *table = tables->querySubObject("Add(QVariant,int,int)",range->asVariant(),row,column); 185 186 table->setProperty("Style","網格型"); 187 //表格自動拉伸列 0固定 1根據內容調整 2 根據窗口調整 188 table->dynamicCall("AutoFitBehavior(WdAutoFitBehavior)", 2); 189 190 //設置表頭 191 for(int i=0;i<headList.size();i++) 192 { 193 table->querySubObject("Cell(int,int)",1,i+1)->querySubObject("Range")->dynamicCall("SetText(QString)", headList.at(i)); 194 //加粗 195 table->querySubObject("Cell(int,int)",1,i+1)->querySubObject("Range")->dynamicCall("SetBold(int)", true); 196 } 197 return table; 198 } 199 200 /* 201 填充表格 202 */ 203 204 void MainWindow::setCellText(QAxObject *table, int row, int column, QString text) 205 { 206 QAxObject* range = table->querySubObject("Cell(int, int)",row+1,column+1)->querySubObject("Range"); 207 if( range) 208 { 209 return; 210 } 211 212 range->dynamicCall("SetText(QString)", text); 213 } 214 215 /* 216 表格插入圖片 217 */ 218 void MainWindow::setAddPicture(QAxObject *table, int row, int column, QString picPath) 219 { 220 QAxObject* range = table->querySubObject("Cell(int, int)",row+1,column+1)->querySubObject("Range"); 221 if(!range) 222 { 223 return; 224 } 225 range->querySubObject("InlineShapes")->dynamicCall("AddPicture(const QString&)",picPath); 226 } 227 228 /* 229 光標跳轉,類似表格中的tab 230 */ 231 232 void MainWindow::moveRight() 233 { 234 QAxObject* selection = m_pWord->querySubObject("Selection"); 235 if(!selection) 236 { 237 return; 238 } 239 selection->dynamicCall("MoveRight(int)",1); 240 } 241 242 /*特殊樣例*/ 243 244 245 246 void MainWindow::Example() 247 { 248 QStringList header; 249 250 newTable(1,2,header);//創建表格 251 252 typeText(tr("文字1"));//添加文字 253 insertEnter();//換行 254 insertText(""); 255 AddPicture("圖像1");//插入圖片 256 257 moveRight();//將光標插入到下個單元格中 258 259 typeText(tr("文字2")); 260 insertEnter(); 261 insertText(""); 262 AddPicture("圖像2"); 263 264 moveForEnd();//光標跳出表格 265 266 /*然后,可以做其他操作了*/ 267 }