欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java poi sax方式處理大數(shù)據(jù)量excel文件

 更新時(shí)間:2020年01月08日 15:06:15   作者:soft_xiang  
這篇文章主要介紹了java poi sax方式處理大數(shù)據(jù)量excel文件,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

系統(tǒng)需要用到一個(gè)導(dǎo)入excel文件的功能,使用poi組件常規(guī)方式讀取excel時(shí),內(nèi)存耗盡,OutOfMemoryError,或者讀取非常慢
所以寫了一個(gè)工具類,使用poi sax方式讀取excel,速度快很多,內(nèi)存消耗可以接受。

測試結(jié)果如下:
.xlsx文件,35M大小,總4個(gè)sheel,
只讀取第一個(gè),37434行,54列

總行數(shù):37434
讀取耗時(shí):39秒
打印耗時(shí):17秒

主要代碼如下:

ExcelUtils.class 主入口

package com.xxx.bi.utils.excel;

import java.util.List;
import java.util.Objects;

import org.apache.commons.lang3.StringUtils;

import com.google.common.collect.Lists;

public class ExcelUtils {
  /** logger日志. */
  // public static final Logger LOGGER = Logger.getLogger(ExcelUtils.class);

  public ExcelUtils() {
  }

  /**
   * 獲取excel的表頭
   * 
   * @param filePath
   *      文件路徑
   * @param headerNum
   *      表頭所在行數(shù)
   * @return
   */
  public static List<String> getHeader(String filePath, int headerNum) {
    if (StringUtils.isBlank(filePath)) {
      throw new IllegalArgumentException("傳入文件路徑不能為空");
    }
    if (Objects.isNull(headerNum) || headerNum < 1) {
      headerNum = 1;
    }
    try {
      return LargeExcelFileReadUtil.getRowFromSheetOne(filePath, headerNum);
    } catch (Exception e) {
      // LOGGER.info("獲取excel[" + filePath + "]表頭失敗,原因:", e);
      e.printStackTrace();
    }
    return Lists.newArrayList();
  }

  /**
   * 獲取excel的所有數(shù)據(jù)<br/>
   * 所有數(shù)據(jù)類型都是String<br/>
   * 會以第一行數(shù)據(jù)的列數(shù)為總列數(shù),所以第一行的數(shù)據(jù)必須都不為空,否則可能出java.lang.IndexOutOfBoundsException
   * 
   * @param filePath
   *      文件路徑
   * @param headerNum
   *      表頭所在行數(shù)
   * @return
   */
  public static List<List<String>> getAllData(String filePath) {
    if (StringUtils.isBlank(filePath)) {
      throw new IllegalArgumentException("傳入文件路徑不能為空");
    }
    try {
      return LargeExcelFileReadUtil.getRowsFromSheetOne(filePath);
    } catch (Exception e) {
      // LOGGER.info("獲取excel[" + filePath + "]表頭失敗,原因:", e);
      e.printStackTrace();
    }
    return Lists.newArrayList();
  }

  public static void main(String[] args) {
    long start = System.currentTimeMillis();
    String filepath = "C:/Users/Administrator/Desktop/05-作業(yè)調(diào)配表 -快遞.xlsx";
    // List<String> result = ExcelUtils.getHeader(filepath, 1);
    // for (String col : result) {
    // System.out.println(col);
    // }

    List<List<String>> result = ExcelUtils.getAllData(filepath);
    long end = System.currentTimeMillis();
    for (List<String> list : result) {
      System.out.println(list.toString());
    }
    long end1 = System.currentTimeMillis();
    try {
      Thread.sleep(1000l);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    System.err.println("總行數(shù):" + result.size());
    System.err.println(("讀取耗時(shí):" + (end - start) / 1000) + "秒");
    System.err.println(("打印耗時(shí):" + (end1 - end) / 1000) + "秒");
  }
}

LargeExcelFileReadUtil.class 真正的工具類

package com.xxx.bi.utils.excel;

import java.io.InputStream;
import java.util.List;
import java.util.Objects;

import org.apache.log4j.Logger;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xssf.eventusermodel.XSSFReader;
import org.apache.poi.xssf.model.SharedStringsTable;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;

public class LargeExcelFileReadUtil {
  /** logger日志. */
  public static final Logger LOGGER = Logger.getLogger(LargeExcelFileReadUtil.class);

