Browse Source

添加自动根据建表excel模板生成orcle建表语句,添加excel操作类

lichanglin 3 năm trước cách đây
mục cha
commit
628097735b

+ 7 - 1
AutoGenetateModelServiceUI/AutoGenetateModelServiceUI.pro

@@ -1,4 +1,4 @@
-QT       += core gui
+QT       += core gui axcontainer
 
 greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
 
@@ -9,10 +9,14 @@ CONFIG += c++11
 #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
 
 SOURCES += \
+    TableHelper.cpp \
+    WzExcel.cpp \
     main.cpp \
     mainwindow.cpp
 
 HEADERS += \
+    TableHelper.h \
+    WzExcel.h \
     mainwindow.h \
     template/Model.cpp.template \
     template/Model.h.template \
@@ -32,3 +36,5 @@ FORMS += \
 qnx: target.path = /tmp/$${TARGET}/bin
 else: unix:!android: target.path = /opt/$${TARGET}/bin
 !isEmpty(target.path): INSTALLS += target
+
+INCLUDEPATH += ../include

+ 55 - 0
AutoGenetateModelServiceUI/TableHelper.cpp

@@ -0,0 +1,55 @@
+#include "TableHelper.h"
+
+#include <QStringList>
+
+TableHelper::TableHelper()
+{
+
+}
+
+QString TableHelper::createTable(const QString &tablename, const QList<QPair<QString, QString> > &fieldAndTypes)
+{
+    QStringList fields;
+    foreach(auto &item, fieldAndTypes)
+    {
+        fields << item.first +" " +  item.second;
+    }
+    return QString("create table %1 ( \n %2 \n ); \n").arg(tablename).arg(fields.join(", \n"));
+}
+
+QString TableHelper::createComment(const QString &tablename, const QList<QPair<QString, QString> > &fieldAndComments)
+{
+    QStringList comments;
+    foreach(auto &item, fieldAndComments)
+    {
+        comments << QString("COMMENT ON COLUMN %1.%2 IS '%3'; ").arg(tablename).arg(item.first).arg(item.second);
+    }
+    return comments.join("\n");
+}
+
+QString TableHelper::createPrimaryAlter(const QString &tablename)
+{
+    return QString("alter table %1 add constraint PK_%1_Id primary key(ID);").arg(tablename);
+}
+
+QString TableHelper::createSeq(const QString &tablename)
+{
+    return QString("drop sequence SEQ_%1; \n\
+                    CREATE SEQUENCE SEQ_%1 \n\
+                    START WITH 1 \n\
+                    MAXVALUE 99999999999999 \n\
+                    MINVALUE 1 \n\
+                    CYCLE \n\
+                    CACHE 20 \n\
+                    ORDER;").arg(tablename);
+}
+
+QString TableHelper::createTrigger(const QString &tablename)
+{
+    return QString("create or replace trigger %1_TRIGGER \n\
+                    before insert on %1  \n\
+                    for each row     \n\
+                    begin   \n\
+                    select  SEQ_%1.nextval into :new.ID from dual;\n\
+                    end;").arg(tablename);
+}

+ 23 - 0
AutoGenetateModelServiceUI/TableHelper.h

@@ -0,0 +1,23 @@
+#ifndef TABLEHELPER_H
+#define TABLEHELPER_H
+
+#include <QString>
+#include <QPair>
+
+class TableHelper
+{
+public:
+    TableHelper();
+
+    static QString createTable(const QString &tablename, const QList<QPair<QString,QString>> &fieldAndTypes);
+
+    static QString createComment(const QString &tablename, const QList<QPair<QString,QString>> &fieldAndComments);
+
+    static QString createPrimaryAlter(const QString &tablename);
+
+    static QString createSeq(const QString &tablename);
+
+    static QString createTrigger(const QString &tablename);
+};
+
+#endif // TABLEHELPER_H

+ 346 - 0
AutoGenetateModelServiceUI/WzExcel.cpp

