当前位置:   article > 正文

《Qt5+QAxObject操作Excel》_qaxobject 创建excel

qaxobject 创建excel

Qt5操作Excel需要用到QAxObject类,还需要在pro文件中添加QT+=axcontainer

QAxObject类

The QAxObject class provides a QObject that wraps a COM object. 

Header:#include <QAxObject>
qmake:QT += axcontainer
Inherits:QObject and QAxBase
Inherited By:

QAxScriptEngine

新建项目 

打开Qt,新建一个Qt Widgets Application项目,转到设计师模式,从左侧工具栏添加两个PushBotton控件到窗口上,调整PushBotton控件的大小,分别为两个PushBotton控件添加槽并修改按钮名字为创建Excel添加数据,具体做法如下:

选中按钮,右键,转到槽...,选择clicked(),OK

添加代码

选择项目文件名,右键,选择添加新文件,类名为Exchange,然后分别添加代码如下:

Pro文件

#-------------------------------------------------
#
# Project created by QtCreator 2018-11-06T14:28:30
#
#-------------------------------------------------
QT       += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = QtExcel
TEMPLATE = app
QT+=axcontainer
# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \       
 main.cpp \        
mainwindow.cpp \    
exchang.cpp
HEADERS += \        
mainwindow.h \    
exchange.h
FORMS += \        
mainwindow.ui

exchange.h

  1. #ifndef EXCHANGE_H
  2. #define EXCHANGE_H
  3. //添加头文件
  4. #include <QVariant>
  5. #include <ActiveQt/QAxObject>//Excel
  6. #include <QDebug>//debug输出
  7. #include <QDir>//保存路径
  8. class Exchange
  9. {
  10. public:
  11. Exchange();
  12. void SetTitle1(const QVariant &var,const QVariant &value);
  13. void SetTitle2(const QVariant &var,const QVariant &value);
  14. void SetInteriorColor(const QVariant &var,const QVariant &value);
  15. void SetFontProperty(const QVariant &var,const QVariant &value1,const QVariant &value2,const QVariant &value3,const QVariant &value4);
  16. void WriteData(const QVariant &var,const QVariant &value);
  17. void CreateExcel();
  18. void AddData();
  19. };
  20. #endif // EXCHANGE_H