  // 處理一個(gè)sheet
  public static List<String> getRowFromSheetOne(String filename, Integer rowNum) throws Exception {
    InputStream inputStream = null;
    OPCPackage pkg = null;
    SingleRowHandler singleRowHandler = null;
    try {
      pkg = OPCPackage.open(filename);
      XSSFReader r = new XSSFReader(pkg);
      SharedStringsTable sst = r.getSharedStringsTable();
      singleRowHandler = new SingleRowHandler(sst, rowNum);
      XMLReader parser = XMLReaderFactory.createXMLReader("com.sun.org.apache.xerces.internal.parsers.SAXParser");
      parser.setContentHandler(singleRowHandler);
      inputStream = r.getSheet("rId1");
      InputSource sheetSource = new InputSource(inputStream);
      parser.parse(sheetSource);
      return singleRowHandler.getRow();
    } catch (Exception e) {
      String message = e.getMessage();
      if (Objects.nonNull(rowNum) && Objects.nonNull(singleRowHandler)
          && SingleRowHandler.FINISH_ROW_MESSAGE.equalsIgnoreCase(message)) {
        // 獲取某一行數(shù)據(jù)完成 ,暫時(shí)不知道怎么能終止excel解析,直接拋出了異常,實(shí)際是成功的
        return singleRowHandler.getRow();
      }
      throw e;
    } finally {
      if (Objects.nonNull(pkg)) {
        pkg.close();
      }
      if (Objects.nonNull(inputStream)) {
        inputStream.close();
      }
    }
  }

  // 處理一個(gè)sheet
  public static List<List<String>> getRowsFromSheetOne(String filename) throws Exception {
    InputStream inputStream = null;
    OPCPackage pkg = null;
    MultiRowHandler multiRowHandler = null;
    try {
      pkg = OPCPackage.open(filename);
      XSSFReader r = new XSSFReader(pkg);
      SharedStringsTable sst = r.getSharedStringsTable();
      multiRowHandler = new MultiRowHandler(sst);
      XMLReader parser = XMLReaderFactory.createXMLReader("com.sun.org.apache.xerces.internal.parsers.SAXParser");
      parser.setContentHandler(multiRowHandler);
      inputStream = r.getSheet("rId1");
      InputSource sheetSource = new InputSource(inputStream);
      parser.parse(sheetSource);
      return multiRowHandler.getRows();
    } catch (Exception e) {
      throw e;
    } finally {
      if (Objects.nonNull(pkg)) {
        pkg.close();
      }
      if (Objects.nonNull(inputStream)) {
        inputStream.close();
      }
    }
  }
}

SingleRowHandler.class 當(dāng)行處理類,可以只獲取表頭或表格中的某一行數(shù)據(jù)

package com.xxx.bi.utils.excel;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.regex.Pattern;

import org.apache.poi.xssf.model.SharedStringsTable;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class SingleRowHandler extends DefaultHandler {
  public final static String FINISH_ROW_MESSAGE = "row data process finish";

  private Integer rowNum = null;// rowNum不為空時(shí)則標(biāo)示只需要獲取這一行的數(shù)據(jù)
  private int curRowNum = 1;
  private String cellType = "";
  private SharedStringsTable sst;
  private String lastContents;
  private boolean nextIsString;
  private String cellPosition;
  private List<String> row = new ArrayList<>();

  public List<String> getRow() {
    return row;
  }

  public SingleRowHandler(SharedStringsTable sst, Integer rowNum) {
    this.sst = sst;
    this.rowNum = rowNum;
  }

  public void startElement(String uri, String localName, String name, Attributes attributes) throws SAXException {
    if (name.equals("c")) {
      cellPosition = attributes.getValue("r");
      // 這是一個(gè)新行
      if (Pattern.compile("^A[0-9]+$").matcher(cellPosition).find()) {
        curRowNum = Integer.valueOf(cellPosition.substring(1));
      }
      cellType = "";
      cellType = attributes.getValue("t");
      if ("s".equals(cellType)) {
        nextIsString = true;
      } else {
        nextIsString = false;
      }
    }
    // 清楚緩存內(nèi)容
    lastContents = "";
    if (Objects.nonNull(rowNum) && curRowNum > rowNum) {
      // 獲取某一行數(shù)據(jù)完成 ,暫時(shí)不知道怎么能終止excel解析,直接拋出了異常,實(shí)際是成功的
      throw new SAXException(FINISH_ROW_MESSAGE);
    }
  }

  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")) {
      if (Objects.isNull(rowNum) || rowNum == curRowNum) {
        row.add(lastContents);
      }
    }
  }

  public void characters(char[] ch, int start, int length) throws SAXException {
    lastContents += new String(ch, start, length);
  }
}

MultiRowHandler.class 獲取excel所有行的數(shù)據(jù)

