Your location:Tech News>News>Script>Solution Excel to read about the Poi lead to memory overflow problem

Latest News

Solution Excel to read about the Poi lead to memory overflow problem

Author:fsadmin

Views:

  import java.io.InputStream;

  import org.apache.poi.openxml4j.opc.OPCPackage;

  import org.apache.poi.xssf.eventusermodel.XSSFReader;

  import org.apache.poi.xssf.model.SharedStringsTable;

  import org.apache.poi.xssf.usermodel.XSSFRichTextString;

  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 EventModel {

  public void processOneSheet (String filename) throws Exception {

  OPCPackage pkg = OPCPackage.open (filename);

  XSSFReader r = new XSSFReader (pkg);

  SharedStringsTable sst = r.getSharedStringsTable ();

  XMLReader parser = fetchSheetParser (sst);

  InputStream sheet2 = r.getSheet ( "rId1");

  InputSource sheetSource = new InputSource (sheet2);

  parser.parse (sheetSource);

  sheet2.close ();

  }

  public XMLReader fetchSheetParser (SharedStringsTable sst) throws SAXException {

  XMLReader parser = XMLReaderFactory.createXMLReader ( "org.apache.xerces.parsers.SAXParser ");

  ContentHandler handler = new SheetHandler (sst);

  parser.setContentHandler (handler);

  return parser;

  }

  private static class SheetHandler extends DefaultHandler {

  private SharedStringsTable sst;

  private String lastContents;

  private boolean nextIsString;

  private SheetHandler (SharedStringsTable sst) {

  this.sst = sst;

  }

  public void startElement (String uri, String localName, String name, Attributes attributes) throws SAXException {

  if (name.equals ( "c")) {

  System.out.print (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 localName, String name) throws SAXException {

  if (nextIsString) {

  int idx = Integer.parseInt (lastContents);

  lastContents = new XSSFRichTextString (sst.getEntryAt (idx)).toString ();

  nextIsString = false;

  }

  if (name.equals ( "v")) {

  System.out.println (lastContents);

  }

  }

  public void characters (char [] ch, int start, int length) throws SAXException {

  lastContents + = new String (ch, start, length);

  }

  }

  public static void main (String [] args) throws Exception {

  Thread.sleep (5000);

  System.out.println ( "start read");

  for (int i = 0; i < 100; i++) {

  EventModel example = new EventModel ();

  example.processOneSheet ( "D: / test.xlsx ");

  Thread.sleep (1000);

  }

  }

  }

Recommend article

Relate article