@@ -0,0 +1,346 @@
+#include "WzExcel.h"
+#include "MessageException.h"
+#include "charcode.h"
+
+WzExcel::WzExcel()
+{
+    qDebug()<<"WzExcel: WzExcel()";
+
+    OleInitialize(0); //当前线程初始化COM库并设置并发模式STA(single-thread apartment——单线程单元)
+
+    excel = NULL;
+    workBooks = NULL;
+    workBook = NULL;
+    workSheets = NULL;
+    workSheet = NULL;
+    data = NULL;
+    isOpened = false;
+    fileName = "";
+    startRow = 0;
+    startColumn = 0;
+}
+
+WzExcel::WzExcel(const QString &fileName)
+{
+    qDebug()<<"WzExcel: WzExcel(const QString &filename)";
+
+    OleInitialize(0); //当前线程初始化COM库并设置并发模式STA(single-thread apartment——单线程单元)
+
+    excel = NULL;
+    workBooks = NULL;
+    workBook = NULL;
+    workSheets = NULL;
+    workSheet = NULL;
+    data = NULL;
+    isOpened = false;
+    startRow = 0;
+    startColumn = 0;
+    this->fileName = fileName;
+}
+
+WzExcel::~WzExcel()
+{
+    qDebug()<<"WzExcel: ~WzExcel()";
+
+    release();
+
+    OleUninitialize(); //关闭当前线程的COM库并释放相关资源
+}
+
+void WzExcel::setFileName(const QString fileName)
+{
+    qDebug()<<"WzExcel: setFileName(const QString fileName)";
+
+    this->fileName = fileName;
+}
+
+void WzExcel::open(bool visible,bool displayAlerts)
+{
+    qDebug()<<"WzExcel: open(bool visible,bool displayAlerts)";
+
+    if(fileName.isEmpty())
+    {
+        throw MessageException("打开失败,文件名为空,请设置文件名");
+    }
+    if (fileName.endsWith(".xlsx"))
+        excel = new QAxObject("Ket.Application"); 
+    else
+		excel = new QAxObject("Excel.Application"); //初始化excel对象
+    if(excel == NULL)
+    {
+        throw MessageException("请安装office");
+    }
+    excel->dynamicCall("SetVisible(bool)", visible); //false不显示窗体
+    excel->setProperty("DisplayAlerts", displayAlerts); //不显示警告。
+    workBooks = excel->querySubObject("WorkBooks"); //获取全部工作簿对象
+    if (!workBooks)
+        throw MessageException("Excel打开失败");
+    QFile file(fileName);
+    if (file.exists())
+    {
+        //导入文件到全部工作簿对象中,并将其设置为当前工作簿
+        workBook = workBooks->querySubObject("Open(const QString &)", fileName);
+        if (!workBook)
+            throw MessageException("Excel打开失败");
+    }
+    else
+    {
+        //文件不存在则创建
+        workBooks->dynamicCall("Add");
+        workBook = excel->querySubObject("ActiveWorkBook");
+    }
+
+    workSheets = workBook->querySubObject("Sheets"); //获得所有工作表对象
+    workSheet = workSheets->querySubObject("Item(int)", 1); //获得第一张工作表对象
+
+    QAxObject* userRange = this->getCurrentUserRange();
+    startRow = userRange->property("Row").toInt();
+    startColumn = userRange->property("Column").toInt();
+
+    isOpened = true;
+}
+
+void WzExcel::close()
+{
+    qDebug()<<"WzExcel: close()";
+
+    release();
+}
+
+bool WzExcel::setVisible(bool visible)
+{
+    qDebug()<<"WzExcel: setVisible(bool visible)";
+
+    if(!isOpened)
+    {
+        qDebug()<<"设置visible失败,文件没有打开,请先调用open函数";
+        return false;
+    }
+    else
+    {
+        excel->dynamicCall("SetVisible(bool)", visible);
+        return true;
+    }
+}
+
+bool WzExcel::setCurrentWorkSheet(const QString &sheetName)
+{
+    qDebug()<<"WzExcel: setCurrentWorkSheet(const QString &sheetName)";
+
+    if(!isOpened)
+    {
+        qDebug()<<"设置当前工作表失败,文件没有打开,请先调用open函数";
+        return false;
+    }
+
+    if(sheetName.isNull())
+    {
+        workSheet = workSheets->querySubObject("Item(int)",1); //获得第一张工作表对象
+    }
+    else
+    {
+        int count = workSheets->property("Count").toInt();
+        for(int i=1;i<=count;i++)
+        {
+            if(workSheets->querySubObject("Item(int)",i)->property("Name").toString() == sheetName)
+            {
+                workSheet = workSheets->querySubObject("Item(int)",i); //找到则设为指定名字的表对象
+                break;
+            }
+            if(i==count)
+            {
+                workSheet = workSheets->querySubObject("Item(int)",1); //如果找不到则设为第一张表
+            }
+        }
+    }
+    qDebug()<<workSheet->property("Name").toString();
+	QAxObject* usedrange = workSheet->querySubObject("UsedRange");
+	startRow = usedrange->property("Row").toInt();
+	startColumn = usedrange->property("Column").toInt();
+    return true;
+}
+
+bool WzExcel::createWorkSheet(const QString &sheetName)
+{
+    qDebug()<<"WzExcel: createWorkSheet(const QString &sheetName)";
+
+    if(!isOpened)
+    {
+        qDebug()<<"创建工作表失败,文件没有打开,请先调用open函数";
+        return false;
+    }
+
+    int count = workSheets->property("Count").toInt();
+    for(int i=1;i<=count;i++)
+    {
+        if(workSheets->querySubObject("Item(int)",i)->property("Name").toString() == sheetName)
+        {
+            qDebug()<<"该表已存在!";
+            return false;
+        }
+    }
+    QAxObject *lastSheet = workSheets->querySubObject("Item(int)", count);
+    workSheets->querySubObject("Add(QVariant)", lastSheet->asVariant());
+    QAxObject *newSheet = workSheets->querySubObject("Item(int)", count);
+    lastSheet->dynamicCall("Move(QVariant)", newSheet->asVariant());
+    newSheet->setProperty("Name", sheetName);
+    return true;
+}
+
+bool WzExcel::deleteWorkSheet(const QString &sheetName)
+{
+    qDebug()<<"WzExcel: deleteWorkSheet(const QString &sheetName)";
+
+    if(!isOpened)
+    {
+        qDebug()<<"删除工作表失败,文件没有打开,请先调用open函数";
+        return false;
+    }
+
+    int count = workSheets->property("Count").toInt();
+    for(int i=1;i<=count;i++)
+    {
+        if(workSheets->querySubObject("Item(int)",i)->property("Name").toString() == sheetName)
+        {
+            workSheets->querySubObject("Item(int)",i)->dynamicCall("delete");
+            break;
+        }
+    }
+    return true;
+}
+
+QVariant WzExcel::getValue(const int &row, const int &column)
+{
+    qDebug()<<"WzExcel: getValue(const int &row, const int &column)";
+
+    if(!isOpened)
+    {
+        qDebug()<<"获得指定位置的单元格内容失败,文件没有打开,请先调用open函数";
+        return "";
+    }
+
+    if(workSheet == NULL)
+    {
+        qDebug()<<"获得指定位置的单元格内容失败,workSheet对象为空,请先调用setCurrentWorkSheet函数";
+        return "";
+    }
+
+    data = workSheet->querySubObject("Cells(int,int)", startRow + row, startColumn + column);
+    return data->dynamicCall("Value()");
+}
+
+bool WzExcel::insertValue(const int &row, const int &column, const QString &value)
+{
+    qDebug()<<"WzExcel: insertValue(const int &row, const int &column, const QString &value)";
+
+    if(!isOpened)
+    {
+        qDebug()<<"插入指定位置的单元格内容失败,文件没有打开,请先调用open函数";
+        return false;
+    }
+
+    if(workSheet == NULL)
+    {
+        qDebug()<<"插入指定位置的单元格内容失败,workSheet对象为空,请先调用setCurrentWorkSheet函数";
+        return false;
+    }
+
+    data = workSheet->querySubObject("Cells(int,int)", row, column);
+    data->dynamicCall("Value", value);
+    return true;
+}
+
+bool WzExcel::save()
+{
+    qDebug()<<"WzExcel: save()";
+
+    if(!isOpened)
+    {
+        qDebug()<<"保存失败,文件没有打开,请先调用open函数";
+        return false;
+    }
+
+    QFile file(fileName);
+    if (file.exists())
+    {
+        //文件存在则保存
+        workBook->dynamicCall("Save()");
+    }
+    else
+    {
+        //文件不存在则另存为
+        this->saveAs(fileName);
+    }
+    return true;
+}
+
+bool WzExcel::saveAs(const QString &fileName)
+{
+    qDebug()<<"WzExcel: saveAs(const QString &fileName)";
+
+    if(!isOpened)
+    {
+        qDebug()<<"另存为失败,文件没有打开,请先调用open函数";
+        return false;
+    }
+
+    workBook->dynamicCall("SaveAs(const QString &)",
+                          QDir::toNativeSeparators(fileName));
+    return true;
+}
+
+int WzExcel::getRows()
+{
+	QAxObject* rowObj = getCurrentUserRange()->querySubObject("Rows");
+	return rowObj->property("Count").toInt();
+}
+
+int WzExcel::getColumns()
+{
+	QAxObject* columnObj = getCurrentUserRange()->querySubObject("Columns");
+	return columnObj->property("Count").toInt();
+}
+
+void WzExcel::release()
+{
+    isOpened = false;
+    if(data != NULL)
+    {
+        delete data;
+        data = NULL;
+    }
+    if(workSheet != NULL)
+    {
+        delete workSheet;
+        workSheet = NULL;
+    }
+    if(workSheets != NULL)
+    {
+        delete workSheets;
+        workSheets = NULL;
+    }
+
+    if(workBook != NULL)
+    {
+        workBook->dynamicCall("Close()");
+        delete workBook;
+        workBook = NULL;
+    }
+    if(workBooks != NULL)
+    {
+        workBooks->dynamicCall("Close()");
+        delete workBooks;
+        workBooks = NULL;
+    }
+    if (excel != NULL)
+    {
+        excel->dynamicCall("Quit()");
+        delete excel;
+        excel = NULL;
+    }
+}
+
+QAxObject* WzExcel::getCurrentUserRange()
+{
+	return workSheet->querySubObject("UsedRange");
+}

