Apache POI组件使用eventusermodel模式读取Excel文档内容
package com.test;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.util.LinkedList;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.openxml4j.opc.PackageAccess;
import org.apache.poi.ss.usermodel.BuiltinFormats;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.xssf.eventusermodel.ReadOnlySharedStringsTable;
import org.apache.poi.xssf.eventusermodel.XSSFReader;
import org.apache.poi.xssf.model.StylesTable;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.xml.sax.Attributes;
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 Test {
public static void main(String[] args) {
long a=System.currentTimeMillis();
ExecThread thread = new ExecThread();
thread.start();
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Main ... "+Thread.currentThread().getName());
System.out.println("执行耗时 :"+(System.currentTimeMillis()-a)/1000f+" 秒 ");
}
public static class ExecThread extends Thread{
public ExecThread(){}
/* (non-Javadoc)
* @see java.lang.Thread#run()
*/
@Override
public void run() {
try {
FileWriter fileWriter = new FileWriter("D:/test-out.txt");
String excelPath = "D:/test.xlsx";
new ExcelHandler(fileWriter,excelPath,"1","").start();
System.out.println("ExecThread ... "+Thread.currentThread().getName());
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static class ExcelHandler extends DefaultHandler{
private final String excelPath;
private final String sheetIndex;
private final String nullString;
/**
* 从<c />标签得到的单元格格式,获取当前单元格数值需要用到
*/
private enum XSSFDataType {
BOOL,
ERROR,
FORMULA,
INLINESTR,
SSTINDEX,
NUMBER,
}
private XSSFReader xssfReader;
private ReadOnlySharedStringsTable sst;
private StylesTable stylesTable;
private boolean vIsOpen = false;
private StringBuffer value = new StringBuffer();
protected XSSFDataType nextDataType;
// 当前遍历的Excel单元格列索引
protected int thisColumnIndex = -1;
// The last column printed to the output stream
protected int lastColumnIndex = -1;
protected int maxColumnCount = -1;
protected int formatIndex;
protected String formatString;
private final DataFormatter formatter = new DataFormatter();
private List<String> rowDatas = null;
private String targetFile;
private FileWriter writer;
public ExcelHandler(String targetFile, String excelPath,
String sheetIndex, String nullString){
// this.writer = writer;
this.targetFile = targetFile;
this.excelPath = excelPath;
this.sheetIndex = sheetIndex;
this.nullString = nullString;
}
public ExcelHandler(FileWriter writer, String excelPath,
String sheetIndex, String nullString){
this.writer = writer;
this.excelPath = excelPath;
this.sheetIndex = sheetIndex;
this.nullString = nullString;
}
/**
* 初始化
*/
public void start(){
try {
OPCPackage op = OPCPackage.open(this.excelPath,PackageAccess.READ);
this.xssfReader = new XSSFReader(op);
this.sst = new ReadOnlySharedStringsTable(op);
this.stylesTable = xssfReader.getStylesTable();
// 开始解析
parseXmlContent(new InputSource(getOneSheetStream(sheetIndex)));
} catch (Exception e) {
e.printStackTrace();
}
}
/* (non-Javadoc)
* @see org.xml.sax.helpers.DefaultHandler#startDocum