头文件:
ifndef MYEXCEL_H
define MYEXCEL_H
include
include
class MyExcel : public QObject
{
Q_OBJECT
public:
explicit MyExcel(QObject *parent = 0);
~MyExcel();
bool addWorkBook(const QString &filename);
void openWorkBook(const QString &filename);
int getWorkBooksCount();
void setWorkBook(const int &index);
int getWorkSheetsCount();
void addWorkSheet(const QString &sheetName);
void setWorkSheetName(const int &index, const QString &name);
void setWorkSheet(const int &index);
void setValue(const int &row, const int &column, const QVariant &variant);
QVariant getValue(const int &row, const int &column);
void closeWorkBook();
signals:
void errors(const QString &);
public slots:
private:
void initData();
void initApplication();
void initMyExcel();
QAxObject *selectWorkBook(const int &index);
bool checkWorkBookExist(const int &index);
QAxObject *selectWorkSheet(const int &index);
bool checkWorkSheetExist(const int &index);
void saveWorkBook();
private:
QAxObject *application;
QAxObject *workBooks;
QAxObject *workBook;
QAxObject *workSheets;
QAxObject *workSheet;
};
endif // MYEXCEL_H
源文件:
include “myexcel.h”
include
include
MyExcel::MyExcel(QObject *parent) : QObject(parent)
{
initMyExcel();
}
MyExcel::~MyExcel()
{
closeWorkBook();
application->dynamicCall(“Quit(void)”);
}
void MyExcel::initMyExcel()
{
initData();
initApplication();
}
void MyExcel::initData()
{
application = NULL;
workBooks = NULL;
workBook = NULL;
workSheets = NULL;
workSheet = NULL;
}
void MyExcel::initApplication()
{
application = new QAxObject(“ket.Application”);
application->setProperty(“Visible”, true);
application->setProperty(“Caption”, “DataToExcel”);
workBooks = application->querySubObject(“WorkBooks”);
}
bool MyExcel::addWorkBook(const QString &fileName)
{
Q_ASSERT(workBooks);
// QString temp;
// if (fileName.contains(“.xlsx”)) {
// temp = fileName;
// }
// else {
// temp = QString(“%1.xlsx”).arg(fileName);
// }
// if (QFileInfo::exists(temp)) {
// emit errors(“The %1 has already exists.”);
// return false;
// }
saveWorkBook();
workBooks->dynamicCall("Add");
workBook = application->querySubObject("ActiveWorkBook");
workSheets = workBook->querySubObject("WorkSheets");
workSheet = workBook->querySubObject("WorkSheets(int)", 1);
workBook->dynamicCall("SaveAs(const QString&)",
QString("%1").arg(QString(fileName).replace("/", "\\")));
return true;
}
void MyExcel::openWorkBook(const QString &fileName)
{
saveWorkBook();
workBooks->dynamicCall(“Open(const QString&)”, QString(fileName).replace(“/”, “\”));
workBook = application->querySubObject(“ActiveWorkBook”);
workSheets = workBook->querySubObject(“WorkSheets”);
workSheet = workBook->querySubObject(“WorkSheets(int)”, 1);
}
int MyExcel::getWorkBooksCount()
{
return workBooks->property(“Count”).toInt();
}
bool MyExcel::checkWorkBookExist(const int &index)
{
if (index && (index<=getWorkBooksCount())) {
return true;
}
emit errors(QString(“WorkBook does not exist.”));
return false;
}
QAxObject *MyExcel::selectWorkBook(const int &index)
{
if (checkWorkBookExist(index)) {
return application->querySubObject(“WorkBooks(int)”, index);
}
return NULL;
}
void MyExcel::setWorkBook(const int &index)
{
workBook = selectWorkBook(index);
}
void MyExcel::addWorkSheet(const QString &sheetName)
{
int intCount = getWorkSheetsCount();
QAxObject *pLastSheet = workSheets->querySubObject("Item(int)", intCount);
workSheets->querySubObject("Add(QVariant)", pLastSheet->asVariant());
workSheet = workSheets->querySubObject("Item(int)", intCount);
pLastSheet->dynamicCall("Move(QVariant)", workSheet->asVariant());
workSheet->setProperty("Name", sheetName);
}
void MyExcel::setWorkSheetName(const int &index, const QString &name)
{
QAxObject *workSheet = selectWorkSheet(index);
if(workSheet != NULL){
workSheet->setProperty(“Name”, name);
}
}
void MyExcel::setWorkSheet(const int &index)
{
workSheet = selectWorkSheet(index);
}
QAxObject* MyExcel::selectWorkSheet(const int &index)
{
if (checkWorkSheetExist(index)) {
return workBook->querySubObject(“WorkSheets(int)”, index);
}
return NULL;
}
bool MyExcel::checkWorkSheetExist(const int &index)
{
if (index && (index<=getWorkSheetsCount())) {
return true;
}
emit errors(QString(“Worksheet does not exist.”));
return false;
}
int MyExcel::getWorkSheetsCount()
{
Q_ASSERT(workSheets);
return workSheets->property(“Count”).toInt();
}
void MyExcel::setValue(const int &row, const int &column, const QVariant &variant)
{
Q_ASSERT(workSheet && row && column);
QAxObject *range = workSheet->querySubObject(“Cells(int,int)”, row, column);
range->dynamicCall(“SetValue(const QVariant&)”, variant);
}
QVariant MyExcel::getValue(const int &row, const int &column)
{
Q_ASSERT(workSheet);
QAxObject *range = workSheet->querySubObject(“Cells(int,int)”, row, column);
return range->property(“Value”);
}
void MyExcel::saveWorkBook()
{
if (workBook != NULL) {
workBook->dynamicCall(“Save(void)”);
}
}
void MyExcel::closeWorkBook()
{
for (int i=1; i<=getWorkBooksCount(); i++)
{
workBook = selectWorkBook(i);
saveWorkBook();
workBook->dynamicCall(“Close(Boolean)”, false);
}
}