+ 103 - 0
AutoGenetateModelServiceUI/WzExcel.h

@@ -0,0 +1,103 @@
+
+#ifndef _WZEXCEL_H
+#define _WZEXCEL_H
+
+#include <QAxObject>
+#include <QString>
+#include <QDir>
+#include <QFile>
+#include <QDebug>
+#include <windows.h>
+
+/*
+ *  类:WzExcel
+ *  作用:Qt操作Excel
+ *  作者:欧阳伟
+ *  日期:2017-12-19
+ *  用法示例(需在*.pro文件中添加:QT += axcontainer)
+ *      WzExcel w("D:/hello.xlsx");                            //创建对象
+ *      if(w.open())                                           //打开
+ *      {
+ *          //w.setCurrentWorkSheet();                           //设置当前工作表
+ *          for(int i=0;i<10;i++)
+ *          {
+ *              for(int 0=1;j<10;j++)
+ *              {
+ *                  w.insertValue(i,j,QString::number(i*j));   //修改内容
+ *                  //w.getValue(i,j,QString::number(i*j));   //修改内容
+ *              }
+ *          }
+ *          w.save();                                          //保存
+ *          w.saveAs("D:/hello1.xlsx");                        //另存为
+ *      }
+ *
+ *  说明:创建对象需传入绝对路径。
+ */
+
+class WzExcel
+{
+public:
+    WzExcel();
+    //传入需要操作的Excel文件的【绝对路径】
+    WzExcel(const QString &fileName);
+    ~WzExcel();
+
+    //设置文件名
+    void setFileName(const QString fileName);
+
+    //打开,不存在则【新建一个工作簿(保存时以传入时的文件名保存)】,成功返回true,失败返回false
+    void open(bool visible=false,bool displayAlerts=false);
+
+    //关闭
+    void close();
+
+    //设置visible,true: 可视,false: 隐藏
+    bool setVisible(bool visible);
+
+    //设置当前工作表,成功返回true,失败返回false
+    bool setCurrentWorkSheet(const QString &sheetName=NULL);
+
+    //创建工作表,成功返回true,失败返回false
+    bool createWorkSheet(const QString &sheetName);
+
+    //删除工作表,成功返回true,失败返回false
+    bool deleteWorkSheet(const QString &sheetName);
+
+    //获得指定位置的单元格内容,成功返回该值,失败返回NULL
+    QVariant getValue(const int &row,const int &column);
+
+    //插入指定位置的单元格内容,成功返回true,失败返回false
+    bool insertValue(const int &row,const int &colum,const QString &value);
+
+    //保存,成功返回true,失败返回false
+    bool save();
+
+    //另存为,成功返回true,失败返回false
+    bool saveAs(const QString &fileName);
+
+	int getRows();
+
+	int getColumns();
+
+private:
+    void release();
+
+    QAxObject* getCurrentUserRange();
+	
+private:
+    QString fileName; //文件名
+
+    bool isOpened;
+
+    QAxObject *excel; //excel对象
+    QAxObject *workBooks; //所有工作簿对象
+    QAxObject *workBook; //当前工作簿对象
+    QAxObject *workSheets; //所有工作表对象
+    QAxObject *workSheet; //当前工作表对象
+    QAxObject *data; //当前数据对象
+ 
+    int startRow, startColumn;
+};
+
+
+#endif

+ 2 - 1
AutoGenetateModelServiceUI/main.cpp

@@ -1,10 +1,11 @@
-#include "mainwindow.h"
+#include "mainwindow.h"
 
 #include <QApplication>
 
 int main(int argc, char *argv[])
 {
     QApplication a(argc, argv);
+    int aaa = 111;
     MainWindow w;
     w.show();
     return a.exec();

+ 61 - 2
AutoGenetateModelServiceUI/mainwindow.cpp

@@ -1,10 +1,16 @@
-#include "MainWindow.h"
+#include "MainWindow.h"
 #include <qfiledialog.h>
 #include <qsettings.h>
 #include <qdatastream.h>
 #include <qfile.h>
 #include <qtextstream.h>
 #include <qmessagebox.h>
+#include <QTextStream>
+
+#include "MessageException.h"
+#include "TableHelper.h"
+#include "DebugUtil.h"
+#include "WzExcel.h"
 
 MainWindow::MainWindow(QWidget *parent)
 	: QMainWindow(parent)
@@ -64,7 +70,60 @@ void MainWindow::onGenerate()
 		createMultipleWidget(widgetName, modelName, serviceName, entityName,ui.name->text().trimmed());
 	else
 		createWidget(widgetName, modelName, serviceName, entityName, ui.name->text().trimmed());
-	QMessageBox::information(this, "", "success");
+    QMessageBox::information(this, "", "success");
+}
+
+void MainWindow::onTableFilePath()
+{
+    QString filename = QFileDialog::getExistingDirectory(this);
+    ui.tableFilePath->setText(filename);
+    QSettings setting("config.ini", QSettings::IniFormat);
+    setting.setValue("TableFilePath",filename);
+    setting.sync();
+}
+
+void MainWindow::onExcelPath()
+{
+    QString path = QFileDialog::getOpenFileName(this);
+    ui.excelPath->setText(path);
+}
+
+void MainWindow::onGenerateTable()
+{
+    WzExcel excelHelper(ui.excelPath->text().trimmed());
+    try
+    {
+        excelHelper.open();
+    }
+    catch (MessageException &e)
+    {
+        excelHelper.close();
+        QMessageBox::information(this,"提示",e.getMessage());
+        return;
+    }  
+    QString tablename = excelHelper.getValue(0,1).toString();
+    Debug(tablename);
+    int rows = excelHelper.getRows();
+
+    QList<QPair<QString,QString>> fieldAndTypes;
+    QList<QPair<QString,QString>> fieldAndComments;
+    for (int row = 2 ; row < rows ; row++)
+    {
+        QString copmment = excelHelper.getValue(row,0).toString();
+        QString name = excelHelper.getValue(row,1).toString();
+        QString type = excelHelper.getValue(row,2).toString();
+        fieldAndTypes << qMakePair(name,type);
+        fieldAndComments << qMakePair(name,copmment);
+    }
+    excelHelper.close();
+    QStringList contents;
+    contents << TableHelper::createTable(tablename,fieldAndTypes)
+             << TableHelper::createPrimaryAlter(tablename)
+             << TableHelper::createComment(tablename,fieldAndComments)
+             << TableHelper::createSeq(tablename)
+             << TableHelper::createTrigger(tablename);
+    write(ui.tableFilePath->text().trimmed() + "/" + tablename + ".txt",contents.join("\n"));
+    ui.textEdit->setText(contents.join("\n\n"));
 }
 
 QString MainWindow::readAll(const QString &filename)

+ 8 - 1
AutoGenetateModelServiceUI/mainwindow.h

@@ -1,4 +1,4 @@
-#pragma once
+#pragma once
 
 #include <QtWidgets/QMainWindow>
 #include "ui_MainWindow.h"
@@ -16,6 +16,13 @@ private slots:
 	void onWidgetPath();
 	void onGenerate();
 
