ExcelFormat.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. ExcelFormat.cpp
  3. Copyright (c) 2009, 2011 Martin Fuchs <martin-fuchs@gmx.net>
  4. License: CPOL
  5. THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS CODE PROJECT OPEN LICENSE ("LICENSE").
  6. THE WORK IS PROTECTED BY COPYRIGHT AND/OR OTHER APPLICABLE LAW. ANY USE OF THE WORK OTHER THAN AS
  7. AUTHORIZED UNDER THIS LICENSE OR COPYRIGHT LAW IS PROHIBITED.
  8. BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HEREIN, YOU ACCEPT AND AGREE TO BE BOUND BY THE TERMS
  9. OF THIS LICENSE. THE AUTHOR GRANTS YOU THE RIGHTS CONTAINED HEREIN IN CONSIDERATION OF YOUR ACCEPTANCE
  10. OF SUCH TERMS AND CONDITIONS. IF YOU DO NOT AGREE TO ACCEPT AND BE BOUND BY THE TERMS OF THIS LICENSE,
  11. YOU CANNOT MAKE ANY USE OF THE WORK.
  12. */
  13. #include "stdafx.h"
  14. #include "ExcelFormat.h"
  15. using namespace ExcelFormat;
  16. XLSFormatManager::XLSFormatManager(BasicExcel& xls)
  17. : _xls(xls),
  18. _next_fmt_idx(FIRST_USER_FORMAT_IDX) // above last reserved format index 163
  19. {
  20. // initialize predefined formats
  21. _formats[0] = XLS_FORMAT_GENERAL; // "General" // General
  22. _formats[1] = XLS_FORMAT_INTEGER; // "0" // Decimal
  23. _formats[2] = XLS_FORMAT_DECIMAL; // "0.00" // Decimal
  24. _formats[3] = L"#,##0"; // Decimal
  25. _formats[4] = L"#,##0.00"; // Decimal
  26. _formats[5] = L"\"$\"#,##0_);(\"$\"#,##0)"; // Currency
  27. _formats[6] = L"\"$\"#,##0_);[Red](\"$\"#,##0)"; // Currency
  28. _formats[7] = L"\"$\"#,##0.00_);(\"$\"#,##0.00)"; // Currency
  29. _formats[8] = L"\"$\"#,##0.00_);[Red](\"$\"#,##0.00)"; // Currency
  30. _formats[9] = XLS_FORMAT_PERCENT; // "0%" // Percent
  31. _formats[10] = L"0.00%"; // Percent
  32. _formats[11] = L"0.00E+00"; // Scientific
  33. _formats[12] = L"# ?/?"; // Fraction
  34. _formats[13] = L"# ?\?/?\?"; // Fraction
  35. _formats[14] = XLS_FORMAT_DATE; // "M/D/YY" // Date
  36. _formats[15] = L"D-MMM-YY"; // Date
  37. _formats[16] = L"D-MMM"; // Date
  38. _formats[17] = L"MMM-YY"; // Date
  39. _formats[18] = L"h:mm AM/PM"; // Time
  40. _formats[19] = L"h:mm:ss AM/PM"; // Time
  41. _formats[20] = L"h:mm"; // Time
  42. _formats[21] = XLS_FORMAT_TIME; // "h:mm:ss" // Time
  43. _formats[22] = XLS_FORMAT_DATETIME; // "M/D/YY h:mm" // Date/Time
  44. _formats[37] = L"_(#,##0_);(#,##0)"; // Account.
  45. _formats[38] = L"_(#,##0_);[Red](#,##0)"; // Account.
  46. _formats[39] = L"_(#,##0.00_);(#,##0.00)"; // Account.
  47. _formats[40] = L"_(#,##0.00_);[Red](#,##0.00)"; // Account.
  48. _formats[41] = L"_(* #,##0_);_(* (#,##0);_(* \"-\"_);_(@_)"; // Currency
  49. _formats[42] = L"_($* #,##0_);_($* (#,##0);_($* \"-\"_);_(@_)"; // Currency
  50. _formats[43] = L"_(* #,##0.00_);_(* (#,##0.00);_(* \"-\"??_);_(@_)"; // Currency
  51. _formats[44] = L"_($* #,##0.00_);_($* (#,##0.00);_($* \"-\"??_);_(@_)"; // Currency
  52. _formats[45] = L"mm:ss"; // Time
  53. _formats[46] = L"[h]:mm:ss"; // Time
  54. _formats[47] = L"mm:ss.0"; // Time
  55. _formats[48] = L"##0.0E+0"; // Scientific
  56. _formats[49] = XLS_FORMAT_TEXT; // "@" // Text
  57. // overwrite formats from workbook
  58. size_t maxFormats = xls.workbook_.formats_.size();
  59. for(size_t i=0; i<maxFormats; ++i) {
  60. int idx = xls.workbook_.formats_[i].index_;
  61. _formats[idx] = wstringFromLargeString(xls.workbook_.formats_[i].fmtstring_);
  62. // adjust index for the next user defined format
  63. if (idx >= _next_fmt_idx)
  64. _next_fmt_idx = idx + 1;
  65. }
  66. // create reverse format map
  67. for(FormatMap::const_iterator it=_formats.begin(); it!=_formats.end(); ++it)
  68. _formats_rev[it->second] = it->first;
  69. }
  70. int XLSFormatManager::get_font_idx(const ExcelFont& font)
  71. {
  72. int i = 0;
  73. for(vector<Workbook::Font>::const_iterator it=_xls.workbook_.fonts_.begin(); it!=_xls.workbook_.fonts_.end(); ++it,++i)
  74. if (font.matches(*it))
  75. return i;
  76. int font_idx = (int) _xls.workbook_.fonts_.size();
  77. _xls.workbook_.fonts_.push_back(Workbook::Font());
  78. Workbook::Font& new_font = _xls.workbook_.fonts_[font_idx];
  79. new_font.height_ = font._height;
  80. new_font.options_ = font._options;
  81. new_font.colourIndex_ = font._color_index;
  82. new_font.weight_ = font._weight;
  83. new_font.escapementType_ = font._escapement_type;
  84. new_font.underlineType_ = font._underline_type;
  85. new_font.family_ = font._font_family;
  86. new_font.characterSet_ = font._character_set;
  87. new_font.name_ = font._name.c_str();
  88. // The font with index 4 is omitted in all BIFF versions. This means the first four fonts have zero-based indexes,
  89. // and the fifth font and all following fonts are referenced with one-based indexes.
  90. if (font_idx >= 4)
  91. ++font_idx;
  92. return font_idx;
  93. }
  94. const Workbook::Font& XLSFormatManager::get_font(const CellFormat& fmt) const
  95. {
  96. size_t font_idx = fmt.get_font_idx();
  97. if (font_idx > 4)
  98. --font_idx;
  99. if (font_idx < _xls.workbook_.fonts_.size())
  100. return _xls.workbook_.fonts_[font_idx];
  101. else {
  102. // assert(0); // font_idx out of range
  103. return _xls.workbook_.fonts_[0];
  104. }
  105. }
  106. wstring XLSFormatManager::get_format_string(const CellFormat& fmt) const
  107. {
  108. int fmt_idx = fmt.get_fmt_idx();
  109. FormatMap::const_iterator found = _formats.find(fmt_idx);
  110. if (found != _formats.end())
  111. return found->second;
  112. else
  113. return XLS_FORMAT_GENERAL;
  114. }
  115. int XLSFormatManager::get_format_idx(const wstring& fmt_str)
  116. {
  117. FormatRevMap::const_iterator found = _formats_rev.find(fmt_str);
  118. if (found != _formats_rev.end())
  119. return found->second;
  120. // register a new format string
  121. int fmt_idx = _next_fmt_idx++;
  122. _formats[fmt_idx] = fmt_str;
  123. _formats_rev[fmt_str] = fmt_idx;
  124. _xls.workbook_.formats_.push_back(Workbook::Format());
  125. Workbook::Format& format = _xls.workbook_.formats_.back();
  126. format.index_ = fmt_idx;
  127. format.fmtstring_ = fmt_str.c_str();
  128. return fmt_idx;
  129. }
  130. int XLSFormatManager::get_xf_idx(const CellFormat& fmt)
  131. {
  132. int i = 0;
  133. for(vector<Workbook::XF>::const_iterator it=_xls.workbook_.XFs_.begin(); it!=_xls.workbook_.XFs_.end(); ++it,++i)
  134. if (fmt.matches(*it))
  135. return i;
  136. // create a new XF
  137. int xf_idx = (int) _xls.workbook_.XFs_.size();
  138. _xls.workbook_.XFs_.push_back(Workbook::XF());
  139. Workbook::XF& xf = _xls.workbook_.XFs_[xf_idx];
  140. fmt.get_xf(xf);
  141. xf.protectionType_ = 0 | (15 << 3); // cell not locked, formula not hidden, type=Cell XF, parent style 15 (default)
  142. // xf.usedAttributes_ = (unsigned char)(0x3F << 2); // XF_USED_ATTRIB: use attributes from parent style: font (0x02), ...
  143. return xf_idx;
  144. }