-
엑셀 SAX 파싱 예제IT, 프로그래밍/Spring 2018. 7. 30. 20:5912345678910111213<!-- https://mvnrepository.com/artifact/org.apache.poi/poi --><dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>3.17</version></dependency><!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml --><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>3.17</version></dependency>
cs maven에 아파치 poi 의존성 추가
밑은 공식 예제
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163/*If memory footprint is an issue, then for XSSF, you can get at the underlying XML data, and process it yourself. This is intended for intermediate developers who are willing to learn a little bit of low level structure of .xlsx files, and who are happy processing XML in java. Its relatively simple to use, but requires a basic understanding of the file structure. The advantage provided is that you can read a XLSX file with a relatively small memory footprint.One important thing to note with the basic Event API is that it triggers events only for things actually stored within the file. With the XLSX file format, it is quite common for things that have yet to be edited to simply not exist in the file. This means there may well be apparent "gaps" in the record stream, which you need to work around.To use this API you construct an instance of org.apache.poi.xssf.eventmodel.XSSFReader. This will optionally provide a nice interace on the shared strings table, and the styles. It provides methods to get the raw xml data from the rest of the file, which you will then pass to SAX.This example shows how to get at a single known sheet, or at all sheets in the file. It is based on the example in svn src/examples/src/org/apache/poi/xssf/eventusermodel/exmaples/FromHowTo.java*/import java.io.InputStream;import java.util.Iterator;import java.util.LinkedHashMap;import java.util.Map;import javax.xml.parsers.ParserConfigurationException;import org.apache.poi.openxml4j.opc.OPCPackage;import org.apache.poi.openxml4j.opc.PackageAccess;import org.apache.poi.util.SAXHelper;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;/*** XSSF and SAX (Event API) basic example.* See {@link XLSX2CSV} for a fuller example of doing* XSLX processing with the XSSF Event code.*/public class FromHowTo {public void processFirstSheet(String filename) throws Exception {try (OPCPackage pkg = OPCPackage.open(filename, PackageAccess.READ)) {XSSFReader r = new XSSFReader(pkg);SharedStringsTable sst = r.getSharedStringsTable();XMLReader parser = fetchSheetParser(sst);// process the first sheettry (InputStream sheet = r.getSheetsData().next()) {InputSource sheetSource = new InputSource(sheet);parser.parse(sheetSource);}}}public void processAllSheets(String filename) throws Exception {try (OPCPackage pkg = OPCPackage.open(filename, PackageAccess.READ)) {XSSFReader r = new XSSFReader(pkg);SharedStringsTable sst = r.getSharedStringsTable();XMLReader parser = fetchSheetParser(sst);Iterator<InputStream> sheets = r.getSheetsData();while (sheets.hasNext()) {System.out.println("Processing new sheet:\n");try (InputStream sheet = sheets.next()) {InputSource sheetSource = new InputSource(sheet);parser.parse(sheetSource);}System.out.println("");}}}public XMLReader fetchSheetParser(SharedStringsTable sst) throws SAXException, ParserConfigurationException {XMLReader parser = SAXHelper.newXMLReader();ContentHandler handler = new SheetHandler(sst);parser.setContentHandler(handler);return parser;}/*** See org.xml.sax.helpers.DefaultHandler javadocs*/private static class SheetHandler extends DefaultHandler {private final SharedStringsTable sst;private String lastContents;private boolean nextIsString;private boolean inlineStr;private final LruCache<Integer,String> lruCache = new LruCache<>(50);private static class LruCache<A,B> extends LinkedHashMap<A, B> {private final int maxEntries;public LruCache(final int maxEntries) {super(maxEntries + 1, 1.0f, true);this.maxEntries = maxEntries;}@Overrideprotected boolean removeEldestEntry(final Map.Entry<A, B> eldest) {return super.size() > maxEntries;}}private SheetHandler(SharedStringsTable sst) {this.sst = sst;}@Overridepublic void startElement(String uri, String localName, String name,Attributes attributes) throws SAXException {// c => cellif(name.equals("c")) {// Print the cell referenceSystem.out.print(attributes.getValue("r") + " - ");// Figure out if the value is an index in the SSTString cellType = attributes.getValue("t");nextIsString = cellType != null && cellType.equals("s");inlineStr = cellType != null && cellType.equals("inlineStr");}// Clear contents cachelastContents = "";}@Overridepublic void endElement(String uri, String localName, String name)throws SAXException {// Process the last contents as required.// Do now, as characters() may be called more than onceif(nextIsString) {Integer idx = Integer.valueOf(lastContents);lastContents = lruCache.get(idx);if (lastContents == null && !lruCache.containsKey(idx)) {lastContents = new XSSFRichTextString(sst.getEntryAt(idx)).toString();lruCache.put(idx, lastContents);}nextIsString = false;}// v => contents of a cell// Output after we've seen the string contentsif(name.equals("v") || (inlineStr && name.equals("c"))) {System.out.println(lastContents);}}@Overridepublic void characters(char[] ch, int start, int length) throws SAXException { // NOSONARlastContents += new String(ch, start, length);}}}cs 'IT, 프로그래밍 > Spring' 카테고리의 다른 글
Daum map API, 클러스터링과 인포윈도우 생성하기 (3) 2018.08.02 Security 설정 후 post 전송 시 403 에러가 뜨는 경우 (0) 2018.08.02 Mybatis $과 #의 차이 (0) 2018.07.29 There is no getter for property named ~~ 에러 (0) 2018.07.29 Gson 사용 시 꼭 알아야 할 기본 지식 (0) 2018.07.20