/Thanks for your help. It is pleasant to meet person like you into the web. I
begin to understand how to use your tool for reading files.
I don't understand these methods and I would like to ask you if you can
semplify them:/
public void startElement(String uri, String localName, String name,
Attributes attributes) throws SAXException {
}
public void endElement(String uri, String localNamem, String name)
throws SAXException {
}
public void characters(char[] ch, int start, int length)
throws SAXException {
}
/uri, localNamem, name are not set! If I remove these three methods the
programme doesn't work so I think that it is possible to rewrite these three
methods more clearly.
I am very happy because I can got my matrix with ArrayList and Iterator but
I would like to remove 100000 and 100, is it possible too?
See my code please:/
package xls;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.apache.poi.xssf.eventusermodel.XSSFReader;
import org.apache.poi.xssf.model.SharedStringsTable;
import org.apache.poi.ss.util.CellReference;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.helpers.XMLReaderFactory;
public class ArrayExample {
private Object[][] dataArray = null;
public ArrayExample(int rows, int cols) {
this.dataArray = new Object[rows][cols];
}
public void processOneSheet(String filename, String sheetname)
throws Exception {
OPCPackage pkg = OPCPackage.open(filename);
XSSFReader r = new XSSFReader(pkg);
SharedStringsTable sst = r.getSharedStringsTable();
XMLReader parser = fetchSheetParser(sst);
InputStream sheet2 = r.getSheet(sheetname);
if (sheet2 != null) {
InputSource sheetSource = new InputSource(sheet2);
parser.parse(sheetSource);
sheet2.close();
} else {
throw new IllegalArgumentException("No sheet exists in
the "
+ "workbook with the name specified: "
+ sheetname);
}
}
public XMLReader fetchSheetParser(SharedStringsTable sst)
throws SAXException {
XMLReader parser = XMLReaderFactory
.createXMLReader("org.apache.xerces.parsers.SAXParser");
ContentHandler handler = new SheetHandler(sst, this.dataArray);
parser.setContentHandler(handler);
return parser;
}
public Object[][] getDataArray() {
return (this.dataArray);
}
private static class SheetHandler extends DefaultHandler {
private SharedStringsTable sst;
private String lastContents;
private boolean nextIsString;
private Object[][] dataArray = null;
private CellReference cellRef = null;
private SheetHandler(SharedStringsTable sst, Object[][]
dataArray) {
this.sst = sst;
this.dataArray = dataArray;
}
public void startElement(String uri, String localName, String
name,
Attributes attributes) throws SAXException {
if (name.equals("c")) {
cellRef = new
CellReference(attributes.getValue("r"));
String cellType = attributes.getValue("t");
if (cellType != null && cellType.equals("s")) {
nextIsString = true;
} else {
nextIsString = false;
}
}
lastContents = "";
}
public void endElement(String uri, String localNamem, String
name)
throws SAXException {
if (nextIsString) {
int idx = Integer.parseInt(lastContents);
lastContents = new
XSSFRichTextString(sst.getEntryAt(idx))
.toString();
nextIsString = false;
}
if (name.equals("v")) {
dataArray[cellRef.getRow()][cellRef.getCol()] =
lastContents;
}
}
public void characters(char[] ch, int start, int length)
throws SAXException {
lastContents += new String(ch, start, length);
}
}
public static void main(String[] args) {
ArrayExample ae = null;
ArrayList riga = new ArrayList();
ArrayList<ArrayList<Object>> foglio = new
ArrayList<ArrayList<Object>>();
try {
ae = new ArrayExample(100000, 100);
ae.processOneSheet("FOGLI_XLS_VARI/_testdata.xlsx",
"rId1");
Object[][] sheetData = ae.getDataArray();
Object[] rowData = null;
Object cellData = null;
for (int i = 0; i < sheetData.length; i++) {
rowData = sheetData[i];
for (int j = 0; j < rowData.length; j++) {
System.out.print(i + ", " + j);
cellData = rowData[j];
if (cellData != null) {
System.out.println("\t" +
cellData.toString());
riga.add(cellData);
} else {
System.out.println("\tEmpty
cell.");
}
}
foglio.add(riga);
riga = new ArrayList();
}
} catch (Exception ex) {
System.out.println("Caught an: " +
ex.getClass().getName());
System.out.println("Message: " + ex.getMessage());
System.out.println("Stacktrace follows:.....");
ex.printStackTrace(System.out);
}
int c = 0;
Iterator<ArrayList<Object>> itr = foglio.iterator();
while (itr.hasNext()) {
ArrayList element = itr.next();
element.size();
if (element.size() > c) {
c = element.size();
}
}
int r = foglio.size();
Object[][] matrice = new Object[r][c];
int i = 0;
int j = 0;
Iterator<ArrayList<Object>> itr2 = foglio.iterator();
Iterator itr3 = null;
while (itr2.hasNext()) {
itr3 = itr2.next().iterator();
while (itr3.hasNext()) {
matrice[i][j] = itr3.next();
System.out.println("matrice[" + i + "][" + j +
"] = "
+ matrice[i][j]);
j++;
}
j = 0;
i++;
}
}
}
--
View this message in context:
http://apache-poi.1045710.n5.nabble.com/Apache-POI-works-but-it-uses-too-RAM-is-it-a-limit-of-your-library-tp5712425p5712526.html
Sent from the POI - User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]