com.qihang.winter.poi.excel.imports.sax.SheetHandler.java Source code

Java tutorial

Introduction

Here is the source code for com.qihang.winter.poi.excel.imports.sax.SheetHandler.java

Source

/**
 * 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 com.qihang.winter.poi.excel.imports.sax;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.xssf.model.SharedStringsTable;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import com.qihang.winter.poi.excel.entity.sax.SaxReadCellEntity;
import com.qihang.winter.poi.excel.imports.sax.parse.ISaxRowRead;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.google.common.collect.Lists;

/**
 * ?
 * @author Zerrion
 * @date 20141229 ?9:50:09
 */
public class SheetHandler extends DefaultHandler {

    private SharedStringsTable sst;
    private String lastContents;

    //?  
    private int curRow = 0;
    //?  
    private int curCol = 0;

    private com.qihang.winter.poi.excel.entity.enmus.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 = com.qihang.winter.poi.excel.entity.enmus.CellValueType.String;
                return;
            }
            //?  
            cellType = attributes.getValue("s");
            if ("1".equals(cellType)) {
                type = com.qihang.winter.poi.excel.entity.enmus.CellValueType.Date;
            } else if ("2".equals(cellType)) {
                type = com.qihang.winter.poi.excel.entity.enmus.CellValueType.Number;
            }
        } else if ("t".equals(name)) {//t  
            type = com.qihang.winter.poi.excel.entity.enmus.CellValueType.TElement;
        }

    }

    @Override
    public void endElement(String uri, String localName, String name) throws SAXException {

        // ?SST??  
        // characters()?  
        if (com.qihang.winter.poi.excel.entity.enmus.CellValueType.String.equals(type)) {
            try {
                int idx = Integer.parseInt(lastContents);
                lastContents = new XSSFRichTextString(sst.getEntryAt(idx)).toString();
            } catch (Exception e) {

            }
        }
        //t?  
        if (com.qihang.winter.poi.excel.entity.enmus.CellValueType.TElement.equals(type)) {
            String value = lastContents.trim();
            rowlist.add(curCol,
                    new SaxReadCellEntity(com.qihang.winter.poi.excel.entity.enmus.CellValueType.String, value));
            curCol++;
            type = com.qihang.winter.poi.excel.entity.enmus.CellValueType.None;
            // v => ??vSST  
            // ?rowlist???  
        } else if ("v".equals(name)) {
            String value = lastContents.trim();
            value = value.equals("") ? " " : value;
            if (com.qihang.winter.poi.excel.entity.enmus.CellValueType.Date.equals(type)) {
                Date date = HSSFDateUtil.getJavaDate(Double.valueOf(value));
                rowlist.add(curCol,
                        new SaxReadCellEntity(com.qihang.winter.poi.excel.entity.enmus.CellValueType.Date, date));
            } else if (com.qihang.winter.poi.excel.entity.enmus.CellValueType.Number.equals(type)) {
                BigDecimal bd = new BigDecimal(value);
                rowlist.add(curCol,
                        new SaxReadCellEntity(com.qihang.winter.poi.excel.entity.enmus.CellValueType.Number, bd));
            } else if (com.qihang.winter.poi.excel.entity.enmus.CellValueType.String.equals(type)) {
                rowlist.add(curCol, new SaxReadCellEntity(
                        com.qihang.winter.poi.excel.entity.enmus.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);
    }

}