+    /**
+     * @brief onTableFilePath 生成数据库建表语句的文件存储的位置
+     */
+    void onTableFilePath();
+    void onExcelPath();
+    void onGenerateTable();
+
 private:
 	void createModel(const QString &modelName, const QString &entityName);
 	void createService(const QString &serviceName, const QString &entityName);

+ 342 - 189
AutoGenetateModelServiceUI/mainwindow.ui

@@ -6,198 +6,300 @@
    <rect>
     <x>0</x>
     <y>0</y>
-    <width>600</width>
-    <height>400</height>
+    <width>798</width>
+    <height>645</height>
    </rect>
   </property>
   <property name="windowTitle">
    <string>自动生成</string>
   </property>
   <widget class="QWidget" name="centralWidget">
-   <layout class="QVBoxLayout" name="verticalLayout">
+   <layout class="QVBoxLayout" name="verticalLayout_2">
     <item>
-     <layout class="QHBoxLayout" name="horizontalLayout_3">
-      <item>
-       <widget class="QLabel" name="label_2">
-        <property name="text">
-         <string>实体类中文名</string>
-        </property>
-       </widget>
-      </item>
-      <item>
-       <widget class="QLineEdit" name="name"/>
-      </item>
-     </layout>
-    </item>
-    <item>
-     <layout class="QGridLayout" name="gridLayout_2">
-      <item row="0" column="0">
-       <widget class="QLabel" name="label">
-        <property name="text">
-         <string>entity名称</string>
-        </property>
-       </widget>
-      </item>
-      <item row="0" column="1">
-       <widget class="QLineEdit" name="entityName"/>
-      </item>
-      <item row="1" column="0">
-       <widget class="QLabel" name="label_3">
-        <property name="text">
-         <string>model名称</string>
-        </property>
-       </widget>
-      </item>
-      <item row="1" column="1">
-       <widget class="QLineEdit" name="modelName">
-        <property name="placeholderText">
-         <string>默认实体类名称后+Model</string>
-        </property>
-       </widget>
-      </item>
-      <item row="2" column="0">
-       <widget class="QLabel" name="label_4">
-        <property name="text">
-         <string>service名称</string>
-        </property>
-       </widget>
-      </item>
-      <item row="2" column="1">
-       <widget class="QLineEdit" name="serviceName">
-        <property name="placeholderText">
-         <string>默认实体类名称后+Service</string>
-        </property>
-       </widget>
-      </item>
-      <item row="3" column="0">
-       <widget class="QLabel" name="label_5">
-        <property name="text">
-         <string>Widget名称</string>
-        </property>
-       </widget>
-      </item>
-      <item row="3" column="1">
-       <layout class="QHBoxLayout" name="horizontalLayout_2">
+     <widget class="QTabWidget" name="tabWidget">
+      <property name="currentIndex">
+       <number>1</number>
+      </property>
+      <widget class="QWidget" name="tab">
+       <attribute name="title">
+        <string>UI相关</string>
+       </attribute>
+       <layout class="QVBoxLayout" name="verticalLayout">
+        <item>
+         <layout class="QHBoxLayout" name="horizontalLayout_3">
+          <item>
+           <widget class="QLabel" name="label_2">
+            <property name="text">
+             <string>实体类中文名</string>
+            </property>
+           </widget>
+          </item>
+          <item>
+           <widget class="QLineEdit" name="name"/>
+          </item>
+         </layout>
+        </item>
+        <item>
+         <layout class="QGridLayout" name="gridLayout_2">
+          <item row="0" column="0">
+           <widget class="QLabel" name="label">
+            <property name="text">
+             <string>entity名称</string>
+            </property>
+           </widget>
+          </item>
+          <item row="0" column="1">
+           <widget class="QLineEdit" name="entityName"/>
+          </item>
+          <item row="1" column="0">
+           <widget class="QLabel" name="label_3">
+            <property name="text">
+             <string>model名称</string>
+            </property>
+           </widget>
+          </item>
+          <item row="1" column="1">
+           <widget class="QLineEdit" name="modelName">
+            <property name="placeholderText">
+             <string>默认实体类名称后+Model</string>
+            </property>
+           </widget>
+          </item>
+          <item row="2" column="0">
+           <widget class="QLabel" name="label_4">
+            <property name="text">
+             <string>service名称</string>
+            </property>
+           </widget>
+          </item>
+          <item row="2" column="1">
+           <widget class="QLineEdit" name="serviceName">
+            <property name="placeholderText">
+             <string>默认实体类名称后+Service</string>
+            </property>
+           </widget>
+          </item>
+          <item row="3" column="0">
+           <widget class="QLabel" name="label_5">
+            <property name="text">
+             <string>Widget名称</string>
+            </property>
+           </widget>
+          </item>
+          <item row="3" column="1">
+           <layout class="QHBoxLayout" name="horizontalLayout_2">
+            <item>
+             <widget class="QLineEdit" name="widgetName">
+              <property name="placeholderText">
+               <string>默认实体类名称后+Widget</string>
+              </property>
+             </widget>
+            </item>
+            <item>
+             <widget class="QCheckBox" name="MultipleSelect">
+              <property name="text">
+               <string>多选</string>
+              </property>
+             </widget>
+            </item>
+           </layout>
+          </item>
+         </layout>
+        </item>
         <item>
-         <widget class="QLineEdit" name="widgetName">
-          <property name="placeholderText">
-           <string>默认实体类名称后+Widget</string>
-          </property>
-         </widget>
+         <layout class="QGridLayout" name="gridLayout">
+          <item row="0" column="0">
+           <widget class="QLabel" name="label_6">
+            <property name="text">
+             <string>model目录</string>
+            </property>
+           </widget>
+          </item>
+          <item row="0" column="1">
+           <widget class="QLineEdit" name="modelPath">
+            <property name="text">
+             <string/>
+            </property>
+           </widget>
+          </item>
+          <item row="0" column="2">
+           <widget class="QPushButton" name="pbModelPath">
+            <property name="text">
+             <string>选择</string>
+            </property>
+           </widget>
+          </item>
+          <item row="1" column="0">
+           <widget class="QLabel" name="label_8">
+            <property name="text">
+             <string>service目录</string>
+            </property>
+           </widget>
+          </item>
+          <item row="1" column="1">
+           <widget class="QLineEdit" name="servicePath">
+            <property name="text">
+             <string/>
+            </property>
+           </widget>
+          </item>
+          <item row="1" column="2">
+           <widget class="QPushButton" name="pbServicelPath">
+            <property name="text">
+             <string>选择</string>
+            </property>
+           </widget>
+          </item>
+          <item row="2" column="0">
+           <widget class="QLabel" name="label_7">
+            <property name="text">
+             <string>Widget目录</string>
+            </property>
+           </widget>
+          </item>
+          <item row="2" column="1">
+           <widget class="QLineEdit" name="widgetPath">
+            <property name="text">
+             <string/>
+            </property>
+           </widget>
+          </item>
+          <item row="2" column="2">
+           <widget class="QPushButton" name="pbWidgetPath">
+            <property name="sizePolicy">
+             <sizepolicy hsizetype="Minimum" vsizetype="Preferred">
+              <horstretch>0</horstretch>
+              <verstretch>0</verstretch>
+             </sizepolicy>
+            </property>
+            <property name="text">
+             <string>选择</string>
+            </property>
+           </widget>
+          </item>
+         </layout>
         </item>
         <item>
