1. 程式人生 > 實用技巧 >百萬資料excel報表讀取

百萬資料excel報表讀取

package com.*.poi.test;

import cn.*.poi.handler.SheetHandler;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.openxml4j.opc.PackageAccess;
import org.apache.poi.xssf.eventusermodel.XSSFReader;
import org.apache.poi.xssf.eventusermodel.XSSFSheetXMLHandler;
import org.apache.poi.xssf.model.SharedStringsTable; import org.apache.poi.xssf.model.StylesTable; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.xml.sax.InputSource; import org.xml.sax.XMLReader; import org.xml.sax.helpers.XMLReaderFactory; import java.io.InputStream; import java.util.Iterator;
/** * 使用事件模型解析百萬資料excel報表 */ public class PoiTest { public static void main(String[] args) throws Exception { String path = "C:\\Users\\\Administrator\\Desktop\\demo.xlsx"; //1.根據excel報表獲取OPCPackage OPCPackage opcPackage = OPCPackage.open(path, PackageAccess.READ);
//2.建立XSSFReader XSSFReader reader = new XSSFReader(opcPackage); //3.獲取SharedStringTable物件 SharedStringsTable table = reader.getSharedStringsTable(); //4.獲取styleTable物件 StylesTable stylesTable = reader.getStylesTable(); //5.建立Sax的xmlReader物件 XMLReader xmlReader = XMLReaderFactory.createXMLReader(); //6.註冊事件處理器(測試時這裡是poi4.0.1的構造方法,3.14時還沒有此構造方法) XSSFSheetXMLHandler xmlHandler = new XSSFSheetXMLHandler(stylesTable,table,new SheetHandler(),false); xmlReader.setContentHandler(xmlHandler); //7.逐行讀取 XSSFReader.SheetIterator sheetIterator = (XSSFReader.SheetIterator) reader.getSheetsData(); while (sheetIterator.hasNext()) { InputStream stream = sheetIterator.next(); //每一個sheet的流資料 InputSource is = new InputSource(stream); xmlReader.parse(is); } } }

package com.*.poi.handler;

import cn.*.poi.entity.Book;
import org.apache.poi.xssf.eventusermodel.XSSFSheetXMLHandler;
import org.apache.poi.xssf.usermodel.XSSFComment;

/**
 * 自定義的事件處理器
 *  處理每一行資料讀取
 *      實現介面
 */
public class SheetHandler implements XSSFSheetXMLHandler.SheetContentsHandler {

    private Book entity;
    /**
     * 當開始解析某一行的時候觸發
     *      i:行索引
     */
    @Override
    public void startRow(int i) {
        //例項化物件
        if(i>0) {
            entity = new Book();
        }
    }

    /**
     * 當結束解析某一行的時候觸發
     *      i:行索引
     */
    @Override
    public void endRow(int i) {
        //TODO 進行業務操作儲存物件
        System.out.println(entity);
    }

    /**
     * 對行中的每一個表格進行處理
     *      cellReference: 單元格名稱
     *      value:資料
     *      xssfComment:批註
     */
    @Override
    public void cell(String cellReference, String value, XSSFComment xssfComment) {
        //物件屬性賦值
        if(entity != null) {
            String pix = cellReference.substring(0,1);
            switch (pix) {
                case "A":
                    entity.setName(value);
                    break;
                case "B":
                    entity.setNote(value);
                    break;
                default:
                    break;
            }
        }
    }
}

package com.*.poi.entity;

public class Book {
    
    private String name;
    private String note;
    
    public String getName(){
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    
    public String getNote() {
        return note;
    }

    public void setNote(String note) {
        this.note = note;
    }
}