头文件:
01 | #ifndef TEXCELREADER_H |
02 | #define TEXCELREADER_H |
03 | |
04 | #include <QObject> |
05 | #include <QAxObject> |
06 | #include <QList> |
07 | |
08 | class TExcelReader; |
09 | class TExcelSheet |
10 | { |
11 | public : |
12 | TExcelSheet(QAxObject* sheet) |
13 | { |
14 | Clear(); |
15 | |
16 | if (0 == sheet) |
17 | return ; |
18 | QAxObject * usedrange = sheet->querySubObject( "UsedRange" ); |
19 | if (0 == usedrange) |
20 | { |
21 | Clear(); |
22 | return ; |
23 | } |
24 | QAxObject * rows = usedrange->querySubObject( "Rows" ); |
25 | if (0 == rows) |
26 | { |
27 | Clear(); |
28 | return ; |
29 | } |
30 | QAxObject * columns = usedrange->querySubObject( "Columns" ); |
31 | if (0 == columns) |
32 | { |
33 | Clear(); |
34 | return ; |
35 | } |
36 | |
37 | mStartRow = usedrange->property( "Row" ).toInt(); |
38 | mStartCol = usedrange->property( "Column" ).toInt(); |
39 | mColCount = columns->property( "Count" ).toInt(); |
40 | mRowCount = rows->property( "Count" ).toInt(); |
41 | } |
42 | |
43 | virtual ~TExcelSheet() |
44 | { |
45 | } |
46 | |
47 | private : |
48 | void Clear() |
49 | { |
50 | mStartRow = mStartCol = mRowCount = mColCount = 0; |
51 | } |
52 | |
53 | private : |
54 | int mStartRow; |
55 | int mStartCol; |
56 | int mRowCount; |
57 | int mColCount; |
58 | |
59 | friend class TExcelReader; |
60 | }; |
61 | |
62 | |
63 | class TExcelReader : public QObject |
64 | { |
65 | Q_OBJECT |
66 | |
67 | public : |
68 | TExcelReader(QString excelName, QObject *parent = 0); |
69 | virtual ~TExcelReader(); |
70 | |
71 | int GetExcelSheetCount() const ; |
72 | |
73 | int GetSheetUseColStart( int sheetIndex) const ; |
74 | int GetSheetUseColCount( int sheetIndex) const ; |
75 | int GetSheetUseRowStart( int sheetIndex) const ; |
76 | int GetSheetUseRowCount( int sheetIndex) const ; |
77 | |
78 | QString GetData( int sheetIndex, int row, int col); |
79 | |
80 | private : |
81 | void Clear(); |
82 | |
83 | private : |
84 | QList<TExcelSheet> mListSheet; |
85 | |
86 | QAxObject* mExcelObj; |
87 | QAxObject* mWorkBooks; |
88 | QAxObject* mWorkBook; |
89 | QAxObject* mSheets; |
90 | }; |
91 | |
92 | #endif // TEXCELREADER_H |
实现文件:
001 | #include "TExcelReader.h" |
002 | |
003 | TExcelReader::TExcelReader(QString excelName, QObject *parent) |
004 | : QObject(parent) |
005 | , mExcelObj(0) |
006 | , mWorkBooks(0) |
007 | , mWorkBook(0) |
008 | , mSheets(0) |
009 | { |
010 | mExcelObj = new QAxObject( "Excel.Application" ); |
011 | mWorkBooks = mExcelObj->querySubObject( "Workbooks" ); //得到Workbooks集合的指针 |
012 | if (0 == mWorkBooks) |
013 | return ; |
014 | mWorkBook = mWorkBooks->querySubObject( "Open(const QString&)" , excelName); //open xls |
015 | if (0 == mWorkBook) |
016 | return ; |
017 | mSheets = mWorkBook->querySubObject( "Sheets" ); //得到Sheets对象的指针 |
018 | if (0 == mSheets) |
019 | return ; |
020 | |
021 | int iSheetCount = mSheets->property( "Count" ).toInt(); |
022 | for ( int index=1; index<=iSheetCount; ++index) |
023 | { |
024 | QAxObject* sheetObj = mWorkBook->querySubObject( "Worksheets(int)" , index); |
025 | if (0 == sheetObj) |
026 | { |
027 | Clear(); |
028 | return ; |
029 | } |
030 | |
031 | mListSheet.append(TExcelSheet(sheetObj)); |
032 | } |
033 | } |
034 | |
035 | TExcelReader::~TExcelReader() |
036 | { |
037 | Clear(); |
038 | } |
039 | |
040 | void TExcelReader::Clear() |
041 | { |
042 | mListSheet.clear(); |
043 | |
044 | mWorkBook->dynamicCall( "Close (Boolean)" , false ); |
045 | mExcelObj->dynamicCall( "Quit(void)" ); |
046 | |
047 | mSheets = 0; |
048 | mWorkBook = 0; |
049 | mWorkBooks = 0; |
050 | |
051 | delete mExcelObj; |
052 | mExcelObj = 0; |
053 | } |
054 | |
055 | int TExcelReader::GetExcelSheetCount() const |
056 | { |
057 | return mListSheet.size(); |
058 | } |
059 | |
060 | int TExcelReader::GetSheetUseColStart( int sheetIndex) const |
061 | { |
062 | if (sheetIndex < 1 || sheetIndex > mListSheet.size()) |
063 | return -1; |
064 | |
065 | return mListSheet[sheetIndex-1].mStartCol; |
066 | } |
067 | |
068 | int TExcelReader::GetSheetUseColCount( int sheetIndex) const |
069 | { |
070 | if (sheetIndex < 1 || sheetIndex > mListSheet.size()) |
071 | return -1; |
072 | |
073 | return mListSheet[sheetIndex-1].mColCount; |
074 | } |
075 | |
076 | int TExcelReader::GetSheetUseRowStart( int sheetIndex) const |
077 | { |
078 | if (sheetIndex < 1 || sheetIndex > mListSheet.size()) |
079 | return -1; |
080 | |
081 | return mListSheet[sheetIndex-1].mStartRow; |
082 | } |
083 | |
084 | int TExcelReader::GetSheetUseRowCount( int sheetIndex) const |
085 | { |
086 | if (sheetIndex < 1 || sheetIndex > mListSheet.size()) |
087 | return -1; |
088 | |
089 | return mListSheet[sheetIndex-1].mRowCount; |
090 | } |
091 | |
092 | QString TExcelReader::GetData( int sheetIndex, int row, int col) |
093 | { |
094 | if (sheetIndex < 1 || sheetIndex > mListSheet.size()) |
095 | return QString(); |
096 | TExcelSheet sh = mListSheet[sheetIndex-1]; |
097 | if (row < sh.mStartRow || row >= sh.mStartRow + sh.mRowCount) |
098 | return QString(); |
099 | if (col < sh.mStartCol || col >= sh.mStartCol + sh.mColCount) |
100 | return QString(); |
101 | |
102 | QAxObject* sheetObj = mWorkBook->querySubObject( "Worksheets(int)" , sheetIndex); |
103 | if (0 == sheetObj) |
104 | return QString(); |
105 | QAxObject* range = sheetObj->querySubObject( "Cells(int,int)" , row, col); |
106 | if (0 == range) |
107 | return QString(); |
108 | |
109 | QVariant var = range->property( "Value" ); |
110 | if (var.type() == QVariant::Invalid) |
111 | return QString(); |
112 | |
113 | return var.toString(); |
114 | } |