exchang.cpp

  1. #include "exchange.h"
  2. QAxObject *excel;
  3. QAxObject *workbooks;
  4. QAxObject *workbook;
  5. QAxObject *worksheets;
  6. QAxObject *worksheet;
  7. QAxObject *range;
  8. QAxObject *interior;
  9. QAxObject *cell;
  10. QAxObject *font;
  11. //Excel保存路径
  12. QString path="C:/Users/dell/Desktop/Excel.xlsx";
  13. Exchange::Exchange()
  14. {
  15. }
  16. /***********************************
  17. * 函数功能:设置一级标题
  18. * var:单元格范围
  19. * value:一级标题名称
  20. * 创建时间:2018/10/17
  21. * 创建者:OYXL
  22. ************************************/
  23. void Exchange::SetTitle1(const QVariant &var,const QVariant &value)
  24. {
  25. range=worksheet->querySubObject("Range(const QString&)",var);
  26. range->setProperty("MergeCells",true);
  27. range->setProperty("Value",value);
  28. }
  29. /***********************************
  30. * 函数功能:设置二级标题
  31. * var:单元格范围
  32. * value:二级标题名称
  33. * 创建时间:2018/10/17
  34. * 创建者:OYXL
  35. ************************************/
  36. void Exchange::SetTitle2(const QVariant &var,const QVariant &value)
  37. {
  38. range=worksheet->querySubObject("Range(const QString&)",var);
  39. range->setProperty("Value",value);
  40. }
  41. /***********************************
  42. * 函数功能:按颜色序号设置背景色
  43. * var:单元格范围
  44. * value:颜色序号
  45. * 创建时间:2018/10/17
  46. * 创建者:OYXL
  47. ************************************/
  48. void Exchange::SetInteriorColor(const QVariant &var,const QVariant &value)
  49. {
  50. range=worksheet->querySubObject("Range(const QString&)",var);
  51. interior=range->querySubObject("Interior");
  52. interior->setProperty("ColorIndex",value); //按颜色序号进行颜色填充
  53. }
  54. /***********************************
  55. * 函数功能:设置字体属性
  56. * var:单元格范围
  57. * value1:列宽
  58. * value2:自动换行true或者false
  59. * value3:加粗true或者false
  60. * value4:颜色序号
  61. * 创建时间:2018/10/17
  62. * 创建者:OYXL
  63. ************************************/
  64. void Exchange::SetFontProperty(const QVariant &var,const QVariant &value1,const QVariant &value2,const QVariant &value3,const QVariant &value4)
  65. {
  66. range=worksheet->querySubObject("Range(const QString&)",var);
  67. range->setProperty("ColumnWidth",value1);
  68. range->setProperty("WrapText", value2);
  69. range->setProperty("HorizontalAlignment", -4108);//水平对齐:默认=1,居中=-4108,左=-4131,右=-4152
  70. range->setProperty("VerticalAlignment", -4108);//垂直对齐:默认=2,居中=-4108,左=-4160,右=-4107
  71. font = range->querySubObject("Font");//获取单元格字体
  72. font->setProperty("Name", QStringLiteral("微软雅黑"));//设置单元格字体
  73. font->setProperty("Bold", value3);//设置单元格字体加粗
  74. font->setProperty("Size", 12);//设置单元格字体大小
  75. font->setProperty("ColorIndex",value4);//按颜色序号进行颜色填充
  76. }
  77. /***********************************
  78. * 函数功能:将数据写入EXCEL
  79. * var:单元格范围
  80. * value:数据值
  81. * 创建时间:2018/10/17
  82. * 创建者:OYXL
  83. ************************************/
  84. void Exchange::WriteData(const QVariant &var,const QVariant &value)
  85. {
  86. range=worksheet->querySubObject("Range(const QString&)",var);
  87. range->setProperty("Value",value);
  88. }
  89. /***********************************
  90. * 函数功能:创建EXCEL表格
  91. * 创建时间:2018/7/5
  92. * 创建者:OYXL
  93. ************************************/
  94. void Exchange::CreateExcel()
  95. {
  96. Exchange change;
  97. excel = new QAxObject("Excel.Application");
  98. if (!excel)
  99. {
  100. qDebug()<<"创建Excel失败!";
  101. }
  102. excel->dynamicCall("SetVisible(bool Visible)", true); //是否可视化excel
  103. excel->dynamicCall("SetUserControl(bool UserControl)", false); //是否用户可操作
  104. //excel->setProperty("DisplayAlerts", true); //是否弹出警告窗口
  105. workbooks = excel->querySubObject("WorkBooks"); //获取工作簿集合
  106. workbooks->dynamicCall("Add"); //新建一个工作簿
  107. workbook = excel->querySubObject("ActiveWorkBook"); //获取当前工作簿
  108. worksheets = workbook->querySubObject("Sheets"); //获取工作表格集合
  109. worksheet = worksheets->querySubObject("Item(int)", 1); //获取当前工作表格1,即sheet1
  110. worksheet->setProperty("Name","恋爱数据"); //修改sheet名称
  111. //<添加数据一级标题
  112. change.SetTitle1("A1:A2","序号");//<序号
  113. change.SetTitle1("B1:B2","时间和日期");//<时间和日期
  114. change.SetTitle1("C1:C2","恋爱模式");//<恋爱模式
  115. change.SetTitle1("D1:D2","姓名");//<姓名
  116. change.SetTitle1("E1:E2","性别");//<性别
  117. change.SetTitle1("F1:F2","年龄");//<年龄
  118. change.SetTitle1("G1:G2","签名");//<签名
  119. change.SetTitle1("H1:K1","爱好");//爱好
  120. //<添加数据二级标题
  121. change.SetTitle2("H2:H2","运动");//运动
  122. change.SetTitle2("I2:I2","音乐");//音乐
  123. change.SetTitle2("J2:J2","舞蹈");//舞蹈
  124. change.SetTitle2("K2:K2","游戏");//游戏
  125. //<颜色填充
  126. change.SetInteriorColor("A1:G1",3);
  127. change.SetInteriorColor("A2:G2",3);
  128. change.SetInteriorColor("H1:K1",4);
  129. change.SetInteriorColor("H2:K2",4);
  130. change.SetFontProperty("A1:A2",5,true,true,2);
  131. change.SetFontProperty("B1:B2",20,true,true,2);
  132. change.SetFontProperty("C1:F2",8,true,true,2);
  133. change.SetFontProperty("G1:G2",20,true,true,2);
  134. change.SetFontProperty("H1:K1",8,true,true,2);
  135. change.SetFontProperty("H2:K2",8,true,true,2);
  136. workbook->dynamicCall("SaveAs(const QString&)", QDir::toNativeSeparators(path));
  137. }
  138. /*
  139. * 函数功能:添加实验数据至EXCEL
  140. * 创建时间:2018/7/5
  141. * 创建者:OYXL
  142. */
  143. void Exchange::AddData()
  144. {
  145. Exchange change;
  146. QString rowsNum="3";
  147. workbooks->dynamicCall("Open(const QString&)", QDir::toNativeSeparators(path));//打开工作簿
  148. workbook = excel->querySubObject("ActiveWorkBook"); //获取当前工作簿
  149. worksheets = workbook->querySubObject("Sheets"); //获取工作表格集合
  150. worksheet = worksheets->querySubObject("Item(int)", 1); //获取当前工作表格1,即sheet1
  151. change.WriteData("A"+rowsNum+":"+"A"+rowsNum,"1");//序号
  152. change.WriteData("B"+rowsNum+":"+"B"+rowsNum,"2018/11/6");//时间和日期
  153. change.WriteData("C"+rowsNum+":"+"C"+rowsNum,"日久生情");//恋爱模式
  154. change.WriteData("D"+rowsNum+":"+"D"+rowsNum,"可乐");//姓名
  155. change.WriteData("E"+rowsNum+":"+"E"+rowsNum,"女");//性别
  156. change.WriteData("F"+rowsNum+":"+"F"+rowsNum,"18");//年龄
  157. change.WriteData("G"+rowsNum+":"+"G"+rowsNum,"一只会拆家的二哈");//签名
  158. //<CH1实验数据
  159. change.WriteData("H"+rowsNum+":"+"H"+rowsNum,"狗刨");
  160. change.WriteData("I"+rowsNum+":"+"I"+rowsNum,"God is gril");
  161. change.WriteData("J"+rowsNum+":"+"J"+rowsNum,"转圈");
  162. change.WriteData("K"+rowsNum+":"+"K"+rowsNum,"LOL");
  163. //<整行处理
  164. //COLORREF ColorFont1=RGB(255,255,255);
  165. change.SetFontProperty("A"+rowsNum+":"+"A"+rowsNum,4,true,false,1);
  166. change.SetFontProperty("B"+rowsNum+":"+"B"+rowsNum,20,true,false,1);
  167. change.SetFontProperty("C"+rowsNum+":"+"F"+rowsNum,8,true,false,1);
  168. change.SetFontProperty("G"+rowsNum+":"+"G"+rowsNum,20,true,false,1);
  169. change.SetFontProperty("H"+rowsNum+":"+"K"+rowsNum,8,true,false,1);
  170. workbook->dynamicCall("Save()");//保存EXCEL
  171. //workbook->dynamicCall("Close()");//关闭工作簿
  172. //excel->dynamicCall("Quit()");//退出
  173. }

