头文件:
01 | #ifndef TEXCELREADER_H |
02 | #define TEXCELREADER_H |
03 | |
04 | #include <QObject> |
05 | #include <QAxObject> |
06 | #include <QList> |
07 | |
08 | classTExcelReader;
|
09 | classTExcelSheet
|
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 | voidClear()
|
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 | friendclassTExcelReader;
|
60 | };
|
61 | |
62 | |
63 | classTExcelReader :
publicQObject
|
64 | {
|
65 | Q_OBJECT |
66 | |
67 | public: |
68 | TExcelReader(QString excelName, QObject *parent = 0); |
69 | virtual~TExcelReader();
|
70 | |
71 | intGetExcelSheetCount()
const;
|
72 | |
73 | intGetSheetUseColStart(intsheetIndex)
const; |
74 | intGetSheetUseColCount(intsheetIndex)
const; |
75 | intGetSheetUseRowStart(intsheetIndex)
const; |
76 | intGetSheetUseRowCount(intsheetIndex)
const; |
77 | |
78 | QString GetData(intsheetIndex,
introw,
intcol);
|
79 | |
80 | private: |
81 | voidClear();
|
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 | intiSheetCount = mSheets->property("Count").toInt(); |
022 | for(intindex=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 | voidTExcelReader::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 | deletemExcelObj;
|
052 | mExcelObj = 0; |
053 | }
|
054 | |
055 | intTExcelReader::GetExcelSheetCount()const |
056 | {
|
057 | returnmListSheet.size();
|
058 | }
|
059 | |
060 | intTExcelReader::GetSheetUseColStart(intsheetIndex)
const |
061 | {
|
062 | if(sheetIndex < 1 || sheetIndex > mListSheet.size()) |
063 | return-1;
|
064 | |
065 | returnmListSheet[sheetIndex-1].mStartCol; |
066 | }
|
067 | |
068 | intTExcelReader::GetSheetUseColCount(intsheetIndex)
const |
069 | {
|
070 | if(sheetIndex < 1 || sheetIndex > mListSheet.size()) |
071 | return-1;
|
072 | |
073 | returnmListSheet[sheetIndex-1].mColCount; |
074 | }
|
075 | |
076 | intTExcelReader::GetSheetUseRowStart(intsheetIndex)
const |
077 | {
|
078 | if(sheetIndex < 1 || sheetIndex > mListSheet.size()) |
079 | return-1;
|
080 | |
081 | returnmListSheet[sheetIndex-1].mStartRow; |
082 | }
|
083 | |
084 | intTExcelReader::GetSheetUseRowCount(intsheetIndex)
const |
085 | {
|
086 | if(sheetIndex < 1 || sheetIndex > mListSheet.size()) |
087 | return-1;
|
088 | |
089 | returnmListSheet[sheetIndex-1].mRowCount; |
090 | }
|
091 | |
092 | QString TExcelReader::GetData(intsheetIndex,
introw,
intcol)
|
093 | {
|
094 | if(sheetIndex < 1 || sheetIndex > mListSheet.size()) |
095 | returnQString();
|
096 | TExcelSheet sh = mListSheet[sheetIndex-1]; |
097 | if(row < sh.mStartRow || row >= sh.mStartRow + sh.mRowCount) |
098 | returnQString();
|
099 | if(col < sh.mStartCol || col >= sh.mStartCol + sh.mColCount) |
100 | returnQString();
|
101 | |
102 | QAxObject* sheetObj = mWorkBook->querySubObject("Worksheets(int)", sheetIndex); |
103 | if(0 == sheetObj) |
104 | returnQString();
|
105 | QAxObject* range = sheetObj->querySubObject("Cells(int,int)", row, col); |
106 | if(0 == range) |
107 | returnQString();
|
108 | |
109 | QVariant var = range->property("Value"); |
110 | if(var.type() == QVariant::Invalid) |
111 | returnQString();
|
112 | |
113 | returnvar.toString();
|
114 | } |
1302

被折叠的 条评论
为什么被折叠?



