• 大小: 13KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-05-28
  • 语言: 其他
  • 标签: qt  office  

资源简介

qt操作office文件的示例程序 基本上可以拿来直接使用,简单修改一下就可以了。

资源截图

代码片段和文件信息

#include “excel.h“

#if defined(Q_OS_WIN32)
Excel::Excel(QString strXlsFile Qobject *parent)
{
    excelApplication = 0;
    excelWorkBooks = 0;
    excelWorkBook = 0;
    excelSheets = 0;
    excelSheet = 0;
    fileName = strXlsFile;
    columnName = “ A“;

    try {
        excelApplication = new QAxobject(“Excel.Application“ parent);

        if (excelApplication) {
            excelApplication->setProperty(“DisplayAlerts“ false);//不显示任何警告信息。
            excelWorkBooks = excelApplication->querySubobject(“Workbooks“);
            if (excelWorkBooks) {
                QFile file(strXlsFile);
                if (file.exists())
                    excelWorkBook = excelWorkBooks->querySubobject(“Open(const QString&)“ strXlsFile);
            } else
                QMessageBox::information(0 ““ “初始化Excel错误可能没有安装Office组件!“);
        } else
            QMessageBox::information(0 ““ “初始化Excel错误可能没有安装Office组件!“);
    } catch (...) {}
}

Excel::~Excel()
{
    closeAll();

}

QAxobject *Excel::setFileName(const QString& strXlsFile)
{
    fileName = strXlsFile;
    if (excelWorkBooks) {
        delete excelWorkBooks;
        excelWorkBooks = excelApplication->querySubobject(“Workbooks“);
    }

    if (excelWorkBooks) {
        QFile file(fileName);
        if (file.exists())
            excelWorkBook = excelWorkBooks->querySubobject(“Open(const QString&)“ fileName);
        else {
            excelWorkBook = excelWorkBooks->querySubobject(“Add()“);
        }

        if (excelWorkBook)
            excelSheets = excelWorkBook->querySubobject(“Sheets“);
        else
            QMessageBox::information(0 ““ “QAxobject workbook fail!“);
    }
    return excelWorkBook;
}

void Excel::closeAll()
{
    if (excelApplication) {
        try {
            excelApplication->dynamicCall(“ Quit()“);
            delete excelSheet;
            delete excelSheets;
            delete excelWorkBook;
            delete excelWorkBooks;
            delete excelApplication;
            excelApplication = 0;
            excelWorkBooks = 0;
            excelWorkBook = 0;
            excelSheets = 0;
            excelSheet = 0;
        } catch (...) {}
    }
}

QAxobject *Excel::getWorkBooks()
{
    return excelWorkBooks;
}

QAxobject *Excel::getWorkBook()
{
    return excelWorkBook;

}

QAxobject *Excel::getWorkSheets()
{
    return excelSheets;
}

QAxobject *Excel::getWorkSheet()
{
    return excelSheet;

}
bool Excel::lockUpdate()
{
    try {
        if (!excelApplication)
            return false;
        excelApplication->dynamicCall(“ SetScreenUpdating(bool)“ false);
    } catch (...) {}

    return true;
}

bool Excel::unlockUpdate()
{
    try {
        if (!excelApplication)
            return false;
        excelApplication->dynamicCall(“ SetScreenUpdating(bool)“ true);
    } catch (...) {}
    return true;
}

QAxobject *Excel::selectSh

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件      17495  2014-06-20 16:36  qt_office\excel.cpp

     文件       2451  2014-06-20 16:36  qt_office\excel.h

     文件      13824  2014-07-10 17:10  qt_office\Excel.xls

     文件        483  2014-07-11 10:40  qt_office\main.cpp

     文件       1745  2014-07-11 10:44  qt_office\mainwindow.cpp

     文件        422  2014-07-11 10:26  qt_office\mainwindow.h

     文件       2799  2014-07-11 10:33  qt_office\mainwindow.ui

     文件        468  2014-07-11 10:27  qt_office\qt_office.pro

     文件      17198  2014-07-11 10:46  qt_office\qt_office.pro.user

     文件       1725  2014-07-10 09:41  qt_office\word.cpp

     文件      19968  2014-07-10 16:57  qt_office\Word.doc

     文件        450  2014-07-10 09:42  qt_office\word.h

     目录          0  2014-07-11 10:46  qt_office

----------- ---------  ---------- -----  ----

                79028                    13


评论

共有 条评论