-         <widget class="QCheckBox" name="MultipleSelect">
-          <property name="text">
-           <string>多选</string>
-          </property>
-         </widget>
+         <layout class="QHBoxLayout" name="horizontalLayout">
+          <item>
+           <spacer name="horizontalSpacer">
+            <property name="orientation">
+             <enum>Qt::Horizontal</enum>
+            </property>
+            <property name="sizeHint" stdset="0">
+             <size>
+              <width>40</width>
+              <height>20</height>
+             </size>
+            </property>
+           </spacer>
+          </item>
+          <item>
+           <widget class="QPushButton" name="pbGenerate">
+            <property name="sizePolicy">
+             <sizepolicy hsizetype="Preferred" vsizetype="Fixed">
+              <horstretch>0</horstretch>
+              <verstretch>0</verstretch>
+             </sizepolicy>
+            </property>
+            <property name="text">
+             <string>生成</string>
+            </property>
+           </widget>
+          </item>
+         </layout>
         </item>
        </layout>
-      </item>
-     </layout>
-    </item>
-    <item>
-     <layout class="QGridLayout" name="gridLayout">
-      <item row="0" column="0">
-       <widget class="QLabel" name="label_6">
-        <property name="text">
-         <string>model路径</string>
-        </property>
-       </widget>
-      </item>
-      <item row="0" column="1">
-       <widget class="QLineEdit" name="modelPath">
-        <property name="text">
-         <string/>
-        </property>
-       </widget>
-      </item>
-      <item row="0" column="2">
-       <widget class="QPushButton" name="pbModelPath">
-        <property name="text">
-         <string>选择</string>
-        </property>
-       </widget>
-      </item>
-      <item row="1" column="0">
-       <widget class="QLabel" name="label_8">
-        <property name="text">
-         <string>service路径</string>
-        </property>
-       </widget>
-      </item>
-      <item row="1" column="1">
-       <widget class="QLineEdit" name="servicePath">
-        <property name="text">
-         <string/>
-        </property>
-       </widget>
-      </item>
-      <item row="1" column="2">
-       <widget class="QPushButton" name="pbServicelPath">
-        <property name="text">
-         <string>选择</string>
-        </property>
-       </widget>
-      </item>
-      <item row="2" column="0">
-       <widget class="QLabel" name="label_7">
-        <property name="text">
-         <string>Widget路径</string>
-        </property>
-       </widget>
-      </item>
-      <item row="2" column="1">
-       <widget class="QLineEdit" name="widgetPath">
-        <property name="text">
-         <string/>
-        </property>
-       </widget>
-      </item>
-      <item row="2" column="2">
-       <widget class="QPushButton" name="pbWidgetPath">
-        <property name="sizePolicy">
-         <sizepolicy hsizetype="Minimum" vsizetype="Preferred">
-          <horstretch>0</horstretch>
-          <verstretch>0</verstretch>
-         </sizepolicy>
-        </property>
-        <property name="text">
-         <string>选择</string>
-        </property>
-       </widget>
-      </item>
-     </layout>
-    </item>
-    <item>
-     <layout class="QHBoxLayout" name="horizontalLayout">
-      <item>
-       <spacer name="horizontalSpacer">
-        <property name="orientation">
-         <enum>Qt::Horizontal</enum>
-        </property>
-        <property name="sizeHint" stdset="0">
-         <size>
-          <width>40</width>
-          <height>20</height>
-         </size>
-        </property>
-       </spacer>
-      </item>
-      <item>
-       <widget class="QPushButton" name="pbGenerate">
-        <property name="sizePolicy">
-         <sizepolicy hsizetype="Preferred" vsizetype="Fixed">
-          <horstretch>0</horstretch>
-          <verstretch>0</verstretch>
-         </sizepolicy>
-        </property>
-        <property name="text">
-         <string>生成</string>
-        </property>
-       </widget>
-      </item>
-     </layout>
+      </widget>
+      <widget class="QWidget" name="tab_2">
+       <attribute name="title">
+        <string>数据库建表</string>
+       </attribute>
+       <layout class="QVBoxLayout" name="verticalLayout_3">
+        <item>
+         <layout class="QGridLayout" name="gridLayout_3">
+          <item row="1" column="2">
+           <widget class="QPushButton" name="btnTableFilePath">
+            <property name="text">
+             <string> 选择</string>
+            </property>
+           </widget>
+          </item>
+          <item row="0" column="2">
+           <widget class="QPushButton" name="btnExcelPath">
+            <property name="text">
+             <string> 选择</string>
+            </property>
+           </widget>
+          </item>
+          <item row="0" column="1">
+           <widget class="QLineEdit" name="excelPath"/>
+          </item>
+          <item row="1" column="1">
+           <widget class="QLineEdit" name="tableFilePath"/>
+          </item>
+          <item row="0" column="0">
+           <widget class="QLabel" name="label_10">
+            <property name="text">
+             <string> 数据库字段文件(excel):</string>
+            </property>
+            <property name="alignment">
+             <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
+            </property>
+           </widget>
+          </item>
+          <item row="1" column="0">
+           <widget class="QLabel" name="label_11">
+            <property name="text">
+             <string>生成文件目录:</string>
+            </property>
+            <property name="textFormat">
+             <enum>Qt::MarkdownText</enum>
+            </property>
+            <property name="alignment">
+             <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
+            </property>
+           </widget>
+          </item>
+         </layout>
+        </item>
+        <item>
+         <widget class="QTextEdit" name="textEdit"/>
+        </item>
+        <item>
+         <layout class="QHBoxLayout" name="horizontalLayout_5">
+          <item>
+           <widget class="QLabel" name="label_9">
+            <property name="text">
+             <string/>
+            </property>
+           </widget>
+          </item>
+          <item>
+           <spacer name="horizontalSpacer_2">
+            <property name="orientation">
+             <enum>Qt::Horizontal</enum>
+            </property>
+            <property name="sizeHint" stdset="0">
+             <size>
+              <width>40</width>
+              <height>20</height>
+             </size>
+            </property>
+           </spacer>
+          </item>
+          <item>
+           <widget class="QPushButton" name="generateTable">
+            <property name="text">
+             <string>   生成</string>
+            </property>
+           </widget>
+          </item>
+         </layout>
+        </item>
+       </layout>
+      </widget>
+     </widget>
     </item>
    </layout>
   </widget>
@@ -206,7 +308,7 @@
     <rect>
      <x>0</x>
      <y>0</y>
-     <width>600</width>
+     <width>798</width>
      <height>23</height>
     </rect>
    </property>
@@ -231,8 +333,8 @@
    <slot>onModelPath()</slot>
    <hints>
     <hint type="sourcelabel">
-     <x>589</x>
-     <y>182</y>
+     <x>696</x>
+     <y>301</y>
     </hint>
     <hint type="destinationlabel">
      <x>601</x>
@@ -247,8 +349,8 @@
    <slot>onServicePath()</slot>
    <hints>
     <hint type="sourcelabel">
-     <x>589</x>
-     <y>216</y>
+     <x>696</x>
+     <y>331</y>
     </hint>
     <hint type="destinationlabel">
      <x>598</x>