mainwindow.h

  1. #ifndef MAINWINDOW_H
  2. #define MAINWINDOW_H
  3. #include <QMainWindow>
  4. namespace Ui {
  5. class MainWindow;
  6. }
  7. class MainWindow : public QMainWindow
  8. {
  9. Q_OBJECT
  10. public:
  11. explicit MainWindow(QWidget *parent = 0);
  12. ~MainWindow();
  13. private slots:
  14. void on_btnCreateExcel_clicked();
  15. void on_btnAddData_clicked();
  16. private:
  17. Ui::MainWindow *ui;
  18. };
  19. #endif // MAINWINDOW_H

mainwindow.cpp

  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3. #include "exchange.h"
  4. MainWindow::MainWindow(QWidget *parent) :
  5. QMainWindow(parent),
  6. ui(new Ui::MainWindow)
  7. {
  8. ui->setupUi(this);
  9. }
  10. MainWindow::~MainWindow()
  11. {
  12. delete ui;
  13. }
  14. //创建Excel
  15. void MainWindow::on_btnCreateExcel_clicked()
  16. {
  17. Exchange change;
  18. change.CreateExcel();
  19. }
  20. //添加数据
  21. void MainWindow::on_btnAddData_clicked()
  22. {
  23. Exchange change;
  24. change.AddData();
  25. }

显示效果

完整代码

百度网盘链接:https://pan.baidu.com/s/1JXZYTFHb6_wL4_uylqsIYw 
提取码:j76q 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/170590
推荐阅读
相关标签
  

闽ICP备14008679号