WzExcel.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. #include "WzExcel.h"
  2. #include "MessageException.h"
  3. #include "charcode.h"
  4. WzExcel::WzExcel()
  5. {
  6. qDebug()<<"WzExcel: WzExcel()";
  7. OleInitialize(0); //当前线程初始化COM库并设置并发模式STA(single-thread apartment——单线程单元)
  8. excel = NULL;
  9. workBooks = NULL;
  10. workBook = NULL;
  11. workSheets = NULL;
  12. workSheet = NULL;
  13. data = NULL;
  14. isOpened = false;
  15. fileName = "";
  16. startRow = 0;
  17. startColumn = 0;
  18. }
  19. WzExcel::WzExcel(const QString &fileName)
  20. {
  21. qDebug()<<"WzExcel: WzExcel(const QString &filename)";
  22. OleInitialize(0); //当前线程初始化COM库并设置并发模式STA(single-thread apartment——单线程单元)
  23. excel = NULL;
  24. workBooks = NULL;
  25. workBook = NULL;
  26. workSheets = NULL;
  27. workSheet = NULL;
  28. data = NULL;
  29. isOpened = false;
  30. startRow = 0;
  31. startColumn = 0;
  32. this->fileName = fileName;
  33. }
  34. WzExcel::~WzExcel()
  35. {
  36. qDebug()<<"WzExcel: ~WzExcel()";
  37. release();
  38. OleUninitialize(); //关闭当前线程的COM库并释放相关资源
  39. }
  40. void WzExcel::setFileName(const QString fileName)
  41. {
  42. qDebug()<<"WzExcel: setFileName(const QString fileName)";
  43. this->fileName = fileName;
  44. }
  45. void WzExcel::open(bool visible,bool displayAlerts)
  46. {
  47. qDebug()<<"WzExcel: open(bool visible,bool displayAlerts)";
  48. if(fileName.isEmpty())
  49. {
  50. throw MessageException("打开失败,文件名为空,请设置文件名");
  51. }
  52. if (fileName.endsWith(".xlsx"))
  53. excel = new QAxObject("Ket.Application");
  54. else
  55. excel = new QAxObject("Excel.Application"); //初始化excel对象
  56. if(excel == NULL)
  57. {
  58. throw MessageException("请安装office");
  59. }
  60. excel->dynamicCall("SetVisible(bool)", visible); //false不显示窗体
  61. excel->setProperty("DisplayAlerts", displayAlerts); //不显示警告。
  62. workBooks = excel->querySubObject("WorkBooks"); //获取全部工作簿对象
  63. if (!workBooks)
  64. throw MessageException("Excel打开失败");
  65. QFile file(fileName);
  66. if (file.exists())
  67. {
  68. //导入文件到全部工作簿对象中,并将其设置为当前工作簿
  69. workBook = workBooks->querySubObject("Open(const QString &)", fileName);
  70. if (!workBook)
  71. throw MessageException("Excel打开失败");
  72. }
  73. else
  74. {
  75. //文件不存在则创建
  76. workBooks->dynamicCall("Add");
  77. workBook = excel->querySubObject("ActiveWorkBook");
  78. }
  79. workSheets = workBook->querySubObject("Sheets"); //获得所有工作表对象
  80. workSheet = workSheets->querySubObject("Item(int)", 1); //获得第一张工作表对象
  81. QAxObject* userRange = this->getCurrentUserRange();
  82. startRow = userRange->property("Row").toInt();
  83. startColumn = userRange->property("Column").toInt();
  84. isOpened = true;
  85. }
  86. void WzExcel::close()
  87. {
  88. qDebug()<<"WzExcel: close()";
  89. release();
  90. }
  91. bool WzExcel::setVisible(bool visible)
  92. {
  93. qDebug()<<"WzExcel: setVisible(bool visible)";
  94. if(!isOpened)
  95. {
  96. qDebug()<<"设置visible失败,文件没有打开,请先调用open函数";
  97. return false;
  98. }
  99. else
  100. {
  101. excel->dynamicCall("SetVisible(bool)", visible);
  102. return true;
  103. }
  104. }
  105. bool WzExcel::setCurrentWorkSheet(const QString &sheetName)
  106. {
  107. qDebug()<<"WzExcel: setCurrentWorkSheet(const QString &sheetName)";
  108. if(!isOpened)
  109. {
  110. qDebug()<<"设置当前工作表失败,文件没有打开,请先调用open函数";
  111. return false;
  112. }
  113. if(sheetName.isNull())
  114. {
  115. workSheet = workSheets->querySubObject("Item(int)",1); //获得第一张工作表对象
  116. }
  117. else
  118. {
  119. int count = workSheets->property("Count").toInt();
  120. for(int i=1;i<=count;i++)
  121. {
  122. if(workSheets->querySubObject("Item(int)",i)->property("Name").toString() == sheetName)
  123. {
  124. workSheet = workSheets->querySubObject("Item(int)",i); //找到则设为指定名字的表对象
  125. break;
  126. }
  127. if(i==count)
  128. {
  129. workSheet = workSheets->querySubObject("Item(int)",1); //如果找不到则设为第一张表
  130. }
  131. }
  132. }
  133. qDebug()<<workSheet->property("Name").toString();
  134. QAxObject* usedrange = workSheet->querySubObject("UsedRange");
  135. startRow = usedrange->property("Row").toInt();
  136. startColumn = usedrange->property("Column").toInt();
  137. return true;
  138. }
  139. bool WzExcel::createWorkSheet(const QString &sheetName)
  140. {
  141. qDebug()<<"WzExcel: createWorkSheet(const QString &sheetName)";
  142. if(!isOpened)
  143. {
  144. qDebug()<<"创建工作表失败,文件没有打开,请先调用open函数";
  145. return false;
  146. }
  147. int count = workSheets->property("Count").toInt();
  148. for(int i=1;i<=count;i++)
  149. {
  150. if(workSheets->querySubObject("Item(int)",i)->property("Name").toString() == sheetName)
  151. {
  152. qDebug()<<"该表已存在!";
  153. return false;
  154. }
  155. }
  156. QAxObject *lastSheet = workSheets->querySubObject("Item(int)", count);
  157. workSheets->querySubObject("Add(QVariant)", lastSheet->asVariant());
  158. QAxObject *newSheet = workSheets->querySubObject("Item(int)", count);
  159. lastSheet->dynamicCall("Move(QVariant)", newSheet->asVariant());
  160. newSheet->setProperty("Name", sheetName);
  161. return true;
  162. }
  163. bool WzExcel::deleteWorkSheet(const QString &sheetName)
  164. {
  165. qDebug()<<"WzExcel: deleteWorkSheet(const QString &sheetName)";
  166. if(!isOpened)
  167. {
  168. qDebug()<<"删除工作表失败,文件没有打开,请先调用open函数";
  169. return false;
  170. }
  171. int count = workSheets->property("Count").toInt();
  172. for(int i=1;i<=count;i++)
  173. {
  174. if(workSheets->querySubObject("Item(int)",i)->property("Name").toString() == sheetName)
  175. {
  176. workSheets->querySubObject("Item(int)",i)->dynamicCall("delete");
  177. break;
  178. }
  179. }
  180. return true;
  181. }
  182. QVariant WzExcel::getValue(const int &row, const int &column)
  183. {
  184. qDebug()<<"WzExcel: getValue(const int &row, const int &column)";
  185. if(!isOpened)
  186. {
  187. qDebug()<<"获得指定位置的单元格内容失败,文件没有打开,请先调用open函数";
  188. return "";
  189. }
  190. if(workSheet == NULL)
  191. {
  192. qDebug()<<"获得指定位置的单元格内容失败,workSheet对象为空,请先调用setCurrentWorkSheet函数";
  193. return "";
  194. }
  195. data = workSheet->querySubObject("Cells(int,int)", startRow + row, startColumn + column);
  196. return data->dynamicCall("Value()");
  197. }
  198. bool WzExcel::insertValue(const int &row, const int &column, const QString &value)
  199. {
  200. qDebug()<<"WzExcel: insertValue(const int &row, const int &column, const QString &value)";
  201. if(!isOpened)
  202. {
  203. qDebug()<<"插入指定位置的单元格内容失败,文件没有打开,请先调用open函数";
  204. return false;
  205. }
  206. if(workSheet == NULL)
  207. {
  208. qDebug()<<"插入指定位置的单元格内容失败,workSheet对象为空,请先调用setCurrentWorkSheet函数";
  209. return false;
  210. }
  211. data = workSheet->querySubObject("Cells(int,int)", row, column);
  212. data->dynamicCall("Value", value);
  213. return true;
  214. }
  215. bool WzExcel::save()
  216. {
  217. qDebug()<<"WzExcel: save()";
  218. if(!isOpened)
  219. {
  220. qDebug()<<"保存失败,文件没有打开,请先调用open函数";
  221. return false;
  222. }
  223. QFile file(fileName);
  224. if (file.exists())
  225. {
  226. //文件存在则保存
  227. workBook->dynamicCall("Save()");
  228. }
  229. else
  230. {
  231. //文件不存在则另存为
  232. this->saveAs(fileName);
  233. }
  234. return true;
  235. }
  236. bool WzExcel::saveAs(const QString &fileName)
  237. {
  238. qDebug()<<"WzExcel: saveAs(const QString &fileName)";
  239. if(!isOpened)
  240. {
  241. qDebug()<<"另存为失败,文件没有打开,请先调用open函数";
  242. return false;
  243. }
  244. workBook->dynamicCall("SaveAs(const QString &)",
  245. QDir::toNativeSeparators(fileName));
  246. return true;
  247. }
  248. int WzExcel::getRows()
  249. {
  250. QAxObject* rowObj = getCurrentUserRange()->querySubObject("Rows");
  251. return rowObj->property("Count").toInt();
  252. }
  253. int WzExcel::getColumns()
  254. {
  255. QAxObject* columnObj = getCurrentUserRange()->querySubObject("Columns");
  256. return columnObj->property("Count").toInt();
  257. }
  258. void WzExcel::release()
  259. {
  260. isOpened = false;
  261. if(data != NULL)
  262. {
  263. delete data;
  264. data = NULL;
  265. }
  266. if(workSheet != NULL)
  267. {
  268. delete workSheet;
  269. workSheet = NULL;
  270. }
  271. if(workSheets != NULL)
  272. {
  273. delete workSheets;
  274. workSheets = NULL;
  275. }
  276. if(workBook != NULL)
  277. {
  278. workBook->dynamicCall("Close()");
  279. delete workBook;
  280. workBook = NULL;
  281. }
  282. if(workBooks != NULL)
  283. {
  284. workBooks->dynamicCall("Close()");
  285. delete workBooks;
  286. workBooks = NULL;
  287. }
  288. if (excel != NULL)
  289. {
  290. excel->dynamicCall("Quit()");
  291. delete excel;
  292. excel = NULL;
  293. }
  294. }
  295. QAxObject* WzExcel::getCurrentUserRange()
  296. {
  297. return workSheet->querySubObject("UsedRange");
  298. }