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
| package com.master.industry.common;
import org.apache.poi.ss.usermodel.BuiltinFormats; import org.apache.poi.ss.usermodel.DataFormatter; import org.apache.poi.xssf.model.SharedStringsTable; import org.apache.poi.xssf.model.StylesTable; import org.apache.poi.xssf.usermodel.XSSFCellStyle; import org.apache.poi.xssf.usermodel.XSSFRichTextString; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler;
import java.text.DecimalFormat; import java.text.SimpleDateFormat; import java.util.Date; import java.util.LinkedList; import java.util.List;
public class BigExcelParse extends DefaultHandler { enum xssfDataType { BOOL, ERROR, FORMULA, INLINESTR, SSTINDEX, NUMBER, }
private StylesTable stylesTable;
private SharedStringsTable sharedStringsTable;
private final int minColumnCount;
private boolean vIsOpen;
private xssfDataType nextDataType;
private short formatIndex; private String formatString; private final DataFormatter formatter;
private int thisColumn = -1; private int lastColumnNumber = -1;
private StringBuffer value; private String[] record; private List<String[]> rows = new LinkedList<>(); private boolean isCellNull = false; private SimpleDateFormat sdf = null; private static DecimalFormat df = new DecimalFormat("###########");
public BigExcelParse(StylesTable styles, SharedStringsTable strings, int cols) { this.stylesTable = styles; this.sharedStringsTable = strings; this.minColumnCount = cols; this.value = new StringBuffer(); this.nextDataType = xssfDataType.NUMBER; this.formatter = new DataFormatter(); record = new String[this.minColumnCount]; rows.clear(); }
public void startElement(String uri, String localName, String name, Attributes attributes) throws SAXException {
if ("inlineStr".equals(name) || "v".equals(name)) { vIsOpen = true; value.setLength(0); } else if ("c".equals(name)) { String r = attributes.getValue("r"); int firstDigit = -1; for (int c = 0; c < r.length(); ++c) { if (Character.isDigit(r.charAt(c))) { firstDigit = c; break; } } thisColumn = nameToColumn(r.substring(0, firstDigit));
this.nextDataType = xssfDataType.NUMBER; this.formatIndex = -1; this.formatString = null; String cellType = attributes.getValue("t"); String cellStyleStr = attributes.getValue("s"); if ("b".equals(cellType)) nextDataType = xssfDataType.BOOL; else if ("e".equals(cellType)) nextDataType = xssfDataType.ERROR; else if ("inlineStr".equals(cellType)) nextDataType = xssfDataType.INLINESTR; else if ("s".equals(cellType)) nextDataType = xssfDataType.SSTINDEX; else if ("str".equals(cellType)) nextDataType = xssfDataType.FORMULA; else if (cellStyleStr != null) { int styleIndex = Integer.parseInt(cellStyleStr); XSSFCellStyle style = stylesTable.getStyleAt(styleIndex); this.formatIndex = style.getDataFormat(); this.formatString = style.getDataFormatString(); if (this.formatString == null) this.formatString = BuiltinFormats .getBuiltinFormat(this.formatIndex); } }
}
public void endElement(String uri, String localName, String name) throws SAXException {
String thisStr = null;
if ("v".equals(name)) { switch (nextDataType) {
case BOOL: char first = value.charAt(0); thisStr = first == '0' ? "FALSE" : "TRUE"; break;
case ERROR: thisStr = "\"ERROR:" + value.toString() + '"'; break;
case FORMULA: thisStr = value.toString(); break;
case INLINESTR: XSSFRichTextString rtsi = new XSSFRichTextString( value.toString()); thisStr =rtsi.toString(); break;
case SSTINDEX: String sstIndex = value.toString(); try { int idx = Integer.parseInt(sstIndex); XSSFRichTextString rtss = new XSSFRichTextString( sharedStringsTable.getEntryAt(idx)); thisStr = rtss.toString(); } catch (NumberFormatException ex) { System.out.println("Failed to parse SST index '" + sstIndex + "': " + ex.toString()); } break;
case NUMBER: String n = value.toString(); if (formatIndex == 14 || formatIndex == 31 || formatIndex == 57 || formatIndex == 58 || (176<=formatIndex && formatIndex<=178) || (182<=formatIndex && formatIndex<=196) || (210<=formatIndex && formatIndex<=213) || (208==formatIndex ) ) { sdf = new SimpleDateFormat("yyyy-MM-dd"); Date date = org.apache.poi.ss.usermodel.DateUtil.getJavaDate(Double.parseDouble(n)); thisStr=sdf.format(date); } else if (formatIndex == 20 || formatIndex == 32 || formatIndex==183 || (200<=formatIndex && formatIndex<=209)) { sdf = new SimpleDateFormat("HH:mm"); Date date = org.apache.poi.ss.usermodel.DateUtil.getJavaDate(Double.parseDouble(n)); thisStr = sdf.format(date); } else{ if(n.contains("E")){ String[] split = n.split("\\+"); String e = split[0].replaceAll("E|e", ""); thisStr=e.replace(".",""); }else { thisStr = n; } } break; default: thisStr = "(TODO: Unexpected type: " + nextDataType + ")"; break; } if (lastColumnNumber == -1) { lastColumnNumber = 0; } if (thisStr == null || "".equals(isCellNull)) { isCellNull = true; } if(thisColumn<record.length) record[thisColumn] = thisStr; if (thisColumn > -1) lastColumnNumber = thisColumn; } else if ("row".equals(name)) { if (minColumnCount > 0) { if (lastColumnNumber == -1) { lastColumnNumber = 0; } if(record!=null &&record.length!=0){ rows.add(record.clone()); isCellNull = false; for (int i = 0; i < record.length; i++) { record[i] = null; } } } lastColumnNumber = -1; }
}
public List<String[]> getRows() { return rows; }
public void setRows(List<String[]> rows) { this.rows = rows; }
public void characters(char[] ch, int start, int length) throws SAXException { if (vIsOpen) value.append(ch, start, length); }
private int nameToColumn(String name) { int column = -1; for (int i = 0; i < name.length(); ++i) { int c = name.charAt(i); column = (column + 1) * 26 + c - 'A'; } return column; } }
|