Qt读Excel文件

*.h文件:

#ifndef ExcelReader_H
#define ExcelReader_H #include <QObject> #include <QAxObject> #include <QList> #include <QString> class ExcelReader; class ExcelSheet { public: ExcelSheet(QAxObject* sheet) { Clear(); if(0 == sheet) return; QAxObject * usedrange = sheet->querySubObject("UsedRange"); if(0 == usedrange) { Clear(); return; } QAxObject * rows = usedrange->querySubObject("Rows"); if(0 == rows) { Clear(); return; } QAxObject * columns = usedrange->querySubObject("Columns"); if(0 == columns) { Clear(); return; } mStartRow = usedrange->property("Row").toInt(); mStartCol = usedrange->property("Column").toInt(); mColCount = columns->property("Count").toInt(); mRowCount = rows->property("Count").toInt(); } virtual ~ExcelSheet() { } private: void Clear() { mStartRow = mStartCol = mRowCount = mColCount = 0; } private: int mStartRow; int mStartCol; int mRowCount; int mColCount; friend class ExcelReader; }; class ExcelReader : public QObject { Q_OBJECT public: ExcelReader(QString excelName, QObject *parent = 0);//文件名excelName需要带文件绝对路径 virtual ~ExcelReader(); int GetExcelSheetCount() const; int GetSheetUseColStart(int sheetIndex) const; int GetSheetUseColCount(int sheetIndex) const; int GetSheetUseRowStart(int sheetIndex) const; int GetSheetUseRowCount(int sheetIndex) const; QString GetData(int sheetIndex, int row, int col); private: void Clear(); private: QList<ExcelSheet> mListSheet; QAxObject* mExcelObj; QAxObject* mWorkBooks; QAxObject* mWorkBook; QAxObject* mSheets; }; #endif // ExcelReader_H
*.cpp文件

#include "ExcelReader.h" ExcelReader::ExcelReader(QString excelName, QObject *parent) : QObject(parent) , mExcelObj(0) , mWorkBooks(0) , mWorkBook(0) , mSheets(0) { QString filename=excelName; mExcelObj = new QAxObject( "Excel.Application"); mWorkBooks = mExcelObj->querySubObject( "Workbooks" ); //得到Workbooks集合的指针 if(0 == mWorkBooks) return; mWorkBook = mWorkBooks->querySubObject( "Open(const QString&)",filename);//open xls if(0 == mWorkBook) return; mSheets = mWorkBook->querySubObject( "Sheets" ); //得到Sheets对象的指针 if(0 == mSheets) return; int iSheetCount = mSheets->property("Count").toInt(); for(int index=1; index<=iSheetCount; ++index) { QAxObject* sheetObj = mWorkBook->querySubObject("Worksheets(int)", index); if(0 == sheetObj) { Clear(); return; } mListSheet.append(ExcelSheet(sheetObj)); } } ExcelReader::~ExcelReader() { Clear(); } void ExcelReader::Clear() { mListSheet.clear(); mWorkBook->dynamicCall("Close (Boolean)", false); mExcelObj->dynamicCall("Quit(void)"); mSheets = 0; mWorkBook = 0; mWorkBooks = 0; delete mExcelObj; mExcelObj = 0; } int ExcelReader::GetExcelSheetCount() const { return mListSheet.size(); } int ExcelReader::GetSheetUseColStart(int sheetIndex) const { if(sheetIndex < 1 || sheetIndex > mListSheet.size()) return -1; return mListSheet[sheetIndex-1].mStartCol; } int ExcelReader::GetSheetUseColCount(int sheetIndex) const { if(sheetIndex < 1 || sheetIndex > mListSheet.size()) return -1; return mListSheet[sheetIndex-1].mColCount; } int ExcelReader::GetSheetUseRowStart(int sheetIndex) const { if(sheetIndex < 1 || sheetIndex > mListSheet.size()) return -1; return mListSheet[sheetIndex-1].mStartRow; } int ExcelReader::GetSheetUseRowCount(int sheetIndex) const { if(sheetIndex < 1 || sheetIndex > mListSheet.size()) return -1; return mListSheet[sheetIndex-1].mRowCount; } QString ExcelReader::GetData(int sheetIndex, int row, int col) { if(sheetIndex < 1 || sheetIndex > mListSheet.size()) return QString(); ExcelSheet sh = mListSheet[sheetIndex-1]; if(row < sh.mStartRow || row >= sh.mStartRow + sh.mRowCount) return QString(); if(col < sh.mStartCol || col >= sh.mStartCol + sh.mColCount) return QString(); QAxObject* sheetObj = mWorkBook->querySubObject("Worksheets(int)", sheetIndex); if(0 == sheetObj) return QString(); QAxObject* range = sheetObj->querySubObject("Cells(int,int)", row, col); if(0 == range) return QString(); QVariant var = range->property("Value"); if(var.type() == QVariant::Invalid) return QString(); return var.toString(); }
posted @ 2012-06-13 12:50  renhang888  阅读(846)  评论(0编辑  收藏  举报