首页 > 社交 > 科普中国

POI导入导出百万级数据Excel

常驻编辑 科普中国 2022-05-17 表头   空行   级数   扩展名   字符串   标识   索引   类型   文件   内容   数据

一、导入pom依赖Evj拜客生活常识网

4.0.0


    org.apache.poi
    poi
    ${poi-version}



    org.apache.poi
    poi-ooxml
    ${poi-version}



    org.apache.poi
    poi-scratchpad
    ${poi-version}



    org.apache.poi
    poi-ooxml-schemas
    ${poi-version}

二、导入Evj拜客生活常识网

1、导入工具类Evj拜客生活常识网

import com.base.infrastructure.exception.ServiceException;
import com.base.infrastructure.utility.MapUtility;
import com.base.infrastructure.utility.ServiceCheckUtility;
import com.base.infrastructure.utility.StringUtility;
import com.base.infrastructure.utility.excel.analysis.ExcelXlsReader;
import com.base.infrastructure.utility.excel.analysis.ExcelXlsxReader;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * excel2003、2007大数据量解析
 */
public class BigExcelReaderUtil {

    //excel2003扩展名
    private static final String XLS_LOWER_CASE = "xls";

    private static final String XLSX_LOWER_CASE = "xlsx";

    //excel2007扩展名
    private static final String XLS_UPPER_CASE = "XLS";

    private static final String XLSX_UPPER_CASE = "XLSX";

    /**
     * 根据文件路径解析
     *
     * @param path
     * @return
     * @throws Exception
     */
    public static Map readExcelPath(String path, int headerIncex, int dataIndex)
            throws Exception {
        //解析数据返回map
        Map map = new HashMap<>();
        //表头内容
        List header = new ArrayList<>();
        //数据内容
        //List> data = new ArrayList<>();
        Map>> listMap = new HashMap<>();

        //处理excel2003文件
        if (path.endsWith(XLS_LOWER_CASE) || path.endsWith(XLS_UPPER_CASE)) {
            ExcelXlsReader excelXls = new ExcelXlsReader() {
                @Override
                public void sendHeaderRows(String filePath, String sheetName, int sheetIndex, int curRow, List cellList) {
                    //ServiceCheckUtility.errorCheck(sheetIndex > 1, "请删除或清除多余的sheet页,只保留一个正确的sheet页");
                    header.addAll(cellList);
                }

                @Override
                public void sendDataRows(String filePath, String sheetName, int sheetIndex, int curRow, List cellList) {
                    List tempList = new ArrayList<>();
                    tempList.addAll(cellList);
                    if (listMap.get(sheetIndex) == null) {
                        List> data = new ArrayList<>();
                        data.add(tempList);
                        listMap.put(sheetIndex, data);
                    } else {
                        listMap.get(sheetIndex).add(tempList);
                    }
                }
            };
            excelXls.process(path, headerIncex, dataIndex);
        }
        //处理excel2007文件
        else if (path.endsWith(XLSX_LOWER_CASE) || path.endsWith(XLSX_UPPER_CASE)) {
            ExcelXlsxReader excelXlsxReader = new ExcelXlsxReader() {
                @Override
                public void sendHeaderRows(String filePath, String sheetName, int sheetIndex, int curRow, List cellList) {
                    //ServiceCheckUtility.errorCheck(sheetIndex > 1, "请删除或清除多余的sheet页,只保留一个正确的sheet页");
                    header.addAll(cellList);
                }

                @Override
                public void sendDataRows(String filePath, String sheetName, int sheetIndex, int curRow, List cellList) {
                    List tempList = new ArrayList<>();
                    tempList.addAll(cellList);
                    if (listMap.get(sheetIndex) == null) {
                        List> data = new ArrayList<>();
                        data.add(tempList);
                        listMap.put(sheetIndex, data);
                    } else {
                        listMap.get(sheetIndex).add(tempList);
                    }
                }
            };
            excelXlsxReader.process(path, headerIncex, dataIndex);
        } else {
            throw new Exception("文件格式错误,上传的excel的扩展名只能是xls或xlsx。");
        }
        map.put("data", listMap);
        map.put("head", header);
        return map;
    }

    /**
     * 根据MultipartFile文件对象解析
     *
     * @param file
     * @return
     * @throws Exception
     */
    public static Map    

相关阅读:

  • 如何打印表头(打印表格表头怎么每页都有)
  • 有了这个开源工具后,我五点就下班了
  • 行动
  • 加拿大枫叶卡续签、补发、补发—IMM5444表填写指南
  • Linux
  • 如何输入字符串(python如何输入字符串)
  • 如何设置条件格式(如何输入数字字符串)
  • 「数据结构之字典树Trie」C语言版本实现
  • #汇编语言#课程设计1#王爽著
  • JavaScript中的数据类型判断
    • 网站地图 |
    • 声明:登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。文章内容仅供参考,不做权威认证,如若验证其真实性,请咨询相关权威专业人士。