1 package org.simantics.excel.poi.parser.streaming;
\r
3 import java.util.ArrayList;
\r
4 import java.util.List;
\r
6 import org.apache.poi.ss.usermodel.Row;
\r
7 import org.apache.poi.xssf.model.SharedStringsTable;
\r
8 import org.apache.poi.xssf.usermodel.XSSFRichTextString;
\r
9 import org.xml.sax.Attributes;
\r
10 import org.xml.sax.SAXException;
\r
11 import org.xml.sax.helpers.DefaultHandler;
\r
13 public class XSSSheetHandler extends DefaultHandler {
\r
14 private SharedStringsTable sst;
\r
15 private String lastContents;
\r
16 private boolean nextIsString;
\r
19 List<String> row = new ArrayList<String>();
\r
20 List<String> rowTypes = new ArrayList<String>();
\r
27 public XSSSheetHandler(SharedStringsTable sst) {
\r
31 public void setSheet(SheetImpl sheet) {
\r
35 public void writeRow(Row row) {
\r
36 System.out.println(row);
\r
39 public void startElement(String uri, String localName, String name,
\r
40 Attributes attributes) throws SAXException {
\r
42 // System.out.println(name);
\r
43 // for (int i = 0; i < attributes.getLength(); i++) {
\r
44 // System.out.println(" " + attributes.getLocalName(i) + "; "+attributes.getValue(i) + "; " + attributes.getType(i) );
\r
47 if(name.equals("c")) {
\r
48 // Figure out if the value is an index in the SST
\r
49 cellType = attributes.getValue("t");
\r
50 cellId = attributes.getValue("r");
\r
51 if(cellType != null) {
\r
52 if (cellType.equals("s"))
\r
53 nextIsString = true;
\r
55 nextIsString = false;
\r
57 nextIsString = false;
\r
61 // Clear contents cache
\r
65 public void endElement(String uri, String localName, String name)
\r
66 throws SAXException {
\r
67 // Process the last contents as required.
\r
68 // Do now, as characters() may be called more than once
\r
70 int idx = Integer.parseInt(lastContents);
\r
71 lastContents = new XSSFRichTextString(sst.getEntryAt(idx)).toString();
\r
72 nextIsString = false;
\r
75 // v => contents of a cell
\r
76 // Output after we've seen the string contents
\r
77 if(name.equals("c")) {
\r
78 int colIndex = getColIndex(cellId);
\r
79 for (int i = row.size(); i< colIndex; i++) {
\r
83 row.add(lastContents);
\r
84 rowTypes.add(cellType);
\r
85 //System.out.println(lastContents);
\r
87 } else if (name.equals("row")) {
\r
88 writeRow( new RowImpl(sheet, row, rowTypes,rowIndex));
\r
95 public void characters(char[] ch, int start, int length)
\r
96 throws SAXException {
\r
97 lastContents += new String(ch, start, length);
\r
98 // System.out.println("char : " + lastContents);
\r
101 private int getColIndex(String id) {
\r
103 int range = 'Z'-'A'+1;
\r
104 for (int i = 0; i < id.length(); i++) {
\r
105 char c = id.charAt(i) ;
\r
106 if ('A' <= c && c <= 'Z') {
\r
113 for (int i = 0; i < len; i++) {
\r
114 char c = id.charAt(len-i-1);
\r
115 index += (c-'A') + i * range;
\r