QT:读excel文件的 *** 作

QT:读excel文件的 *** 作,第1张

QT:读excel文件 *** 作

效果图

 直接看代码:

excelbash.h  excelbash.cpp

#ifndef EXCELbase_H
#define EXCELbase_H

#include 
#include 
#include 
#include 
#include 
#include 

class excelbase : public QObject
{
public:
    excelbase(QString xlsFilePath, QObject *parent = NULL);
    ~excelbase();

    void save();
    void close();
    int sheetCount();
    QString getSheetName(int sheetIndex);
    QAxObject *getWorkBooks();
    QAxObject *getWorkBook();
    QAxObject *getWorkSheets();
    QAxObject *getWorkSheet();
    // 返回当前 Sheet.
    QVariant read(int index, int row, int col);
    void readAll(int index);

private:
    QAxObject * excel;
    QAxObject * workBooks;
    QAxObject * workBook;
    QAxObject * sheets;
    QAxObject * sheet;
};

#endif // EXCELbase_H
#include "excelbase.h"
#include 
#include 

excelbase::excelbase(QString xlsFilePath, QObject *parent)
{
    excel = NULL;
    workBooks = NULL;
    workBook = NULL;
    sheets = NULL;
    sheet = NULL;
    excel = new QAxObject("Excel.Application", parent);
    workBooks = excel->querySubObject("Workbooks");
    QFile file(xlsFilePath);
    if(file.exists())
    {
        workBooks->dynamicCall("Open(const QString&)", xlsFilePath);
        workBook = excel->querySubObject("ActiveWorkBook");
        sheets = workBook->querySubObject("WorkSheets");
        sheet = workBook->querySubObject("WorkSheets(int)", 1);
    }
}

excelbase::~excelbase()
{
    close();
}

int excelbase::sheetCount()
{
    return sheets->property("Count").toInt();
}

QString excelbase::getSheetName(int sheetIndex)
{
    sheet = workBook->querySubObject("WorkSheets(int)", sheetIndex);
    return sheet->property("Name").toString();
}

QAxObject *excelbase::getWorkBook()
{
    return workBook;
}

void excelbase::save()
{
    workBook->dynamicCall("Save()");
}

void excelbase::close()
{
    excel->dynamicCall("Quit()");
    delete sheet;
    delete sheets;
    delete workBook;
    delete workBooks;
    delete excel;
    excel = NULL;
    workBooks = NULL;
    workBook = NULL;
    sheets = NULL;
    sheet = NULL;
}

void excelbase::readAll(int index)
{
    sheet = workBook->querySubObject("WorkSheets(int)", index);
    if(sheet->isNull())
        return;
    QAxObject* range = sheet->querySubObject("UsedRange");//获取该sheet的使用范围对象
    QVariant var = range->dynamicCall("Value");
    delete range;
    QVariantList varRows = var.toList();
    QString str1, str2, str3, str4;
    if(varRows.isEmpty())
        return;
    const int rowCount = varRows.size();
    for(int i = 1; i < rowCount;++i)
    {
        QVariantList rowData = varRows[i].toList();
        str1 = rowData[0].toString();
        str2 = rowData[1].toString();
        str3 = rowData[2].toString();
        str4 = rowData[3].toString();
        qDebug() << str1 << str2 << str3 << str4;
    }
}

widget.h  widget.cpp

#ifndef WIDGET_H
#define WIDGET_H

#include 
#include "excelbase.h"

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();

private:
    Ui::Widget *ui;
    excelbase *excel;
};

#endif // WIDGET_H
#include "widget.h"
#include "ui_widget.h" 
#include 
#include 

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    QString strFilePathName = QFileDialog::getOpenFileName(this,QStringLiteral("选择Excel文件"),"",tr("Exel file(*.xls *.xlsx)"));
    if(strFilePathName.isNull())
        return;
    excel = new excelbase(strFilePathName);
    if(excel == NULL)
        return;
    int worksheetCount = excel->sheetCount();
    qDebug() << excel->sheetCount();
    for(int i = 1; i <= worksheetCount; i++)
    {
        if(excel->getSheetName(i) == "飞机系统原理")
            excel->readAll(i);
        if(excel->getSheetName(i) == " *** 作使用")
        {
            excel->readAll(i);
        }
    }

    if(strFilePathName.isNull())
        return;
    QString dir = QFileDialog::getSaveFileName(this, tr("Open File"),"/demo",tr("Text File(*.xlsx *.xls)"));
    excel->getWorkBook()->dynamicCall("SaveAs(const QString&)",QDir::tonativeSeparators(dir));
}

Widget::~Widget()
{
    delete ui;
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5503599.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-13
下一篇 2022-12-13

发表评论

登录后才能评论

评论列表(0条)

保存