package com.xxx.bi.utils.excel;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.xssf.model.SharedStringsTable;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

/**
 * 獲取完整excel數(shù)據(jù)的handler<br/>
 * 
 * @author Administrator
 *
 */
public class MultiRowHandler extends DefaultHandler {
  private int curRowNum = 0;// 行號,從1開始
  private int curColIndex = -1;// 列索引,從0開始
  private int colCnt = 0;// 列數(shù),取第一行列數(shù)做為列總數(shù)
  private String cellType = "";
  private SharedStringsTable sst;
  private String lastContents;
  private boolean nextIsString;
  private String cellPosition;
  private List<String> head = null;
  private List<String> curRowData = null;
  private boolean curRowIsBlank = true;// 當(dāng)前是個(gè)空行
  private List<List<String>> rows = new ArrayList<>();

  public List<List<String>> getRows() {
    return rows;
  }

  public MultiRowHandler(SharedStringsTable sst) {
    this.sst = sst;
  }

  @Override
  public void startElement(String uri, String localName, String name, Attributes attributes) throws SAXException {
    if (name.equals("c")) {
      cellPosition = attributes.getValue("r");
      curColIndex = getColIndex(cellPosition);
      // 這是一個(gè)新行
      if (isNewRow(cellPosition)) {
        curRowNum = getRowNum(cellPosition);
        if (2 == curRowNum && Objects.nonNull(curRowData)) {
          head = curRowData;
          colCnt = head.size();
        }
        curRowData = getBlankRow(colCnt);
      }
      cellType = "";
      cellType = attributes.getValue("t");
      if ("s".equals(cellType)) {
        nextIsString = true;
      } else {
        nextIsString = false;
      }
    }
    // 清楚緩存內(nèi)容
    lastContents = "";
  }

  private boolean isNewRow(String cellPosition) {
    // 坐標(biāo)以A開頭,后面跟數(shù)字 或者坐標(biāo)行和當(dāng)前行不一致的
    boolean newRow = Pattern.compile("^A[0-9]+$").matcher(cellPosition).find();
    if (!newRow) {
      int cellRowNum = getRowNum(cellPosition);
      newRow = (cellRowNum != curRowNum);
    }
    return newRow;
  }

  /**
   * 根據(jù)列坐標(biāo)獲取行號,從1開始,返回0時(shí)標(biāo)示出錯(cuò)
   * 
   * @param cellPosition
   *      列坐標(biāo),為A1,B23等
   * @return 行號,從1開始,返回0是為失敗
   */
  private static int getRowNum(String cellPosition) {
    String strVal = Pattern.compile("[^0-9]").matcher(cellPosition).replaceAll("").trim();// 獲取坐標(biāo)中的數(shù)字
    if (StringUtils.isNotBlank(strVal)) {
      return Integer.valueOf(strVal);
    }
    return 0;
  }

  /**
   * 根據(jù)列坐標(biāo)返回當(dāng)前列索引,從0開始,返回-1時(shí)標(biāo)示出錯(cuò)<br/>
   * A1->0; B1->1...AA1->26
   * 
   * @param cellPosition
   *      列坐標(biāo),為A1,B23等
   * @return 列索引,從0開始,返回-1是為失敗,A1->0; B1->1...AA1->26
   */
  private static int getColIndex(String cellPosition) {
    int index = -1;
    int num = 65;// A的Unicode碼
    int length = cellPosition.length();
    for (int i = 0; i < length; i++) {
      char c = cellPosition.charAt(i);
      if (Character.isDigit(c)) {
        break;// 確定指定的char值是否為數(shù)字
      }
      index = (index + 1) * 26 + (int) c - num;
    }
    return index;
  }

  /**
   * 返回一個(gè)全部為空字符串的空行
   * 
   * @param cnt
   * @return
   */
  private List<String> getBlankRow(int cnt) {
    List<String> result = new ArrayList<>(cnt);
    for (int i = 0; i < cnt; i++) {
      result.add(i, "");
    }
    curRowIsBlank = true;
    return result;
  }

  @Override
  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(MessageFormat.format("當(dāng)前列定位:{0},當(dāng)前行:{1},當(dāng)前列:{2},當(dāng)前值:{3}",
      // cellPosition, curRowNum,
      // curColIndex, lastContents));
      if (Objects.isNull(head)) {
        curRowData.add(lastContents);
      } else {
        curRowData.set(curColIndex, lastContents);
      }
      curRowIsBlank = false;
      // 這是一個(gè)新行
      if (isNewRow(cellPosition)) {
        if (Objects.nonNull(curRowData)) {
          if (curRowIsBlank) {
            curRowData.clear();// 如果當(dāng)前行是空行,則清空當(dāng)前行數(shù)據(jù)
          }
          rows.add(curRowData);
        }
      }

    }
  }