@@ -263,8 +365,8 @@
    <slot>onWidgetPath()</slot>
    <hints>
     <hint type="sourcelabel">
-     <x>589</x>
-     <y>250</y>
+     <x>696</x>
+     <y>361</y>
     </hint>
     <hint type="destinationlabel">
      <x>600</x>
@@ -279,8 +381,8 @@
    <slot>onGenerate()</slot>
    <hints>
     <hint type="sourcelabel">
-     <x>530</x>
-     <y>315</y>
+     <x>696</x>
+     <y>427</y>
     </hint>
     <hint type="destinationlabel">
      <x>508</x>
@@ -288,11 +390,62 @@
     </hint>
    </hints>
   </connection>
+  <connection>
+   <sender>generateTable</sender>
+   <signal>clicked()</signal>
+   <receiver>MainWindowClass</receiver>
+   <slot>onGenerateTable()</slot>
+   <hints>
+    <hint type="sourcelabel">
+     <x>659</x>
+     <y>369</y>
+    </hint>
+    <hint type="destinationlabel">
+     <x>639</x>
+     <y>511</y>
+    </hint>
+   </hints>
+  </connection>
+  <connection>
+   <sender>btnTableFilePath</sender>
+   <signal>clicked()</signal>
+   <receiver>MainWindowClass</receiver>
+   <slot>onTableFilePath()</slot>
+   <hints>
+    <hint type="sourcelabel">
+     <x>646</x>
+     <y>225</y>
+    </hint>
+    <hint type="destinationlabel">
+     <x>729</x>
+     <y>283</y>
+    </hint>
+   </hints>
+  </connection>
+  <connection>
+   <sender>btnExcelPath</sender>
+   <signal>clicked()</signal>
+   <receiver>MainWindowClass</receiver>
+   <slot>onExcelPath()</slot>
+   <hints>
+    <hint type="sourcelabel">
+     <x>677</x>
+     <y>164</y>
+    </hint>
+    <hint type="destinationlabel">
+     <x>718</x>
+     <y>165</y>
+    </hint>
+   </hints>
+  </connection>
  </connections>
  <slots>
   <slot>onModelPath()</slot>
   <slot>onServicePath()</slot>
   <slot>onWidgetPath()</slot>
   <slot>onGenerate()</slot>
+  <slot>onGenerateTable()</slot>
+  <slot>onExcelPath()</slot>
+  <slot>onTableFilePath()</slot>
  </slots>
 </ui>

+ 2 - 2
DataManage/DataManage.pro

@@ -32,7 +32,7 @@ SOURCES += \
     utils/FormBuilder.cpp \
     utils/IxWidgetUtil.cpp \
     utils/SqlGenerator_Oracle.cpp \
-    utils/excelinport.cpp \
+    utils/WzExcel.cpp \
     widgets/AdvancedTableView.cpp \
     widgets/CustomDelegate.cpp \
     widgets/DialogTemplate.cpp \
@@ -84,8 +84,8 @@ HEADERS += \
     utils/IxWidgetUtil.h \
     utils/MessageException.h \
     utils/SqlGenerator_Oracle.h \
+    utils/WzExcel.h \
     utils/charcode.h \
-    utils/excelinport.h \
     utils/regExp.h \
     utils/utils.h \
     widgets/AdvancedTableView.h \

+ 346 - 0
DataManage/utils/WzExcel.cpp

