Java tutorial
/** * Copyright 2013-2015 JueYue (qrb.jueyue@gmail.com) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package cn.bzvs.excel.imports.sax; import cn.bzvs.excel.entity.enmus.CellValueType; import cn.bzvs.excel.entity.sax.SaxReadCellEntity; import cn.bzvs.excel.imports.sax.parse.ISaxRowRead; import com.google.common.collect.Lists; import org.apache.poi.hssf.usermodel.HSSFDateUtil; import org.apache.poi.xssf.model.SharedStringsTable; 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.math.BigDecimal; import java.util.Date; import java.util.List; /** * ? */ public class SheetHandler extends DefaultHandler { private SharedStringsTable sst; private String lastContents; //? private int curRow = 0; //? private int curCol = 0; private CellValueType type; private ISaxRowRead read; // private List<SaxReadCellEntity> rowlist = Lists.newArrayList(); public SheetHandler(SharedStringsTable sst, ISaxRowRead rowRead) { this.sst = sst; this.read = rowRead; } @Override public void startElement(String uri, String localName, String name, Attributes attributes) throws SAXException { // lastContents = ""; // c => ? if ("c".equals(name)) { // SST nextIsStringtrue String cellType = attributes.getValue("t"); if ("s".equals(cellType)) { type = CellValueType.String; return; } //? cellType = attributes.getValue("s"); if ("1".equals(cellType)) { type = CellValueType.Date; } else if ("2".equals(cellType)) { type = CellValueType.Number; } } else if ("t".equals(name)) {//t type = CellValueType.TElement; } } @Override public void endElement(String uri, String localName, String name) throws SAXException { // ?SST?? // characters()? if (CellValueType.String.equals(type)) { try { int idx = Integer.parseInt(lastContents); lastContents = new XSSFRichTextString(sst.getEntryAt(idx)).toString(); } catch (Exception e) { } } //t? if (CellValueType.TElement.equals(type)) { String value = lastContents.trim(); rowlist.add(curCol, new SaxReadCellEntity(CellValueType.String, value)); curCol++; type = CellValueType.None; // v => ??vSST // ?rowlist??? } else if ("v".equals(name)) { String value = lastContents.trim(); value = value.equals("") ? " " : value; if (CellValueType.Date.equals(type)) { Date date = HSSFDateUtil.getJavaDate(Double.valueOf(value)); rowlist.add(curCol, new SaxReadCellEntity(CellValueType.Date, date)); } else if (CellValueType.Number.equals(type)) { BigDecimal bd = new BigDecimal(value); rowlist.add(curCol, new SaxReadCellEntity(CellValueType.Number, bd)); } else if (CellValueType.String.equals(type)) { rowlist.add(curCol, new SaxReadCellEntity(CellValueType.String, value)); } curCol++; } else if (name.equals("row")) {//?? row optRows() read.parse(curRow, rowlist); rowlist.clear(); curRow++; curCol = 0; } } @Override public void characters(char[] ch, int start, int length) throws SAXException { //? lastContents += new String(ch, start, length); } }