  @Override
  public void endDocument() throws SAXException {
    if (Objects.nonNull(curRowData) && !curRowIsBlank) {
      rows.add(curRowData);// 最后一行在上面不好加入,最后一行全是空行的不加入
    }
    super.endDocument();
  }

  @Override
  public void characters(char[] ch, int start, int length) throws SAXException {
    lastContents += new String(ch, start, length);
  }

  @Override
  public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {
    lastContents += "";
  }

  public static void main(String[] args) {
    System.out.println(getColIndex("BC2"));
  }
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java POI實(shí)現(xiàn)將導(dǎo)入Excel文件的示例代碼

    Java POI實(shí)現(xiàn)將導(dǎo)入Excel文件的示例代碼

    這篇文章主要介紹了Java POI實(shí)現(xiàn)將導(dǎo)入Excel文件的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2019-02-02
  • Java基礎(chǔ)教程之實(shí)現(xiàn)接口

    Java基礎(chǔ)教程之實(shí)現(xiàn)接口

    這篇文章主要介紹了Java基礎(chǔ)教程之實(shí)現(xiàn)接口,也可以說是實(shí)施接口,因?yàn)榻涌谥皇嵌x,最終要實(shí)現(xiàn)它,本文就專門講解接口的實(shí)現(xiàn),需要的朋友可以參考下
    2014-08-08
  • Java中Switch用法代碼示例

    Java中Switch用法代碼示例

    這篇文章主要介紹了Java中Switch用法代碼示例,向大家分享了兩段代碼,具有一定參考價(jià)值,需要的朋友可以了解下。
    2017-11-11
  • 常見的排序算法,一篇就夠了

    常見的排序算法,一篇就夠了

    這篇文章主要介紹了一些常用排序算法整理,插入排序算法、直接插入排序、希爾排序、選擇排序、冒泡排序等排序,需要的朋友可以參考下
    2021-07-07
  • 深入了解Java Object類的使用

    深入了解Java Object類的使用

    java繼承中說到的Object類是java中一個(gè)特殊的類,所有的類都是直接或者間接的繼承自O(shè)bject類。本文就和大家詳細(xì)講講Java Object類的使用,感興趣的可以了解一下
    2022-07-07
  • 簡述JAVA中堆內(nèi)存與棧內(nèi)存的區(qū)別

    簡述JAVA中堆內(nèi)存與棧內(nèi)存的區(qū)別

    這篇文章主要介紹了JAVA中堆內(nèi)存與棧內(nèi)存的區(qū)別,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-07-07
  • Netty分布式Future與Promise執(zhí)行回調(diào)相關(guān)邏輯剖析

    Netty分布式Future與Promise執(zhí)行回調(diào)相關(guān)邏輯剖析

    這篇文章主要為大家介紹了Netty分布式Future與Promise執(zhí)行回調(diào)相關(guān)邏輯剖析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步
    2022-03-03
  • 淺談Java成員變量與屬性的區(qū)別(簡單最易懂的解釋)

    淺談Java成員變量與屬性的區(qū)別(簡單最易懂的解釋)

    下面小編就為大家?guī)硪黄獪\談Java成員變量與屬性的區(qū)別(簡單最易懂的解釋)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-05-05
  • JavaWeb項(xiàng)目實(shí)現(xiàn)文件上傳動(dòng)態(tài)顯示進(jìn)度實(shí)例

    JavaWeb項(xiàng)目實(shí)現(xiàn)文件上傳動(dòng)態(tài)顯示進(jìn)度實(shí)例

    本篇文章主要介紹了JavaWeb項(xiàng)目實(shí)現(xiàn)文件上傳動(dòng)態(tài)顯示進(jìn)度實(shí)例,具有一定的參考價(jià)值,有興趣的可以了解一下。
    2017-04-04
  • Java Swagger使用教程

    Java Swagger使用教程

    Swagger是一個(gè)規(guī)范和完整的框架,用于生成、描述、調(diào)用和可視化 Restful 風(fēng)格的 Web 服務(wù)。總體目標(biāo)是使客戶端和文件系統(tǒng)作為服務(wù)器以同樣的速度來更新。文件的方法、參數(shù)和模型緊密集成到服務(wù)器端的代碼,允許API來始終保持同步
    2022-07-07

最新評論