@@ -0,0 +1,346 @@
+#include "WzExcel.h"
+#include "MessageException.h"
+#include "charcode.h"
+
+WzExcel::WzExcel()
+{
+    qDebug()<<"WzExcel: WzExcel()";
+
+    OleInitialize(0); //当前线程初始化COM库并设置并发模式STA(single-thread apartment——单线程单元)
+
+    excel = NULL;
+    workBooks = NULL;
+    workBook = NULL;
+    workSheets = NULL;
+    workSheet = NULL;
+    data = NULL;
+    isOpened = false;
+    fileName = "";
+    startRow = 0;
+    startColumn = 0;
+}
+
+WzExcel::WzExcel(const QString &fileName)
+{
+    qDebug()<<"WzExcel: WzExcel(const QString &filename)";
+
+    OleInitialize(0); //当前线程初始化COM库并设置并发模式STA(single-thread apartment——单线程单元)
+
+    excel = NULL;
+    workBooks = NULL;
+    workBook = NULL;
+    workSheets = NULL;
+    workSheet = NULL;
+    data = NULL;
+    isOpened = false;
+    startRow = 0;
+    startColumn = 0;
+    this->fileName = fileName;
+}
+
+WzExcel::~WzExcel()
+{
+    qDebug()<<"WzExcel: ~WzExcel()";
+
+    release();
+
+    OleUninitialize(); //关闭当前线程的COM库并释放相关资源
+}
+
+void WzExcel::setFileName(const QString fileName)
+{
+    qDebug()<<"WzExcel: setFileName(const QString fileName)";
+
+    this->fileName = fileName;
+}
+
+void WzExcel::open(bool visible,bool displayAlerts)
+{
+    qDebug()<<"WzExcel: open(bool visible,bool displayAlerts)";
+
+    if(fileName.isEmpty())
+    {
+        throw MessageException("打开失败,文件名为空,请设置文件名");
+    }
+    if (fileName.endsWith(".xlsx"))
+        excel = new QAxObject("Ket.Application"); 
+    else
+		excel = new QAxObject("Excel.Application"); //初始化excel对象
+    if(excel == NULL)
+    {
+        throw MessageException("请安装office");
+    }
+    excel->dynamicCall("SetVisible(bool)", visible); //false不显示窗体
+    excel->setProperty("DisplayAlerts", displayAlerts); //不显示警告。
+    workBooks = excel->querySubObject("WorkBooks"); //获取全部工作簿对象
+    if (!workBooks)
+        throw MessageException("Excel打开失败");
+    QFile file(fileName);
+    if (file.exists())
+    {
+        //导入文件到全部工作簿对象中,并将其设置为当前工作簿
+        workBook = workBooks->querySubObject("Open(const QString &)", fileName);
+        if (!workBook)
+            throw MessageException("Excel打开失败");
+    }
+    else
+    {
+        //文件不存在则创建
+        workBooks->dynamicCall("Add");
+        workBook = excel->querySubObject("ActiveWorkBook");
+    }
+
+    workSheets = workBook->querySubObject("Sheets"); //获得所有工作表对象
+    workSheet = workSheets->querySubObject("Item(int)", 1); //获得第一张工作表对象
+
+    QAxObject* userRange = this->getCurrentUserRange();
+    startRow = userRange->property("Row").toInt();
+    startColumn = userRange->property("Column").toInt();
+
+    isOpened = true;
+}
+
+void WzExcel::close()
+{
+    qDebug()<<"WzExcel: close()";
+
+    release();
+}
+
+bool WzExcel::setVisible(bool visible)
+{
+    qDebug()<<"WzExcel: setVisible(bool visible)";
+
+    if(!isOpened)
+    {
+        qDebug()<<"设置visible失败,文件没有打开,请先调用open函数";
+        return false;
+    }
+    else
+    {
+        excel->dynamicCall("SetVisible(bool)", visible);
+        return true;
+    }
+}
+
+bool WzExcel::setCurrentWorkSheet(const QString &sheetName)
+{
+    qDebug()<<"WzExcel: setCurrentWorkSheet(const QString &sheetName)";
+
+    if(!isOpened)
+    {
+        qDebug()<<"设置当前工作表失败,文件没有打开,请先调用open函数";
+        return false;
+    }
+
+    if(sheetName.isNull())
+    {
+        workSheet = workSheets->querySubObject("Item(int)",1); //获得第一张工作表对象
+    }
+    else
+    {
+        int count = workSheets->property("Count").toInt();
+        for(int i=1;i<=count;i++)
+        {
+            if(workSheets->querySubObject("Item(int)",i)->property("Name").toString() == sheetName)
+            {
+                workSheet = workSheets->querySubObject("Item(int)",i); //找到则设为指定名字的表对象
+                break;
+            }
+            if(i==count)
+            {
+                workSheet = workSheets->querySubObject("Item(int)",1); //如果找不到则设为第一张表
+            }
+        }
+    }
+    qDebug()<<workSheet->property("Name").toString();
+	QAxObject* usedrange = workSheet->querySubObject("UsedRange");
+	startRow = usedrange->property("Row").toInt();
+	startColumn = usedrange->property("Column").toInt();
+    return true;
+}
+
+bool WzExcel::createWorkSheet(const QString &sheetName)
+{
+    qDebug()<<"WzExcel: createWorkSheet(const QString &sheetName)";
+
+    if(!isOpened)
+    {
+        qDebug()<<"创建工作表失败,文件没有打开,请先调用open函数";
+        return false;
+    }
+
+    int count = workSheets->property("Count").toInt();
+    for(int i=1;i<=count;i++)
+    {
+        if(workSheets->querySubObject("Item(int)",i)->property("Name").toString() == sheetName)
+        {
+            qDebug()<<"该表已存在!";
+            return false;
+        }
+    }
+    QAxObject *lastSheet = workSheets->querySubObject("Item(int)", count);
+    workSheets->querySubObject("Add(QVariant)", lastSheet->asVariant());
+    QAxObject *newSheet = workSheets->querySubObject("Item(int)", count);
+    lastSheet->dynamicCall("Move(QVariant)", newSheet->asVariant());
+    newSheet->setProperty("Name", sheetName);
+    return true;
+}
+
+bool WzExcel::deleteWorkSheet(const QString &sheetName)
+{
+    qDebug()<<"WzExcel: deleteWorkSheet(const QString &sheetName)";
+
+    if(!isOpened)
+    {
+        qDebug()<<"删除工作表失败,文件没有打开,请先调用open函数";
+        return false;
+    }
+
+    int count = workSheets->property("Count").toInt();
+    for(int i=1;i<=count;i++)
+    {
+        if(workSheets->querySubObject("Item(int)",i)->property("Name").toString() == sheetName)
+        {
+            workSheets->querySubObject("Item(int)",i)->dynamicCall("delete");
+            break;
+        }
+    }
+    return true;
+}
+
+QVariant WzExcel::getValue(const int &row, const int &column)
+{
+    qDebug()<<"WzExcel: getValue(const int &row, const int &column)";
+
+    if(!isOpened)
+    {
+        qDebug()<<"获得指定位置的单元格内容失败,文件没有打开,请先调用open函数";
+        return "";
+    }
+
+    if(workSheet == NULL)
+    {
+        qDebug()<<"获得指定位置的单元格内容失败,workSheet对象为空,请先调用setCurrentWorkSheet函数";
+        return "";
+    }
+
+    data = workSheet->querySubObject("Cells(int,int)", startRow + row, startColumn + column);
+    return data->dynamicCall("Value()");
+}
+
+bool WzExcel::insertValue(const int &row, const int &column, const QString &value)
+{
+    qDebug()<<"WzExcel: insertValue(const int &row, const int &column, const QString &value)";
+
+    if(!isOpened)
+    {
+        qDebug()<<"插入指定位置的单元格内容失败,文件没有打开,请先调用open函数";
+        return false;
+    }
+
+    if(workSheet == NULL)
+    {
+        qDebug()<<"插入指定位置的单元格内容失败,workSheet对象为空,请先调用setCurrentWorkSheet函数";
+        return false;
+    }
+
+    data = workSheet->querySubObject("Cells(int,int)", row, column);
+    data->dynamicCall("Value", value);
+    return true;
+}
+
+bool WzExcel::save()
+{
+    qDebug()<<"WzExcel: save()";
+
+    if(!isOpened)
+    {
+        qDebug()<<"保存失败,文件没有打开,请先调用open函数";
+        return false;
+    }
+
+    QFile file(fileName);
+    if (file.exists())
+    {
+        //文件存在则保存
+        workBook->dynamicCall("Save()");
+    }
+    else
+    {
+        //文件不存在则另存为
+        this->saveAs(fileName);
+    }
+    return true;
+}
+
+bool WzExcel::saveAs(const QString &fileName)
+{
+    qDebug()<<"WzExcel: saveAs(const QString &fileName)";
+
+    if(!isOpened)
+    {
+        qDebug()<<"另存为失败,文件没有打开,请先调用open函数";
+        return false;
+    }
+
+    workBook->dynamicCall("SaveAs(const QString &)",
+                          QDir::toNativeSeparators(fileName));
+    return true;
+}
+
+int WzExcel::getRows()
+{
+	QAxObject* rowObj = getCurrentUserRange()->querySubObject("Rows");
+	return rowObj->property("Count").toInt();
+}
+
+int WzExcel::getColumns()
+{
+	QAxObject* columnObj = getCurrentUserRange()->querySubObject("Columns");
+	return columnObj->property("Count").toInt();
+}
+
+void WzExcel::release()
+{
+    isOpened = false;
+    if(data != NULL)
+    {
+        delete data;
+        data = NULL;
+    }
+    if(workSheet != NULL)
+    {
+        delete workSheet;
+        workSheet = NULL;
+    }
+    if(workSheets != NULL)
+    {
+        delete workSheets;
+        workSheets = NULL;
+    }
+
+    if(workBook != NULL)
+    {
+        workBook->dynamicCall("Close()");
+        delete workBook;
+        workBook = NULL;
+    }
+    if(workBooks != NULL)
+    {
+        workBooks->dynamicCall("Close()");
+        delete workBooks;
+        workBooks = NULL;
+    }
+    if (excel != NULL)
+    {
+        excel->dynamicCall("Quit()");
+        delete excel;
+        excel = NULL;
+    }
+}
+
+QAxObject* WzExcel::getCurrentUserRange()
+{
+	return workSheet->querySubObject("UsedRange");
+}

+ 103 - 0
DataManage/utils/WzExcel.h

