package frame.file.office.excel; import java.util.Date; import org.apache.poi.hssf.usermodel.HSSFDateUtil; 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 frame.file.FileIOItem; import frame.util.Util; public abstract class SheetHandler extends DefaultHandler { public static final int ERROR = 1; public static final int BOOLEAN = 1; public static final int NUMBER = 2; public static final int STRING = 3; public static final int DATE = 4; private int index = 0; private SharedStringsTable sharedStringsTable; private StylesTable stylesTable; private String readValue; private XssfDataType dataType; private String[] rowDatas; private int[] rowTypes; private int colIdx; private short formatIndex; private FileIOItem fileIOItem; public SheetHandler(SharedStringsTable sst,StylesTable stylesTable) { this.sharedStringsTable = sst; this.stylesTable = stylesTable; } protected abstract void outputRow(String[] datas, int[] rowTypes, int rowIndex, FileIOItem fileIOItem); public void startElement(String uri, String localName, String name, Attributes attributes) throws SAXException { if(name.equals("c")) {// c > 单元格 colIdx = getColumn(attributes); String cellType = attributes.getValue("t"); String cellStyle = attributes.getValue("s"); this.dataType = XssfDataType.NUMBER; if ("b".equals(cellType)){ this.dataType = XssfDataType.BOOL; } else if ("e".equals(cellType)){ this.dataType = XssfDataType.ERROR; } else if ("inlineStr".equals(cellType)){ this.dataType = XssfDataType.INLINESTR; } else if ("s".equals(cellType)){ this.dataType = XssfDataType.SSTINDEX; } else if ("str".equals(cellType)){ this.dataType = XssfDataType.FORMULA; } else if(cellStyle != null){ int styleIndex = Integer.parseInt(cellStyle); XSSFCellStyle style = stylesTable.getStyleAt(styleIndex); this.formatIndex = style.getDataFormat(); // this.formatString = style.getDataFormatString(); } } // 解析到一行的开始处时,初始化数组 else if(name.equals("row")){ int cols = getColsNum(attributes);// 获取该行的单元格数 rowDatas = new String[cols]; rowTypes = new int[cols]; } readValue = ""; } public void endElement(String uri, String localName, String name) throws SAXException { if(name.equals("v")) { // 单元格的值 switch(this.dataType){ case BOOL: { char first = readValue.charAt(0); rowDatas[colIdx] = first == '0' ? "FALSE" : "TRUE"; rowTypes[colIdx] = BOOLEAN; break; } case ERROR: { rowDatas[colIdx] = "ERROR:" + readValue.toString(); rowTypes[colIdx] = ERROR; break; } case INLINESTR: { rowDatas[colIdx] = new XSSFRichTextString(readValue).toString(); rowTypes[colIdx] = STRING; break; } case SSTINDEX:{ int idx = Integer.parseInt(readValue); rowDatas[colIdx] = new XSSFRichTextString(sharedStringsTable.getEntryAt(idx)).toString(); rowTypes[colIdx] = STRING; break; } case FORMULA:{ rowDatas[colIdx] = readValue; rowTypes[colIdx] = STRING; break; } case NUMBER:{ // 判断是否是日期格式 if (HSSFDateUtil.isADateFormat(formatIndex, readValue)) { Double d = Double.parseDouble(readValue); Date date = HSSFDateUtil.getJavaDate(d); rowDatas[colIdx] = Util.getTimeStamp(date); rowTypes[colIdx] = DATE; } // else if (formatString != null){ // cellData.value = formatter.formatRawCellContents(Double.parseDouble(cellValue), formatIndex, formatString); // cellData.dataType = NUMBER; // } else{ rowDatas[colIdx] = readValue; rowTypes[colIdx] = NUMBER; } break; } } } // 当解析的一行的末尾时,输出数组中的数据 else if(name.equals("row")){ outputRow(rowDatas, rowTypes, index++, fileIOItem); } } public void characters(char[] ch, int start, int length) throws SAXException { readValue += new String(ch, start, length); } public void setFileIoItem(FileIOItem fileIOItem) { this.fileIOItem = fileIOItem; } private int getColumn(Attributes attrubuts) { String name = attrubuts.getValue("r"); int column = -1; for (int i = 0; i < name.length(); ++i) { if (Character.isDigit(name.charAt(i))) { break; } int c = name.charAt(i); column = (column + 1) * 26 + c - 'A'; } return column; } private int getColsNum(Attributes attrubuts){ String spans = attrubuts.getValue("spans"); String cols = spans.substring(spans.indexOf(":") + 1); return Integer.parseInt(cols); } }