-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXlsReader.h
170 lines (147 loc) · 5.19 KB
/
XlsReader.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*
* Copyright 2004 Komarov Valery
* Copyright 2006 Christophe Leitienne
* Copyright 2008-2017 David Hoerl
* Copyright 2013 Bob Colbert
* Copyright 2013-2018 Evan Miller
*
* This file is part of libxls -- A multiplatform, C/C++ library for parsing
* Excel(TM) files.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ''AS
* IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include <string>
#include <limits>
#include <stdexcept>
// Inside namespace xls:
#include "include/xls.h"
#ifndef UINT32_MAX
#define UINT32_MAX std::numeric_limits<uint32_t>::max()
#endif
#define XLS_WIDE_STRINGS 0
#if XLS_WIDE_STRINGS == 0
typedef std::string xlsString;
#else
#include <iconv.h>
typedef std::wstring xlsString;
#endif
namespace xls
{
typedef enum { cellBlank=0, cellString, cellInteger, cellFloat, cellBool, cellError, cellUnknown } contentsType;
struct cellContent {
contentsType type;
char colStr[3]; // String "A"..."Z", "AA"..."ZZ" (second char is either nil or a capital letter)
uint32_t col; // 1 based
uint16_t row; // 1 based
xlsString str; // even for numbers these values are formatted as well as provided below
union Val {
long l;
double d;
bool b;
int32_t e;
Val(int x) { l = x; }
~Val() { }
} val;
cellContent(void) :
type(cellBlank),
colStr(),
col(0),
row(0),
val(0) { }
~cellContent() {}
};
class WorkBook
{
public:
#if XLS_WIDE_STRINGS == 0
// characterSet is the 8-bit encoding you want to convert 16-bit unicode strings to
WorkBook(const std::string& fileName, int debug=0, const char *characterSet="UTF-8");
#else
// characterSet has to be UTF-8
WorkBook(const std::string& fileName, int debug=0);
#endif
~WorkBook();
std::string GetLibraryVersion() const;
uint32_t GetSheetCount() const;
// Sheets
xlsString GetSheetName(uint32_t sheetNum) const;
bool GetSheetVisible(uint32_t sheetNum) const;
// Summary
xlsString GetSummaryAppName(void) const;
xlsString GetSummaryAuthor(void) const;
xlsString GetSummaryCategory(void) const;
xlsString GetSummaryComment(void) const;
xlsString GetSummaryCompany(void) const;
xlsString GetSummaryKeywords(void) const;
xlsString GetSummaryLastAuthor(void) const;
xlsString GetSummaryManager(void) const;
xlsString GetSummarySubject(void) const;
xlsString GetSummaryTitle(void) const;
cellContent GetCell(uint32_t workSheetIndex, uint16_t row, uint16_t col); // uses 1 based indexing!
cellContent GetCell(uint32_t workSheetIndex, uint16_t row, const char *colStr); // "A"...."Z" "AA"..."ZZ"
void InitIterator(uint32_t sheetNum = UINT32_MAX); // call this first...
cellContent GetNextCell(void); // ...then this continually til you get a blank cell
void ShowCell(const cellContent& content) const;
private:
WorkBook(const WorkBook& that);
WorkBook& operator=(const WorkBook& right);
private:
void OpenSheet(uint32_t sheetNum);
void FormatCell(xlsCell *cell, cellContent& content) const;
xlsString char2string(const char *ptr) const;
#if XLS_WIDE_STRINGS == 1
bool isAscii(const char *ptr) const;
#endif
private:
const char *charSet; // must be first ivar
bool isUTF8; // unused in the wstring case
#if XLS_WIDE_STRINGS == 1
iconv_t iconvCD;
#endif
uint32_t numSheets;
xlsWorkBook *workBook;
uint32_t activeWorkSheetID; // keep last one active
xlsWorkSheet *activeWorkSheet; // keep last one active
xlsSummaryInfo *summary;
bool iterating;
uint32_t lastRowIndex;
uint32_t lastColIndex;
};
/*
* This is the exception which can be thrown by the xls reader.
*/
class XlsException : public std::runtime_error
{
public:
XlsException(const XlsException &ex) throw()
: std::runtime_error(ex)
{;}
explicit XlsException(const std::string &msg) throw()
: std::runtime_error("XlsReader: " + msg)
{;}
virtual ~XlsException() throw()
{;}
};
}