ExcelExport.cpp 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #include "ExcelExport.h"
  2. #include <QDebug>
  3. #include <iostream>
  4. #include <qt_windows.h>
  5. #include <qvector.h>
  6. using namespace std;
  7. ExcelExport::ExcelExport() {}
  8. void ExcelExport::newExcel(const QString &fileName) {
  9. HRESULT r = OleInitialize(0);
  10. if (r != S_OK && r != S_FALSE) {
  11. qDebug() << "初始化失败";
  12. }
  13. pApplication = new QAxObject();
  14. //连接Excel控件
  15. pApplication->setControl("Excel.Application");
  16. // false不显示窗体(看具体过程)
  17. pApplication->dynamicCall("SetVisible(bool)", false);
  18. //不显示任何警告信息
  19. pApplication->setProperty("DisplayAlerts", false);
  20. pWorkBooks = pApplication->querySubObject("Workbooks");
  21. QFile file(fileName);
  22. if (file.exists()) {
  23. file.remove();
  24. // pWorkBook = pWorkBooks->querySubObject("Open(const QString &", fileName);
  25. } // else {
  26. pWorkBooks->dynamicCall("Add");
  27. pWorkBook = pApplication->querySubObject("ActiveWorkBook");
  28. //}
  29. //默认有一个worksheet
  30. pSheets = pWorkBook->querySubObject("Sheets");
  31. pSheet = pSheets->querySubObject("Item(int)", 1);
  32. }
  33. void ExcelExport::appendSheet(const QString &sheetName) {
  34. int cnt = pSheets->property("Count").toInt();
  35. qDebug() << "appent---" << cnt;
  36. QAxObject *pLastSheet = pSheets->querySubObject("Item(int)", cnt);
  37. pSheets->querySubObject("Add(QVariant)", pLastSheet->asVariant());
  38. pSheet = pSheets->querySubObject("Item(int)", cnt);
  39. pLastSheet->dynamicCall("Move(QVariant)", pSheet->asVariant());
  40. pSheet->setProperty("Name", sheetName);
  41. }
  42. void ExcelExport::setCellValue(int row, int column, const QString &value) {
  43. QAxObject *pRange = pSheet->querySubObject("Cells(int,int)", row, column);
  44. pRange->setProperty("NumberFormatLocal", "@");
  45. pRange->dynamicCall("Value", value);
  46. //内容居中
  47. pRange->setProperty("HorizontalAlignment", -4108);
  48. pRange->setProperty("VerticalAlignment", -4108);
  49. }
  50. void ExcelExport::setHeaderCellValue(int row, int column,
  51. const QString &value) {
  52. QAxObject *pRange = pSheet->querySubObject("Cells(int,int)", row, column);
  53. QAxObject *cells = pRange->querySubObject("Columns");
  54. cells->dynamicCall("AutoFit");
  55. pRange->setProperty("NumberFormatLocal", "@");
  56. //换行
  57. pRange->setProperty("WrapText", true);
  58. //宽度
  59. pRange->setProperty("ColumnWidth",
  60. value.length() == 0 ? 20 : value.length() * 3);
  61. pRange->dynamicCall("Value", value);
  62. //内容居中
  63. pRange->setProperty("HorizontalAlignment", -4108);
  64. pRange->setProperty("VerticalAlignment", -4108);
  65. }
  66. void ExcelExport::setCellValue(int row, int column, const int &value) {
  67. QAxObject *pRange = pSheet->querySubObject("Cells(int,int)", row, column);
  68. pRange->setProperty("Value", value);
  69. //内容居中
  70. pRange->setProperty("HorizontalAlignment", -4108);
  71. pRange->setProperty("VerticalAlignment", -4108);
  72. }
  73. void ExcelExport::saveExcel(const QString &fileName) {
  74. //删除第一个sheet页
  75. QAxObject *firstSheet = pSheets->querySubObject("Item(int)", 1);
  76. firstSheet->dynamicCall("delete");
  77. //保存
  78. pWorkBook->dynamicCall("SaveAs(const QString &)",
  79. QDir::toNativeSeparators(fileName));
  80. if (pApplication != NULL) {
  81. qDebug() << "退出";
  82. pApplication->dynamicCall("Quit(void)"); //退出
  83. delete pApplication;
  84. pApplication = NULL;
  85. }
  86. }
  87. ExcelExport::~ExcelExport() {}