@@ -0,0 +1,103 @@
+
+#ifndef _WZEXCEL_H
+#define _WZEXCEL_H
+
+#include <QAxObject>
+#include <QString>
+#include <QDir>
+#include <QFile>
+#include <QDebug>
+#include <windows.h>
+
+/*
+ *  类:WzExcel
+ *  作用:Qt操作Excel
+ *  作者:欧阳伟
+ *  日期:2017-12-19
+ *  用法示例(需在*.pro文件中添加:QT += axcontainer)
+ *      WzExcel w("D:/hello.xlsx");                            //创建对象
+ *      if(w.open())                                           //打开
+ *      {
+ *          //w.setCurrentWorkSheet();                           //设置当前工作表
+ *          for(int i=0;i<10;i++)
+ *          {
+ *              for(int 0=1;j<10;j++)
+ *              {
+ *                  w.insertValue(i,j,QString::number(i*j));   //修改内容
+ *                  //w.getValue(i,j,QString::number(i*j));   //修改内容
+ *              }
+ *          }
+ *          w.save();                                          //保存
+ *          w.saveAs("D:/hello1.xlsx");                        //另存为
+ *      }
+ *
+ *  说明:创建对象需传入绝对路径。
+ */
+
+class WzExcel
+{
+public:
+    WzExcel();
+    //传入需要操作的Excel文件的【绝对路径】
+    WzExcel(const QString &fileName);
+    ~WzExcel();
+
+    //设置文件名
+    void setFileName(const QString fileName);
+
+    //打开,不存在则【新建一个工作簿(保存时以传入时的文件名保存)】,成功返回true,失败返回false
+    void open(bool visible=false,bool displayAlerts=false);
+
+    //关闭
+    void close();
+
+    //设置visible,true: 可视,false: 隐藏
+    bool setVisible(bool visible);
+
+    //设置当前工作表,成功返回true,失败返回false
+    bool setCurrentWorkSheet(const QString &sheetName=NULL);
+
+    //创建工作表,成功返回true,失败返回false
+    bool createWorkSheet(const QString &sheetName);
+
+    //删除工作表,成功返回true,失败返回false
+    bool deleteWorkSheet(const QString &sheetName);
+
+    //获得指定位置的单元格内容,成功返回该值,失败返回NULL
+    QVariant getValue(const int &row,const int &column);
+
+    //插入指定位置的单元格内容,成功返回true,失败返回false
+    bool insertValue(const int &row,const int &colum,const QString &value);
+
+    //保存,成功返回true,失败返回false
+    bool save();
+
+    //另存为,成功返回true,失败返回false
+    bool saveAs(const QString &fileName);
+
+	int getRows();
+
+	int getColumns();
+
+private:
+    void release();
+
+    QAxObject* getCurrentUserRange();
+	
+private:
+    QString fileName; //文件名
+
+    bool isOpened;
+
+    QAxObject *excel; //excel对象
+    QAxObject *workBooks; //所有工作簿对象
+    QAxObject *workBook; //当前工作簿对象
+    QAxObject *workSheets; //所有工作表对象
+    QAxObject *workSheet; //当前工作表对象
+    QAxObject *data; //当前数据对象
+ 
+    int startRow, startColumn;
+};
+
+
+#endif

+ 0 - 85
DataManage/utils/excelinport.cpp

@@ -1,85 +0,0 @@
-#include "excelinport.h"
-#include "DebugUtil.h"
-#include "MessageException.h"
-#include "charcode.h"
-
-#include <QMessageBox>
-
-ExcelInport::ExcelInport(QObject *parent)
-	: QObject(parent)
-	, excel(nullptr)
-	, workbook(nullptr)
-	, currentWorkSheet(nullptr)
-	, startRow(-1)
-	, startColumn(-1)
-{
-}
-
-void ExcelInport::openExcel(const QString &filename)
-{
-	excel = new QAxObject("Excel.Application", this);
-    if(excel == nullptr)
-		throw MessageException("请安装office");
-
-    excel->dynamicCall("SetVisible(bool)",false);
-	QAxObject *workbooks = excel->querySubObject("Workbooks");
-	if (!workbooks)
-		throw MessageException("Excel打开失败");
-	workbook = workbooks->querySubObject("Open(QString,QVariant,QVariant)", filename, 3, true);
-	if (!workbook)
-		throw MessageException("Excel打开失败");
-	currentWorkSheet = workbook->querySubObject("WorkSheets(int)", 1);
-	if (!currentWorkSheet)
-		throw MessageException("打开Excel工作薄失败");
-	QAxObject *usedrange = currentWorkSheet->querySubObject("UsedRange");
-	startRow = usedrange->property("Row").toInt();
-	startColumn = usedrange->property("Column").toInt();
-}
-
-QAxObject *ExcelInport::getCurrentUserRange()
-{
-	return currentWorkSheet->querySubObject("UsedRange");
-}
-
-
-int ExcelInport::getRows()
-{
-	QAxObject *rowObj = getCurrentUserRange()->querySubObject("Rows");
-	return rowObj->property("Count").toInt();
-}
-
-int ExcelInport::getColumns()
-{
-	QAxObject *columnObj = getCurrentUserRange()->querySubObject("Columns");
-	return columnObj->property("Count").toInt();
-}
-
-QVariant ExcelInport::getValue(int row, int column)
-{
-	QAxObject *cell = currentWorkSheet->querySubObject("Cells(int,int)", row + startRow, column + startColumn );
-	if (cell)
-		return cell->property("Value");
-	return QVariant();
-}
-
-void ExcelInport::closeExcel()
-{
-	if (workbook != nullptr)
-	{
-		workbook->dynamicCall("Close(Boolean)", false);
-		workbook = nullptr;
-	}
-
-	if (excel != nullptr)
-	{
-		excel->dynamicCall("Quit()");
-		delete excel;
-		excel = nullptr;
-		currentWorkSheet = nullptr;
-		startRow = -1;
-		startColumn = -1;
-	}
-}
-
-
-

+ 0 - 34
DataManage/utils/excelinport.h

@@ -1,34 +0,0 @@
-#ifndef EXCELINPORT_H
-#define EXCELINPORT_H
-
-#include <QObject>
-#include <QAxObject>
-
-class ExcelInport : public QObject
-{
-    Q_OBJECT
-public:
-    explicit ExcelInport(QObject *parent = nullptr);
-
-    void openExcel(const QString &filename);
-
-    void closeExcel();
-
-	int getRows();
-
-	int getColumns();
-
-	QVariant getValue(int row, int column);
-
-private:
-	QAxObject *getCurrentUserRange();
-
-private:
-    QAxObject *excel;
-    QAxObject *workbook;
-	QAxObject *currentWorkSheet;
-
-	int startRow, startColumn;
-};
-
-#endif // EXCELINPORT_H

+ 2 - 2
DataManage/widgets/TitleBar.cpp

@@ -1,4 +1,4 @@
-#include "TitleBar.h"
+#include "TitleBar.h"
 
 #include <QMenu>
 #include <QHBoxLayout>
@@ -33,7 +33,7 @@ TitleBar::TitleBar(QWidget *parent, bool showUserInfo)
 		m_pUserButton = new QToolButton(this);
 
 		QMenu *menu = new QMenu(this);
-		menu->addAction(QIcon(":/MainWindow/Resources/changePassword.png"), "修改密码", parent, SLOT(onChangePassword()));
+        menu->addAction(QIcon(":/MainWindow/Resources/changePassword.png"), "修改密码", parent, SLOT(onChangePassword()));
 		menu->addAction(QIcon(":/MainWindow/Resources/logout.png"), "退出登录", parent, SLOT(onLogout()));
 		menu->setStyleSheet("\
 			QMenu {background-color:#272727;border: 1px solid #333333;} \