00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include <qstring.h>
00031 #include <qarray.h>
00032 #include <qlist.h>
00033
00034
00035 #include <stdio.h>
00036 #include <stdlib.h>
00037 #include <math.h>
00038 #include <time.h>
00039 #include <sys/types.h>
00040 #include <strings.h>
00041
00042 #define DATEFORMAT 0x1
00043 #define NUMBERFORMAT 0x2
00044
00045 #define BIFF8 0x600
00046 #define BIFF7 0x500
00047 #define WBKGLOBAL 0x5
00048 #define WRKSHEET 0x10
00049
00050 #define XL_ARRAY 0x221
00051 #define XL_BOUNDSHEET 0x85
00052 #define XL_BOF 0x809
00053 #define XL_BOOLERR 0x205
00054 #define XL_CONTINUE 0x3c
00055 #define XL_DIMENSION 0x200
00056 #define XL_EOF 0x0a
00057 #define XL_EXTSST 0xff
00058 #define XL_FORMULA 0x406
00059 #define XL_FORMULA2 0x6
00060 #define XL_FORMAT 0x41e
00061 #define XL_INDEX 0x20b
00062 #define XL_LABEL 0x204
00063 #define XL_LABELSST 0xfd
00064 #define XL_MULRK 0xbd
00065 #define XL_NAME 0x18
00066 #define XL_NOTE 0x1c
00067 #define XL_NUMBER 0x203
00068 #define XL_RK 0x7e
00069 #define XL_RK2 0x27e
00070 #define XL_ROW 0x208
00071 #define XL_SST 0xfc
00072 #define XL_STRING 0x207
00073 #define XL_TXO 0x1b6
00074 #define XL_XF 0xe0
00075 #define XL_UNKNOWN 0xffff
00076
00077 #define CELL_LABEL 0x2
00078 #define CELL_NUMBER 0x3
00079 #define CELL_DATE 0x4
00080 #define CELL_BOOLEAN 0x5
00081 #define CELL_ERROR 0x6
00082
00083
00084
00085 class ExcelFormat
00086 {
00087 public:
00088 int code;
00089 int type;
00090 QString format;
00091 ExcelFormat();
00092 ExcelFormat(int c,int t, QString s);
00093 };
00094
00095 struct xfrecord
00096 {
00097 int code;
00098 int type;
00099 QString format;
00100 };
00101
00102 class ExcelCell
00103 {
00104 public:
00105 int type;
00106 int row,col;
00107 int xfindex;
00108 int valuei;
00109 double valued;
00110 QString valuec;
00111
00112 };
00113
00114 class ExcelBREC
00115 {
00116 public:
00117 int code;
00118 int length;
00119 int position;
00120 char* data;
00121 };
00122
00123 class SSTList
00124 {
00125 public:
00126 QArray <ExcelBREC*> rec;
00127 };
00128
00129 class ExcelSheet
00130 {
00131 public:
00132 QString name;
00133 ExcelBREC BOFRecord;
00134 int position;
00135 int type;
00136 int rows;
00137 int cols;
00138
00139 int cellsize,rowalloc,cellalloc;
00140 QArray <ExcelCell*> Cells;
00141 bool InitCells(void);
00142 ExcelCell* Get(int row, int col);
00143 void Set(int row, int col, ExcelCell* cell);
00144
00145 };
00146
00147 struct mulrk
00148 {
00149 int row;
00150 int first;
00151 int last;
00152 int numrks;
00153 QArray<int> rknumbers;
00154 QArray<double> rkdbls;
00155 QArray<int> xfindices;
00156 };
00157
00158 class ExcelBook
00159 {
00160 public:
00161 FILE *File;
00162 int Position;
00163
00164 QArray <QString*> SharedStrings;
00165
00166 QArray <ExcelFormat*> XFRecords;
00167
00168 QArray <ExcelSheet*> Sheets;
00169
00170 QArray <QString*> Names;
00171
00172 QString dateformat;
00173 int Version;
00174 int endian;
00175 int Integer2Byte(int b1, int b2 );
00176 int Integer4Byte(int b1, int b2, int b3, int b4 );
00177 int Integer2ByteFile(FILE *f);
00178 float Float4Byte(int b1, int b2, int b3, int b4);
00179 double Double4Byte(int b1, int b2, int b3, int b4);
00180 double Double8Byte(int b1, int b2, int b3, int b4, int b5, int b6, int b7, int b8);
00181 void DetectEndian(void);
00182
00183 bool OpenFile(char *Filename);
00184 bool CloseFile(void);
00185 void SeekPosition(int pos);
00186 void SeekSkip(int pos);
00187 int FileEOF(void);
00188 int Get2Bytes(void);
00189 char* Read(int pos, int length);
00190 QString ReadUnicodeChar(int pos, int length);
00191 QString* GetString(int num);
00192 int SeekBOF(void);
00193 ExcelBREC* GetBREC(void);
00194 ExcelBREC* PeekBREC(void);
00195 char* GetDataOfBREC(ExcelBREC* record);
00196 void ConvertCharToArray(ExcelBREC* record, char* chars, int length);
00197 int SheetHandleRecord(ExcelSheet* sheet, ExcelBREC* record);
00198 int ReadSheet(ExcelSheet* sheet);
00199 ExcelSheet* GetSheet(void);
00200 void ParseSheets(void);
00201 void GetSheets(void);
00202
00203 bool ParseBook(char *file);
00204 QString GetASCII(char* inbytes, int pos, int chars);
00205 QString GetUnicode(char * inbytes, int pos, int chars);
00206 void HandleBoundSheet( ExcelBREC* rec);
00207 void HandleName(ExcelSheet* sheet, ExcelBREC* rec);
00208 ExcelFormat* GetFormatting(int xf);
00209 void HandleSetOfSST(ExcelBREC* rec, char* bytes);
00210 char* MergeBytesFromSSTs(ExcelBREC* rec,SSTList* cont);
00211 void HandleSST(ExcelBREC* rec);
00212 void HandleLabelSST(ExcelSheet* sheet, ExcelBREC* rec);
00213 ExcelCell* CellLabel(int row, int col, QString str);
00214 ExcelCell* CellNumber(int row, int col, int index, double d);
00215 QString* CellDataString(ExcelSheet* sh, int row, int col);
00216 int CellGetPrecision(double d);
00217 void CellSetDateFormat(char *d);
00218 void HandleMulrk(ExcelSheet* sheet, ExcelBREC* record);
00219 void MulrkRead(struct mulrk *mulrk, char* data);
00220 void HandleNumber(ExcelSheet* sheet, ExcelBREC* record);
00221 void HandleFormat(ExcelBREC* rec);
00222 void HandleXF(ExcelBREC* rec);
00223 void HandleRK(ExcelSheet* sheet, ExcelBREC* record);
00224 void HandleFormula(ExcelSheet* sheet, ExcelBREC* record);
00225 QString GetFormula(int row, int col, ExcelSheet* sheet, char* data, int sz);
00226 QString FindCellName(int row, int col);
00227 };
00228