Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

XSSFBSheetHandler.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.xssf.binary;
  16. import java.io.InputStream;
  17. import java.util.Queue;
  18. import org.apache.poi.ss.usermodel.BuiltinFormats;
  19. import org.apache.poi.ss.usermodel.DataFormatter;
  20. import org.apache.poi.ss.usermodel.RichTextString;
  21. import org.apache.poi.ss.util.CellAddress;
  22. import org.apache.poi.util.Internal;
  23. import org.apache.poi.util.LittleEndian;
  24. import org.apache.poi.xssf.eventusermodel.XSSFSheetXMLHandler;
  25. import org.apache.poi.xssf.model.SharedStrings;
  26. import org.apache.poi.xssf.usermodel.XSSFComment;
  27. /**
  28. * @since 3.16-beta3
  29. */
  30. @Internal
  31. public class XSSFBSheetHandler extends XSSFBParser {
  32. private static final int CHECK_ALL_ROWS = -1;
  33. private final SharedStrings stringsTable;
  34. private final XSSFSheetXMLHandler.SheetContentsHandler handler;
  35. private final XSSFBStylesTable styles;
  36. private final XSSFBCommentsTable comments;
  37. private final DataFormatter dataFormatter;
  38. private final boolean formulasNotResults;//TODO: implement this
  39. private int lastEndedRow = -1;
  40. private int lastStartedRow = -1;
  41. private int currentRow;
  42. private byte[] rkBuffer = new byte[8];
  43. private XSSFBCellRange hyperlinkCellRange;
  44. private StringBuilder xlWideStringBuffer = new StringBuilder();
  45. private final XSSFBCellHeader cellBuffer = new XSSFBCellHeader();
  46. public XSSFBSheetHandler(InputStream is,
  47. XSSFBStylesTable styles,
  48. XSSFBCommentsTable comments,
  49. SharedStrings strings,
  50. XSSFSheetXMLHandler.SheetContentsHandler sheetContentsHandler,
  51. DataFormatter dataFormatter,
  52. boolean formulasNotResults) {
  53. super(is);
  54. this.styles = styles;
  55. this.comments = comments;
  56. this.stringsTable = strings;
  57. this.handler = sheetContentsHandler;
  58. this.dataFormatter = dataFormatter;
  59. this.formulasNotResults = formulasNotResults;
  60. }
  61. @Override
  62. public void handleRecord(int id, byte[] data) throws XSSFBParseException {
  63. XSSFBRecordType type = XSSFBRecordType.lookup(id);
  64. switch(type) {
  65. case BrtRowHdr:
  66. int rw = XSSFBUtils.castToInt(LittleEndian.getUInt(data, 0));
  67. if (rw > 0x00100000) {//could make sure this is larger than currentRow, according to spec?
  68. throw new XSSFBParseException("Row number beyond allowable range: "+rw);
  69. }
  70. currentRow = rw;
  71. checkMissedComments(currentRow);
  72. startRow(currentRow);
  73. break;
  74. case BrtCellIsst:
  75. handleBrtCellIsst(data);
  76. break;
  77. case BrtCellSt: //TODO: needs test
  78. handleCellSt(data);
  79. break;
  80. case BrtCellRk:
  81. handleCellRk(data);
  82. break;
  83. case BrtCellReal:
  84. handleCellReal(data);
  85. break;
  86. case BrtCellBool:
  87. handleBoolean(data);
  88. break;
  89. case BrtCellError:
  90. handleCellError(data);
  91. break;
  92. case BrtCellBlank:
  93. beforeCellValue(data);//read cell info and check for missing comments
  94. break;
  95. case BrtFmlaString:
  96. handleFmlaString(data);
  97. break;
  98. case BrtFmlaNum:
  99. handleFmlaNum(data);
  100. break;
  101. case BrtFmlaError:
  102. handleFmlaError(data);
  103. break;
  104. //TODO: All the PCDI and PCDIA
  105. case BrtEndSheetData:
  106. checkMissedComments(CHECK_ALL_ROWS);
  107. endRow(lastStartedRow);
  108. break;
  109. case BrtBeginHeaderFooter:
  110. handleHeaderFooter(data);
  111. break;
  112. }
  113. }
  114. private void beforeCellValue(byte[] data) {
  115. XSSFBCellHeader.parse(data, 0, currentRow, cellBuffer);
  116. checkMissedComments(currentRow, cellBuffer.getColNum());
  117. }
  118. private void handleCellValue(String formattedValue) {
  119. CellAddress cellAddress = new CellAddress(currentRow, cellBuffer.getColNum());
  120. XSSFBComment comment = null;
  121. if (comments != null) {
  122. comment = comments.get(cellAddress);
  123. }
  124. handler.cell(cellAddress.formatAsString(), formattedValue, comment);
  125. }
  126. private void handleFmlaNum(byte[] data) {
  127. beforeCellValue(data);
  128. //xNum
  129. double val = LittleEndian.getDouble(data, XSSFBCellHeader.length);
  130. handleCellValue(formatVal(val, cellBuffer.getStyleIdx()));
  131. }
  132. private void handleCellSt(byte[] data) {
  133. beforeCellValue(data);
  134. xlWideStringBuffer.setLength(0);
  135. XSSFBUtils.readXLWideString(data, XSSFBCellHeader.length, xlWideStringBuffer);
  136. handleCellValue(xlWideStringBuffer.toString());
  137. }
  138. private void handleFmlaString(byte[] data) {
  139. beforeCellValue(data);
  140. xlWideStringBuffer.setLength(0);
  141. XSSFBUtils.readXLWideString(data, XSSFBCellHeader.length, xlWideStringBuffer);
  142. handleCellValue(xlWideStringBuffer.toString());
  143. }
  144. private void handleCellError(byte[] data) {
  145. beforeCellValue(data);
  146. //TODO, read byte to figure out the type of error
  147. handleCellValue("ERROR");
  148. }
  149. private void handleFmlaError(byte[] data) {
  150. beforeCellValue(data);
  151. //TODO, read byte to figure out the type of error
  152. handleCellValue("ERROR");
  153. }
  154. private void handleBoolean(byte[] data) {
  155. beforeCellValue(data);
  156. String formattedVal = (data[XSSFBCellHeader.length] == 1) ? "TRUE" : "FALSE";
  157. handleCellValue(formattedVal);
  158. }
  159. private void handleCellReal(byte[] data) {
  160. beforeCellValue(data);
  161. //xNum
  162. double val = LittleEndian.getDouble(data, XSSFBCellHeader.length);
  163. handleCellValue(formatVal(val, cellBuffer.getStyleIdx()));
  164. }
  165. private void handleCellRk(byte[] data) {
  166. beforeCellValue(data);
  167. double val = rkNumber(data, XSSFBCellHeader.length);
  168. handleCellValue(formatVal(val, cellBuffer.getStyleIdx()));
  169. }
  170. private String formatVal(double val, int styleIdx) {
  171. String formatString = styles.getNumberFormatString(styleIdx);
  172. short styleIndex = styles.getNumberFormatIndex(styleIdx);
  173. //for now, if formatString is null, silently punt
  174. //and use "General". Not the best behavior,
  175. //but we're doing it now in the streaming and non-streaming
  176. //extractors for xlsx. See BUG-61053
  177. if (formatString == null) {
  178. formatString = BuiltinFormats.getBuiltinFormat(0);
  179. styleIndex = 0;
  180. }
  181. return dataFormatter.formatRawCellContents(val, styleIndex, formatString);
  182. }
  183. private void handleBrtCellIsst(byte[] data) {
  184. beforeCellValue(data);
  185. int idx = XSSFBUtils.castToInt(LittleEndian.getUInt(data, XSSFBCellHeader.length));
  186. RichTextString rtss = stringsTable.getItemAt(idx);
  187. handleCellValue(rtss.getString());
  188. }
  189. private void handleHeaderFooter(byte[] data) {
  190. XSSFBHeaderFooters headerFooter = XSSFBHeaderFooters.parse(data);
  191. outputHeaderFooter(headerFooter.getHeader());
  192. outputHeaderFooter(headerFooter.getFooter());
  193. outputHeaderFooter(headerFooter.getHeaderEven());
  194. outputHeaderFooter(headerFooter.getFooterEven());
  195. outputHeaderFooter(headerFooter.getHeaderFirst());
  196. outputHeaderFooter(headerFooter.getFooterFirst());
  197. }
  198. private void outputHeaderFooter(XSSFBHeaderFooter headerFooter) {
  199. String text = headerFooter.getString();
  200. if (text != null && text.trim().length() > 0) {
  201. handler.headerFooter(text, headerFooter.isHeader(), headerFooter.getHeaderFooterTypeLabel());
  202. }
  203. }
  204. //at start of next cell or end of row, return the cellAddress if it equals currentRow and col
  205. private void checkMissedComments(int currentRow, int colNum) {
  206. if (comments == null) {
  207. return;
  208. }
  209. Queue<CellAddress> queue = comments.getAddresses();
  210. while (queue.size() > 0) {
  211. CellAddress cellAddress = queue.peek();
  212. if (cellAddress.getRow() == currentRow && cellAddress.getColumn() < colNum) {
  213. cellAddress = queue.remove();
  214. dumpEmptyCellComment(cellAddress, comments.get(cellAddress));
  215. } else if (cellAddress.getRow() == currentRow && cellAddress.getColumn() == colNum) {
  216. queue.remove();
  217. return;
  218. } else if (cellAddress.getRow() == currentRow && cellAddress.getColumn() > colNum) {
  219. return;
  220. } else if (cellAddress.getRow() > currentRow) {
  221. return;
  222. }
  223. }
  224. }
  225. //check for anything from rows before
  226. private void checkMissedComments(int currentRow) {
  227. if (comments == null) {
  228. return;
  229. }
  230. Queue<CellAddress> queue = comments.getAddresses();
  231. int lastInterpolatedRow = -1;
  232. while (queue.size() > 0) {
  233. CellAddress cellAddress = queue.peek();
  234. if (currentRow == CHECK_ALL_ROWS || cellAddress.getRow() < currentRow) {
  235. cellAddress = queue.remove();
  236. if (cellAddress.getRow() != lastInterpolatedRow) {
  237. startRow(cellAddress.getRow());
  238. }
  239. dumpEmptyCellComment(cellAddress, comments.get(cellAddress));
  240. lastInterpolatedRow = cellAddress.getRow();
  241. } else {
  242. break;
  243. }
  244. }
  245. }
  246. private void startRow(int row) {
  247. if (row == lastStartedRow) {
  248. return;
  249. }
  250. if (lastStartedRow != lastEndedRow) {
  251. endRow(lastStartedRow);
  252. }
  253. handler.startRow(row);
  254. lastStartedRow = row;
  255. }
  256. private void endRow(int row) {
  257. if (lastEndedRow == row) {
  258. return;
  259. }
  260. handler.endRow(row);
  261. lastEndedRow = row;
  262. }
  263. private void dumpEmptyCellComment(CellAddress cellAddress, XSSFBComment comment) {
  264. handler.cell(cellAddress.formatAsString(), null, comment);
  265. }
  266. private double rkNumber(byte[] data, int offset) {
  267. //see 2.5.122
  268. byte b0 = data[offset];
  269. boolean numDivBy100 = ((b0 & 1) == 1); // else as is
  270. boolean floatingPoint = ((b0 >> 1 & 1) == 0); // else signed integer
  271. //unset highest 2 bits
  272. b0 &= ~1;
  273. b0 &= ~(1<<1);
  274. rkBuffer[4] = b0;
  275. for (int i = 1; i < 4; i++) {
  276. rkBuffer[i+4] = data[offset+i];
  277. }
  278. double d = 0.0;
  279. if (floatingPoint) {
  280. d = LittleEndian.getDouble(rkBuffer);
  281. } else {
  282. int rawInt = LittleEndian.getInt(rkBuffer, 4);
  283. d = rawInt >> 2;//divide by 4/shift bits coz 30 bit int, not 32
  284. }
  285. d = (numDivBy100) ? d/100 : d;
  286. return d;
  287. }
  288. /**
  289. * You need to implement this to handle the results
  290. * of the sheet parsing.
  291. */
  292. public interface SheetContentsHandler extends XSSFSheetXMLHandler.SheetContentsHandler {
  293. /**
  294. * A cell, with the given formatted value (may be null),
  295. * a url (may be null), a toolTip (may be null)
  296. * and possibly a comment (may be null), was encountered */
  297. void hyperlinkCell(String cellReference, String formattedValue, String url, String toolTip, XSSFComment comment);
  298. }
  299. }