目前处理Excel的开源javaAPI主要有两种,一是Jxl(Java Excel API),Jxl只支持Excel2003以下的版本。另外一种是Apache的Jakarta POI,相比于Jxl,POI对微软办公文档的支持更加强大,但是它使用复杂,上手慢。POI可支持更高的Excel版本2007。对Excel的读取,POI有两种模式,一是用户模式,这种方式同Jxl的使用很类似,使用简单,都是将文件一次性读到内存,文件小的时候,没有什么问题,当文件大的时候,就会出现OutOfMemory的内存溢出问题。第二种是事件驱动模式,拿Excel2007来说,其内容采用XML的格式来存储,所以处理excel就是解析XML,而目前使用事件驱动模式解析XML的API是SAX(Simple API for XML),这种模型在读取XML文档时,并没有将整个文档读入内存,而是按顺序将整个文档解析完,在解析过程中,会主动产生事件交给程序中相应的处理函数来处理当前内容。因此这种方式对系统资源要求不高,可以处理海量数据。笔者曾经做过测试,这种方法处理一千万条,每条五列的数据花费大约11分钟。可见处理海量数据的文件事件驱动是一个很好的方式。而本文中用到的AbstractExcel2003Reader、AbstractExcel2007Reader对Excel的读取都是采用这种POI的事件驱动模式。至于Excel的写操作,对较高版本的Excel2007,POI提供了很好的支持,主要流程是第一步构建工作薄和电子表格对象,第二步在一个流中构建文本文件,第三步使用流中产生的数据替换模板中的电子表格。这种方式也可以处理海量数据文件。AbstractExcel2007Writer就是使用这种方式进行写操作。对于写入较低版本的Excel2003,POI使用了用户模式来处理,就是将整个文档加载进内存,如果数据量大的话就会出现内存溢出的问题,Excel2003Writer就是使用这种方式。据笔者的测试,如果数据量大于3万条,每条8列的话,就会报OutOfMemory的错误。Excel2003中每个电子表格的记录数必须在65536以下,否则就会发生异常。目前还没有好的解决方案,建议对于海量数据写入操作,尽量使用Excel2007。
[java] view plaincopy
-
-
-
-
-
- public class Excel2003Reader implements HSSFListener{
- private int minColumns = -1;
- private POIFSFileSystem fs;
- private int lastRowNumber;
- private int lastColumnNumber;
-
-
- private boolean outputFormulaValues = true;
-
-
- private SheetRecordCollectingListener workbookBuildingListener;
-
- private HSSFWorkbook stubWorkbook;
-
-
- private SSTRecord sstRecord;
- private FormatTrackingHSSFListener formatListener;
-
-
- private int sheetIndex = -1;
- private BoundSheetRecord[] orderedBSRs;
- @SuppressWarnings("unchecked")
- private ArrayList boundSheetRecords = new ArrayList();
-
-
- private int nextRow;
- private int nextColumn;
- private boolean outputNextStringRecord;
-
- private int curRow = 0;
-
- private List<String> rowlist = new ArrayList<String>();;
- @SuppressWarnings( "unused")
- private String sheetName;
-
- private IRowReader rowReader;
-
-
- public void setRowReader(IRowReader rowReader){
- this.rowReader = rowReader;
- }
-
-
-
-
-
- public void process(String fileName) throws IOException {
- this.fs = new POIFSFileSystem(new FileInputStream(fileName));
- MissingRecordAwareHSSFListener listener = new MissingRecordAwareHSSFListener(
- this);
- formatListener = new FormatTrackingHSSFListener(listener);
- HSSFEventFactory factory = new HSSFEventFactory();
- HSSFRequest request = new HSSFRequest();
- if (outputFormulaValues) {
- request.addListenerForAllRecords(formatListener);
- } else {
- workbookBuildingListener = new SheetRecordCollectingListener(
- formatListener);
- request.addListenerForAllRecords(workbookBuildingListener);
- }
- factory.processWorkbookEvents(request, fs);
- }
-
-
-
-
- @SuppressWarnings("unchecked")
- public void processRecord(Record record) {
- int thisRow = -1;
- int thisColumn = -1;
- String thisStr = null;
- String value = null;
- switch (record.getSid()) {
- case BoundSheetRecord.sid:
- boundSheetRecords.add(record);
- break;
- case BOFRecord.sid:
- BOFRecord br = (BOFRecord) record;
- if (br.getType() == BOFRecord.TYPE_WORKSHEET) {
-
- if (workbookBuildingListener != null && stubWorkbook == null) {
- stubWorkbook = workbookBuildingListener
- .getStubHSSFWorkbook();
- }
-
- sheetIndex++;
- if (orderedBSRs == null) {
- orderedBSRs = BoundSheetRecord
- .orderByBofPosition(boundSheetRecords);
- }
- sheetName = orderedBSRs[sheetIndex].getSheetname();
- }
- break;
-
- case SSTRecord.sid:
- sstRecord = (SSTRecord) record;
- break;
-
- case BlankRecord.sid:
- BlankRecord brec = (BlankRecord) record;
- thisRow = brec.getRow();
- thisColumn = brec.getColumn();
- thisStr = "";
- rowlist.add(thisColumn, thisStr);
- break;
- case BoolErrRecord.sid:
- BoolErrRecord berec = (BoolErrRecord) record;
- thisRow = berec.getRow();
- thisColumn = berec.getColumn();
- thisStr = berec.getBooleanValue()+"";
- rowlist.add(thisColumn, thisStr);
- break;
-
- case FormulaRecord.sid:
- FormulaRecord frec = (FormulaRecord) record;
- thisRow = frec.getRow();
- thisColumn = frec.getColumn();
- if (outputFormulaValues) {
- if (Double.isNaN(frec.getValue())) {
-
-
- outputNextStringRecord = true;
- nextRow = frec.getRow();
- nextColumn = frec.getColumn();
- } else {
- thisStr = formatListener.formatNumberDateCell(frec);
- }
- } else {
- thisStr = '"' + HSSFFormulaParser.toFormulaString(stubWorkbook,
- frec.getParsedExpression()) + '"';
- }
- rowlist.add(thisColumn,thisStr);
- break;
- case StringRecord.sid:
- if (outputNextStringRecord) {
-
- StringRecord srec = (StringRecord) record;
- thisStr = srec.getString();
- thisRow = nextRow;
- thisColumn = nextColumn;
- outputNextStringRecord = false;
- }
- break;
- case LabelRecord.sid:
- LabelRecord lrec = (LabelRecord) record;
- curRow = thisRow = lrec.getRow();
- thisColumn = lrec.getColumn();
- value = lrec.getValue().trim();
- value = value.equals("")?" ":value;
- this.rowlist.add(thisColumn, value);
- break;
- case LabelSSTRecord.sid:
- LabelSSTRecord lsrec = (LabelSSTRecord) record;
- curRow = thisRow = lsrec.getRow();
- thisColumn = lsrec.getColumn();
- if (sstRecord == null) {
- rowlist.add(thisColumn, " ");
- } else {
- value = sstRecord
- .getString(lsrec.getSSTIndex()).toString().trim();
- value = value.equals("")?" ":value;
- rowlist.add(thisColumn,value);
- }
- break;
- case NumberRecord.sid:
- NumberRecord numrec = (NumberRecord) record;
- curRow = thisRow = numrec.getRow();
- thisColumn = numrec.getColumn();
- value = formatListener.formatNumberDateCell(numrec).trim();
- value = value.equals("")?" ":value;
-
- rowlist.add(thisColumn, value);
- break;
- default:
- break;
- }
-
-
- if (thisRow != -1 && thisRow != lastRowNumber) {
- lastColumnNumber = -1;
- }
-
-
- if (record instanceof MissingCellDummyRecord) {
- MissingCellDummyRecord mc = (MissingCellDummyRecord) record;
- curRow = thisRow = mc.getRow();
- thisColumn = mc.getColumn();
- rowlist.add(thisColumn," ");
- }
-
-
- if (thisRow > -1)
- lastRowNumber = thisRow;
- if (thisColumn > -1)
- lastColumnNumber = thisColumn;
-
-
- if (record instanceof LastCellOfRowDummyRecord) {
- if (minColumns > 0) {
-
- if (lastColumnNumber == -1) {
- lastColumnNumber = 0;
- }
- }
- lastColumnNumber = -1;
-
- rowReader.getRows(sheetIndex,curRow, rowlist);
-
-
- rowlist.clear();
- }
- }
-
- }
[java] view plaincopy
-
-
-
-
-
-
- public class Excel2007Reader extends DefaultHandler {
-
- private SharedStringsTable sst;
-
- private String lastContents;
- private boolean nextIsString;
-
- private int sheetIndex = -1;
- private List<String> rowlist = new ArrayList<String>();
-
- private int curRow = 0;
-
- private int curCol = 0;
-
- private boolean dateFlag;
-
- private boolean numberFlag;
-
- private boolean isTElement;
-
- private IRowReader rowReader;
-
- public void setRowReader(IRowReader rowReader){
- this.rowReader = rowReader;
- }
-
-
-
-
-
-
- public void processOneSheet(String filename,int sheetId) throws Exception {
- OPCPackage pkg = OPCPackage.open(filename);
- XSSFReader r = new XSSFReader(pkg);
- SharedStringsTable sst = r.getSharedStringsTable();
- XMLReader parser = fetchSheetParser(sst);
-
-
- InputStream sheet2 = r.getSheet("rId"+sheetId);
- sheetIndex++;
- InputSource sheetSource = new InputSource(sheet2);
- parser.parse(sheetSource);
- sheet2.close();
- }
-
-
-
-
-
-
- public void process(String filename) throws Exception {
- OPCPackage pkg = OPCPackage.open(filename);
- XSSFReader r = new XSSFReader(pkg);
- SharedStringsTable sst = r.getSharedStringsTable();
- XMLReader parser = fetchSheetParser(sst);
- Iterator<InputStream> sheets = r.getSheetsData();
- while (sheets.hasNext()) {
- curRow = 0;
- sheetIndex++;
- InputStream sheet = sheets.next();
- InputSource sheetSource = new InputSource(sheet);
- parser.parse(sheetSource);
- sheet.close();
- }
- }
-
- public XMLReader fetchSheetParser(SharedStringsTable sst)
- throws SAXException {
- XMLReader parser = XMLReaderFactory
- .createXMLReader("org.apache.xerces.parsers.SAXParser");
- this.sst = sst;
- parser.setContentHandler(this);
- return parser;
- }
-
- public void startElement(String uri, String localName, String name,
- Attributes attributes) throws SAXException {
-
-
- if ("c".equals(name)) {
-
- String cellType = attributes.getValue("t");
- if ("s".equals(cellType)) {
- nextIsString = true;
- } else {
- nextIsString = false;
- }
-
- String cellDateType = attributes.getValue("s");
- if ("1".equals(cellDateType)){
- dateFlag = true;
- } else {
- dateFlag = false;
- }
- String cellNumberType = attributes.getValue("s");
- if("2".equals(cellNumberType)){
- numberFlag = true;
- } else {
- numberFlag = false;
- }
-
- }
-
- if("t".equals(name)){
- isTElement = true;
- } else {
- isTElement = false;
- }
-
-
- lastContents = "";
- }
-
- public void endElement(String uri, String localName, String name)
- throws SAXException {
-
-
-
- if (nextIsString) {
- try {
- int idx = Integer.parseInt(lastContents);
- lastContents = new XSSFRichTextString(sst.getEntryAt(idx))
- .toString();
- } catch (Exception e) {
-
- }
- }
-
- if(isTElement){
- String value = lastContents.trim();
- rowlist.add(curCol, value);
- curCol++;
- isTElement = false;
-
-
- } else if ("v".equals(name)) {
- String value = lastContents.trim();
- value = value.equals("")?" ":value;
-
- if(dateFlag){
- Date date = HSSFDateUtil.getJavaDate(Double.valueOf(value));
- SimpleDateFormat dateFormat = new SimpleDateFormat(
- "dd/MM/yyyy");
- value = dateFormat.format(date);
- }
-
- if(numberFlag){
- BigDecimal bd = new BigDecimal(value);
- value = bd.setScale(3,BigDecimal.ROUND_UP).toString();
- }
- rowlist.add(curCol, value);
- curCol++;
- }else {
-
- if (name.equals("row")) {
- rowReader.getRows(sheetIndex,curRow,rowlist);
- rowlist.clear();
- curRow++;
- curCol = 0;
- }
- }
-
- }
-
- public void characters(char[] ch, int start, int length)
- throws SAXException {
-
- lastContents += new String(ch, start, length);
- }
- }
[java] view plaincopy
- public class ExcelReaderUtil {
-
-
- public static final String EXCEL03_EXTENSION = ".xls";
-
- public static final String EXCEL07_EXTENSION = ".xlsx";
-
-
-
-
-
-
-
-
- public static void readExcel(IRowReader reader,String fileName) throws Exception{
-
- if (fileName.endsWith(EXCEL03_EXTENSION)){
- Excel2003Reader excel03 = new Excel2003Reader();
- excel03.setRowReader(reader);
- excel03.process(fileName);
-
- } else if (fileName.endsWith(EXCEL07_EXTENSION)){
- Excel2007Reader excel07 = new Excel2007Reader();
- excel07.setRowReader(reader);
- excel07.process(fileName);
- } else {
- throw new Exception("文件格式错误,fileName的扩展名只能是xls或xlsx。");
- }
- }
- }
[java] view plaincopy
- public interface IRowReader {
-
-
-
-
-
-
- public void getRows(int sheetIndex,int curRow, List<String> rowlist);
- }
[java] view plaincopy
- public class RowReader implements IRowReader{
-
-
-
-
-
- public void getRows(int sheetIndex, int curRow, List<String> rowlist) {
-
- System.out.print(curRow+" ");
- for (int i = 0; i < rowlist.size(); i++) {
- System.out.print(rowlist.get(i) + " ");
- }
- System.out.println();
- }
-
- }
[java] view plaincopy
- public class Main {
-
- public static void main(String[] args) throws Exception {
- IRowReader reader = new RowReader();
-
- ExcelReaderUtil.readExcel(reader, "F://test07.xlsx");
- }
- }
[java] view plaincopy
- public class Excel2003Writer {
-
-
-
-
- public static void main(String[] args) {
- try{
- System.out.println("开始写入excel2003....");
- writeExcel("tes2003.xls");
- System.out.println("写完xcel2003");
- } catch (IOException e) {
-
- }
- }
-
-
-
-
-
-
-
- public static void writeExcel(String fileName) throws IOException{
-
-
- Workbook wb = new HSSFWorkbook();
-
-
- FileOutputStream fileOut = new FileOutputStream(fileName);
-
- Sheet sheet = wb.createSheet("newsheet");
-
- for(int i=0;i<20000;i++){
- Row row = sheet.createRow(i);
-
- Cell cell = row.createCell(0);
-
- cell.setCellValue(1);
- row.createCell(1).setCellValue(1+i);
- row.createCell(2).setCellValue(true);
- row.createCell(3).setCellValue(0.43d);
- row.createCell(4).setCellValue('d');
- row.createCell(5).setCellValue("");
- row.createCell(6).setCellValue("第七列"+i);
- row.createCell(7).setCellValue("第八列"+i);
- }
- wb.write(fileOut);
- fileOut.close();
- }
-
-
- }
[java] view plaincopy
-
-
-
-
-
- public abstract class AbstractExcel2007Writer {
-
- private SpreadsheetWriter sw;
-
-
-
-
-
-
- public void process(String fileName) throws Exception{
-
- XSSFWorkbook wb = new XSSFWorkbook();
- XSSFSheet sheet = wb.createSheet("sheet1");
-
- String sheetRef = sheet.getPackagePart().getPartName().getName();
-
-
- FileOutputStream os = new FileOutputStream("template.xlsx");
- wb.write(os);
- os.close();
-
-
- File tmp = File.createTempFile("sheet", ".xml");
- Writer fw = new FileWriter(tmp);
- sw = new SpreadsheetWriter(fw);
- generate();
- fw.close();
-
-
- File templateFile = new File("template.xlsx");
- FileOutputStream out = new FileOutputStream(fileName);
- substitute(templateFile, tmp, sheetRef.substring(1), out);
- out.close();
-
- System.gc();
-
- if (templateFile.isFile()&&templateFile.exists()){
- templateFile.delete();
- }
- }
-
-
-
-
-
- public abstract void generate() throws Exception;
-
- public void beginSheet() throws IOException {
- sw.beginSheet();
- }
-
- public void insertRow(int rowNum) throws IOException {
- sw.insertRow(rowNum);
- }
-
- public void createCell(int columnIndex, String value) throws IOException {
- sw.createCell(columnIndex, value, -1);
- }
-
- public void createCell(int columnIndex, double value) throws IOException {
- sw.createCell(columnIndex, value, -1);
- }
-
- public void endRow() throws IOException {
- sw.endRow();
- }
-
- public void endSheet() throws IOException {
- sw.endSheet();
- }
-
-
-
-
-
-
-
-
- private static void substitute(File zipfile, File tmpfile, String entry,
- OutputStream out) throws IOException {
- ZipFile zip = new ZipFile(zipfile);
- ZipOutputStream zos = new ZipOutputStream(out);
-
- @SuppressWarnings("unchecked")
- Enumeration<ZipEntry> en = (Enumeration<ZipEntry>) zip.entries();
- while (en.hasMoreElements()) {
- ZipEntry ze = en.nextElement();
- if (!ze.getName().equals(entry)) {
- zos.putNextEntry(new ZipEntry(ze.getName()));
- InputStream is = zip.getInputStream(ze);
- copyStream(is, zos);
- is.close();
- }
- }
- zos.putNextEntry(new ZipEntry(entry));
- InputStream is = new FileInputStream(tmpfile);
- copyStream(is, zos);
- is.close();
- zos.close();
- }
-
- private static void copyStream(InputStream in, OutputStream out)
- throws IOException {
- byte[] chunk = new byte[1024];
- int count;
- while ((count = in.read(chunk)) >= 0) {
- out.write(chunk, 0, count);
- }
- }
-
-
-
-
-
- public static class SpreadsheetWriter {
- private final Writer _out;
- private int _rownum;
- private static String LINE_SEPARATOR = System.getProperty("line.separator");
-
- public SpreadsheetWriter(Writer out) {
- _out = out;
- }
-
- public void beginSheet() throws IOException {
- _out.write("<?xml version=\"1.0\" encoding=\"GB2312\"?>"
- + "<worksheet xmlns=\"http://schemas.openxmlformats.org/spreadsheetml/2006/main\">");
- _out.write("<sheetData>"+LINE_SEPARATOR);
- }
-
- public void endSheet() throws IOException {
- _out.write("</sheetData>");
- _out.write("</worksheet>");
- }
-
-
-
-
-
-
- public void insertRow(int rownum) throws IOException {
- _out.write("<row r=\"" + (rownum + 1) + "\">"+LINE_SEPARATOR);
- this._rownum = rownum;
- }
-
-
-
-
- public void endRow() throws IOException {
- _out.write("</row>"+LINE_SEPARATOR);
- }
-
-
-
-
-
-
-
-
- public void createCell(int columnIndex, String value, int styleIndex)
- throws IOException {
- String ref = new CellReference(_rownum, columnIndex)
- .formatAsString();
- _out.write("<c r=\"" + ref + "\" t=\"inlineStr\"");
- if (styleIndex != -1)
- _out.write(" s=\"" + styleIndex + "\"");
- _out.write(">");
- _out.write("<is><t>"+XMLEncoder.encode(value)+"</t></is>");
- _out.write("</c>");
- }
-
- public void createCell(int columnIndex, String value)
- throws IOException {
- createCell(columnIndex, value, -1);
- }
-
- public void createCell(int columnIndex, double value, int styleIndex)
- throws IOException {
- String ref = new CellReference(_rownum, columnIndex)
- .formatAsString();
- _out.write("<c r=\"" + ref + "\" t=\"n\"");
- if (styleIndex != -1)
- _out.write(" s=\"" + styleIndex + "\"");
- _out.write(">");
- _out.write("<v>" + value + "</v>");
- _out.write("</c>");
- }
-
- public void createCell(int columnIndex, double value)
- throws IOException {
- createCell(columnIndex, value, -1);
- }
-
- public void createCell(int columnIndex, Calendar value, int styleIndex)
- throws IOException {
- createCell(columnIndex, DateUtil.getExcelDate(value, false),
- styleIndex);
- }
- }
- }
[java] view plaincopy
- public class Excel2007WriterImpl extends AbstractExcel2007Writer{
-
-
-
-
-
-
- public static void main(String[] args) throws Exception {
-
- System.out.println("............................");
- long start = System.currentTimeMillis();
-
- AbstractExcel2007Writer excel07Writer = new Excel2007WriterImpl();
-
- excel07Writer.process("F://test07.xlsx");
- long end = System.currentTimeMillis();
- System.out.println("....................."+(end-start)/1000);
- }
-
-
-
-
-
-
- @Override
- public void generate()throws Exception {
-
- beginSheet();
- for (int rownum = 0; rownum < 100; rownum++) {
-
- insertRow(rownum);
-
- createCell(0, "中国<" + rownum + "!");
- createCell(1, 34343.123456789);
- createCell(2, "23.67%");
- createCell(3, "12:12:23");
- createCell(4, "2010-10-11 12:12:23");
- createCell(5, "true");
- createCell(6, "false");
-
-
- endRow();
- }
-
- endSheet();
- }
-
- }
[java] view plaincopy
- public class XMLEncoder {
-
- private static final String[] xmlCode = new String[256];
-
- static {
-
- xmlCode['\''] = "'";
- xmlCode['\"'] = """;
- xmlCode['&'] = "&";
- xmlCode['<'] = "<";
- xmlCode['>'] = ">";
- }
-
-
-
-
-
-
-
-
-
- public static String encode(String string) {
- if (string == null) return "";
- int n = string.length();
- char character;
- String xmlchar;
- StringBuffer buffer = new StringBuffer();
-
- for (int i = 0; i < n; i++) {
- character = string.charAt(i);
-
- try {
- xmlchar = xmlCode[character];
- if (xmlchar == null) {
- buffer.append(character);
- } else {
- buffer.append(xmlCode[character]);
- }
- } catch (ArrayIndexOutOfBoundsException aioobe) {
- buffer.append(character);
- }
- }
- return buffer.toString();
- }
-
- }