david-PC\david
2018-06-12 f240ac3ccd37c541cab2c21cfc433d3510999a3c
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
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.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 XssfDataType dataType;
    private String[] rowDatas;
    private int[] rowTypes;
    private int colIdx;
      
    private short formatIndex;  
      
    private SheetHandler(SharedStringsTable sst,StylesTable stylesTable) {  
        this.sharedStringsTable = sst;  
        this.stylesTable = stylesTable;  
    }  
      
    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++);  
        }  
    }  
 
    public void characters(char[] ch, int start, int length)  
            throws SAXException {  
        readValue += new String(ch, start, length);  
    }  
    
    protected abstract void outputRow(String[] datas, int[] rowTypes, int rowIndex);  
    
    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);  
    }  
}