-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExcelFormat.cpp
179 lines (144 loc) · 6.56 KB
/
ExcelFormat.cpp
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
171
172
173
174
175
176
177
178
179
/*
ExcelFormat.cpp
Copyright (c) 2009, 2011 Martin Fuchs <[email protected]>
License: CPOL
THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS CODE PROJECT OPEN LICENSE ("LICENSE").
THE WORK IS PROTECTED BY COPYRIGHT AND/OR OTHER APPLICABLE LAW. ANY USE OF THE WORK OTHER THAN AS
AUTHORIZED UNDER THIS LICENSE OR COPYRIGHT LAW IS PROHIBITED.
BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HEREIN, YOU ACCEPT AND AGREE TO BE BOUND BY THE TERMS
OF THIS LICENSE. THE AUTHOR GRANTS YOU THE RIGHTS CONTAINED HEREIN IN CONSIDERATION OF YOUR ACCEPTANCE
OF SUCH TERMS AND CONDITIONS. IF YOU DO NOT AGREE TO ACCEPT AND BE BOUND BY THE TERMS OF THIS LICENSE,
YOU CANNOT MAKE ANY USE OF THE WORK.
*/
#include "StdAfx.h"
#include "ExcelFormat.h"
using namespace ExcelFormat;
XLSFormatManager::XLSFormatManager(BasicExcel& xls)
: _xls(xls),
_next_fmt_idx(FIRST_USER_FORMAT_IDX) // above last reserved format index 163
{
// initialize predefined formats
_formats[0] = XLS_FORMAT_GENERAL; // "General" // General
_formats[1] = XLS_FORMAT_INTEGER; // "0" // Decimal
_formats[2] = XLS_FORMAT_DECIMAL; // "0.00" // Decimal
_formats[3] = L"#,##0"; // Decimal
_formats[4] = L"#,##0.00"; // Decimal
_formats[5] = L"\"$\"#,##0_);(\"$\"#,##0)"; // Currency
_formats[6] = L"\"$\"#,##0_);[Red](\"$\"#,##0)"; // Currency
_formats[7] = L"\"$\"#,##0.00_);(\"$\"#,##0.00)"; // Currency
_formats[8] = L"\"$\"#,##0.00_);[Red](\"$\"#,##0.00)"; // Currency
_formats[9] = XLS_FORMAT_PERCENT; // "0%" // Percent
_formats[10] = L"0.00%"; // Percent
_formats[11] = L"0.00E+00"; // Scientific
_formats[12] = L"# ?/?"; // Fraction
_formats[13] = L"# ?\?/?\?"; // Fraction
_formats[14] = XLS_FORMAT_DATE; // "M/D/YY" // Date
_formats[15] = L"D-MMM-YY"; // Date
_formats[16] = L"D-MMM"; // Date
_formats[17] = L"MMM-YY"; // Date
_formats[18] = L"h:mm AM/PM"; // Time
_formats[19] = L"h:mm:ss AM/PM"; // Time
_formats[20] = L"h:mm"; // Time
_formats[21] = XLS_FORMAT_TIME; // "h:mm:ss" // Time
_formats[22] = XLS_FORMAT_DATETIME; // "M/D/YY h:mm" // Date/Time
_formats[37] = L"_(#,##0_);(#,##0)"; // Account.
_formats[38] = L"_(#,##0_);[Red](#,##0)"; // Account.
_formats[39] = L"_(#,##0.00_);(#,##0.00)"; // Account.
_formats[40] = L"_(#,##0.00_);[Red](#,##0.00)"; // Account.
_formats[41] = L"_(* #,##0_);_(* (#,##0);_(* \"-\"_);_(@_)"; // Currency
_formats[42] = L"_($* #,##0_);_($* (#,##0);_($* \"-\"_);_(@_)"; // Currency
_formats[43] = L"_(* #,##0.00_);_(* (#,##0.00);_(* \"-\"??_);_(@_)"; // Currency
_formats[44] = L"_($* #,##0.00_);_($* (#,##0.00);_($* \"-\"??_);_(@_)"; // Currency
_formats[45] = L"mm:ss"; // Time
_formats[46] = L"[h]:mm:ss"; // Time
_formats[47] = L"mm:ss.0"; // Time
_formats[48] = L"##0.0E+0"; // Scientific
_formats[49] = XLS_FORMAT_TEXT; // "@" // Text
// overwrite formats from workbook
size_t maxFormats = xls.workbook_.formats_.size();
for(size_t i=0; i<maxFormats; ++i) {
int idx = xls.workbook_.formats_[i].index_;
_formats[idx] = wstringFromLargeString(xls.workbook_.formats_[i].fmtstring_);
// adjust index for the next user defined format
if (idx >= _next_fmt_idx)
_next_fmt_idx = idx + 1;
}
// create reverse format map
for(FormatMap::const_iterator it=_formats.begin(); it!=_formats.end(); ++it)
_formats_rev[it->second] = it->first;
}
int XLSFormatManager::get_font_idx(const ExcelFont& font)
{
int i = 0;
for(vector<Workbook::Font>::const_iterator it=_xls.workbook_.fonts_.begin(); it!=_xls.workbook_.fonts_.end(); ++it,++i)
if (font.matches(*it))
return i;
int font_idx = (int) _xls.workbook_.fonts_.size();
_xls.workbook_.fonts_.push_back(Workbook::Font());
Workbook::Font& new_font = _xls.workbook_.fonts_[font_idx];
new_font.height_ = font._height;
new_font.options_ = font._options;
new_font.colourIndex_ = font._color_index;
new_font.weight_ = font._weight;
new_font.escapementType_ = font._escapement_type;
new_font.underlineType_ = font._underline_type;
new_font.family_ = font._font_family;
new_font.characterSet_ = font._character_set;
new_font.name_ = font._name.c_str();
// The font with index 4 is omitted in all BIFF versions. This means the first four fonts have zero-based indexes,
// and the fifth font and all following fonts are referenced with one-based indexes.
if (font_idx >= 4)
++font_idx;
return font_idx;
}
const Workbook::Font& XLSFormatManager::get_font(const CellFormat& fmt) const
{
size_t font_idx = fmt.get_font_idx();
if (font_idx > 4)
--font_idx;
if (font_idx < _xls.workbook_.fonts_.size())
return _xls.workbook_.fonts_[font_idx];
else {
// assert(0); // font_idx out of range
return _xls.workbook_.fonts_[0];
}
}
wstring XLSFormatManager::get_format_string(const CellFormat& fmt) const
{
int fmt_idx = fmt.get_fmt_idx();
FormatMap::const_iterator found = _formats.find(fmt_idx);
if (found != _formats.end())
return found->second;
else
return XLS_FORMAT_GENERAL;
}
int XLSFormatManager::get_format_idx(const wstring& fmt_str)
{
FormatRevMap::const_iterator found = _formats_rev.find(fmt_str);
if (found != _formats_rev.end())
return found->second;
// register a new format string
int fmt_idx = _next_fmt_idx++;
_formats[fmt_idx] = fmt_str;
_formats_rev[fmt_str] = fmt_idx;
_xls.workbook_.formats_.push_back(Workbook::Format());
Workbook::Format& format = _xls.workbook_.formats_.back();
format.index_ = fmt_idx;
format.fmtstring_ = fmt_str.c_str();
return fmt_idx;
}
int XLSFormatManager::get_xf_idx(const CellFormat& fmt)
{
int i = 0;
for(vector<Workbook::XF>::const_iterator it=_xls.workbook_.XFs_.begin(); it!=_xls.workbook_.XFs_.end(); ++it,++i)
if (fmt.matches(*it))
return i;
// create a new XF
int xf_idx = (int) _xls.workbook_.XFs_.size();
_xls.workbook_.XFs_.push_back(Workbook::XF());
Workbook::XF& xf = _xls.workbook_.XFs_[xf_idx];
fmt.get_xf(xf);
xf.protectionType_ = 0 | (15 << 3); // cell not locked, formula not hidden, type=Cell XF, parent style 15 (default)
// xf.usedAttributes_ = (unsigned char)(0x3F << 2); // XF_USED_ATTRIB: use attributes from parent style: font (0x02), ...
return xf_idx;
}