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 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
| private class ExcelXSSFHandler extends DefaultHandler {
private XSSFReader xssfReader;
private XMLReader xmlReader;
private SharedStringsTable stringTable;
private StylesTable stylesTable;
private Map<String, String> sheetNameRidMap = new LinkedHashMap<>();
private List<String> sheetNameList = new ArrayList<>();
private List<String> sheetNameGivenList = new ArrayList<>();
private int sheetReadingIndex = 0;
private String sheetName;
private int sheetIndex;
private int rowMax;
private int cellMax;
private int rowIndex = 0;
private int cellIndex = 0;
private CellType cellType;
private String lastContents = "";
private List<String> columnList;
private boolean isEnd = false;
private List<ExcelRow> sheetData;
private int rowReadingIndex = 0;
private String formatString;
private short formatIndex;
public ExcelXSSFHandler(XSSFReader xssfReader, XMLReader xmlReader) throws InvalidFormatException, IOException, SAXException, DocumentException { this.xmlReader = xmlReader; this.xssfReader = xssfReader; this.stringTable = xssfReader.getSharedStringsTable(); this.stylesTable = xssfReader.getStylesTable(); InputStream bookStream = null; try{ bookStream = xssfReader.getWorkbookData(); SAXReader reader = new SAXReader(); reader.setEncoding("UTF-8"); Document doc = reader.read(bookStream); Element book = doc.getRootElement(); Element sheets = book.element("sheets"); Iterator<?> it = sheets.elementIterator("sheet"); while(it.hasNext()){ Element sheet = (Element)it.next(); String name = sheet.attributeValue("name"); String rid = sheet.attributeValue("id"); sheetNameList.add(name); sheetNameRidMap.put(name, rid); } }finally{ IoUtil.close(bookStream); } sheetNameGivenList.addAll(sheetNameList); }
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { if (qName.equals("dimension")) { String dimension = attributes.getValue("ref"); if(dimension.contains(":")){ dimension = dimension.split(":")[1]; } rowMax = getRowIndex(dimension); cellMax = getCellIndex(dimension); sheetData = new ArrayList<>(rowMax); }
if (qName.equals("row")) { cellIndex = 0; int currentRowIndex = Integer.valueOf(attributes.getValue("r")) - 1; while(rowIndex < currentRowIndex){ ExcelRow data = new ExcelRow(rowIndex + 1, new ArrayList<String>(0)); data.setSheetIndex(sheetIndex); data.setSheetName(sheetName); data.setEmpty(true); data.setLastRow(rowIndex == rowMax - 1); sheetData.add(data); rowIndex++; } columnList = new ArrayList<>(cellMax); }
if(qName.equals("c")) { lastContents = ""; int currentCellIndex = getCellIndex(attributes.getValue("r")); while(cellIndex < currentCellIndex){ columnList.add(""); cellIndex++; }
String type = attributes.getValue("t"); if(type == null){ cellType = CellType.BLANK; }else{ switch (type) { case "b" -> cellType = CellType.BOOLEAN; case "e" -> cellType = CellType.ERROR; case "inlineStr" -> cellType = CellType._NONE; case "s" -> cellType = CellType.STRING; case "str" -> cellType = CellType.FORMULA; default -> cellType = CellType.BLANK; } }
String cellStyle = attributes.getValue("s"); if (cellStyle != null) { int styleIndex = Integer.parseInt(cellStyle); XSSFCellStyle style = stylesTable.getStyleAt(styleIndex); formatIndex = style.getDataFormat(); formatString = style.getDataFormatString(); if (formatString == null) { formatString = BuiltinFormats.getBuiltinFormat(formatIndex); } } } }
public void endElement(String uri, String localName, String qName) throws SAXException {
if (qName.equals("v")) { switch (cellType){ case STRING: int sharedIndex = Integer.valueOf(lastContents); lastContents = stringTable.getItemAt(sharedIndex).toString(); break; case _NONE: lastContents = new XSSFRichTextString(lastContents).toString(); break; case FORMULA: try { lastContents = ExcelReader.formatDouble(lastContents); } catch (Exception e) { lastContents = new XSSFRichTextString(lastContents).toString(); LOG.error("Excel format error: sheet=" + sheetName + ",row=" + rowIndex + ",column=" + cellIndex, e); } break; case BOOLEAN: case ERROR: default:
}
if (formatString != null) { try{ if(DateUtil.isADateFormat(formatIndex,formatString)) { double value = Double.valueOf(lastContents); if(DateUtil.isValidExcelDate(value)) { Date date = DateUtil.getJavaDate(value, false); lastContents = String.valueOf(date.getTime()); } }else{ lastContents = ExcelReader.formatDouble(lastContents); } }catch(Exception e){ LOG.error("Excel format error: sheetName=" + sheetName + ", rowIndex=" + (rowIndex + 1) + ",column=" + cellIndex, e); } formatString = null; } }
if (qName.equals("c")) { columnList.add(lastContents); cellIndex++; }
if (qName.equals("row")) { ExcelRow data = new ExcelRow(rowIndex + 1, columnList); data.setSheetIndex(sheetIndex); data.setSheetName(sheetName); data.setEmpty(false); data.setLastRow(rowIndex == rowMax - 1); sheetData.add(data); rowIndex++; } }
public void characters(char[] ch, int start, int length) throws SAXException { lastContents += new String(ch, start, length); }
private int getRowIndex(String dimension){ int len = dimension.length(); int index = 0; for(int i = len - 1; i >= 0; i--){ if(dimension.charAt(i) > 64){ index = i; break; } } return Integer.valueOf(dimension.substring(index + 1)); }
private int getCellIndex(String dimension){ int len = dimension.length(); int index = 0; for(int i = len - 1; i >= 0; i--){ if(dimension.charAt(i) > 64){ index = i; break; } } String cellstr = dimension.substring(0, index + 1);
int cellIndex = 0; int indexLen = cellstr.length(); int bitIndex = 0; for(int i = indexLen - 1; i >= 0; i--){ int base = 1; int c = cellstr.charAt(i) - 65; if(bitIndex > 0){ c++; for(int j = 0; j < bitIndex; j++){ base *= 26; } } cellIndex += c * base; bitIndex++; } return cellIndex; }
public List<ExcelRow> readSheet() throws Exception{ while(true){ List<ExcelRow> list = new ArrayList<>(); if(isEnd){ return null; }else if(sheetData == null){ if(sheetFilter != null){ sheetFilter.resetSheetListForRead(sheetNameGivenList); } read(); continue; }else if(rowReadingIndex > 0 && rowReadingIndex < sheetData.size()){ while(rowReadingIndex < sheetData.size()){ ExcelRow row = sheetData.get(rowReadingIndex); rowReadingIndex++; list.add(row); } read(); return list; }else{ list.addAll(sheetData); read(); return list; } } }
public ExcelRow readRow() throws Exception { while(true){ if(isEnd){ return null; }else if(sheetData == null){ if(sheetFilter != null){ sheetFilter.resetSheetListForRead(sheetNameGivenList); } read(); continue; }
if(rowReadingIndex < sheetData.size()){ ExcelRow row = sheetData.get(rowReadingIndex); rowReadingIndex++; return row; }else{ read(); } } }
private void read() throws Exception { if(sheetReadingIndex < sheetNameGivenList.size()) { sheetName = sheetNameGivenList.get(sheetReadingIndex); sheetIndex = sheetNameList.indexOf(sheetName) + 1; if(sheetIndex == -1){ rowReadingIndex = 0; sheetReadingIndex++; return; }
if(sheetFilter != null && !sheetFilter.filter(sheetIndex, sheetName)){ rowReadingIndex = 0; sheetReadingIndex++; return; }
String relId = sheetNameRidMap.get(sheetName); rowIndex = 0; InputStream sheetStream = null; try{ sheetStream = xssfReader.getSheet(relId); xmlReader.parse(new InputSource(sheetStream)); }finally{ IoUtil.close(sheetStream); } rowReadingIndex = 0; sheetReadingIndex++; }else{ isEnd = true; } } }
|