There are too many objects that are a part of your library that I do not know
how to manipulate. It 's really too complicated for me. I step whole days
before this class.
I tried to make this change but it does not work and /I'm not sure it's fair
to use twice ArrayList/. What you see below is not working:
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 ArrayList<ArrayList<Object>> dataArray = null;
public ArrayExample(int rows, int cols) {
this.dataArray = new ArrayList<ArrayList<Object>>();
}
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) {
ArrayList<ArrayList<Object>> ae = null;
ArrayList riga = new ArrayList();
ArrayList<ArrayList<Object>> foglio = new
ArrayList<ArrayList<Object>>();
try {
ae = new ArrayList<ArrayList<Object>>();
ae.processOneSheet("file.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-tp5712425p5712533.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]