Excel解析easyexcel工具類

Excel解析easyexcel工具類java

easyexcel解決POI解析Excel出現OOM web

        <!-- https://mvnrepository.com/artifact/com.alibaba/easyexcel -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>easyexcel</artifactId>
            <version>1.1.1</version>
        </dependency>

一、ExcelModel——Java模型映射spring

package com.xinyartech.erp.system.model; import java.io.Serializable; import com.alibaba.excel.annotation.ExcelProperty; import com.alibaba.excel.metadata.BaseRowModel; import lombok.Data; /** * * * @author Lynch */ @SuppressWarnings("serial") @Data public class ExcelModel extends BaseRowModel implements Serializable{ @ExcelProperty(value = "姓名" ,index = 0) private String name; @ExcelProperty(value = "年齡",index = 1) private String age; @ExcelProperty(value = "郵箱",index = 2) private String email; @ExcelProperty(value = "地址",index = 3) private String address; @ExcelProperty(value = "性別",index = 4) private String sax; @ExcelProperty(value = "高度",index = 5) private String heigh; @ExcelProperty(value = "備註",index = 6) private String last; }

 

二、EasyExcelUtil——easyexcel工具類 -- Excel解析apache

package com.xinyartech.erp.core.util; import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; import java.util.List; import com.alibaba.excel.ExcelReader; import com.alibaba.excel.ExcelWriter; import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.event.AnalysisEventListener; import com.alibaba.excel.metadata.BaseRowModel; import com.alibaba.excel.metadata.Sheet; import com.alibaba.excel.metadata.Table; import com.alibaba.excel.support.ExcelTypeEnum; /** * easyexcel工具類 -- Excel解析 * * @author Lynch */
public class EasyExcelUtil { /** * StringList 解析監聽器 */
    private static class StringExcelListener extends AnalysisEventListener { /** * 自定義用於暫時存儲data * 能夠經過實例獲取該值 */
        private List<List<String>> datas = new ArrayList<>(); /** * 每解析一行都會回調invoke()方法 * * @param object * @param context */ @Override public void invoke(Object object, AnalysisContext context) { List<String> stringList= (List<String>) object; //數據存儲到list,供批量處理,或後續本身業務邏輯處理。
 datas.add(stringList); //根據本身業務作處理
 } @Override public void doAfterAllAnalysed(AnalysisContext context) { //解析結束銷燬不用的資源 //注意不要調用datas.clear(),不然getDatas爲null
 } public List<List<String>> getDatas() { return datas; } public void setDatas(List<List<String>> datas) { this.datas = datas; } } /** * 使用 StringList 來讀取Excel * @param inputStream Excel的輸入流 * @param excelTypeEnum Excel的格式(XLS或XLSX) * @return 返回 StringList 的列表 */
    public static List<List<String>> readExcelWithStringList(InputStream inputStream,ExcelTypeEnum excelTypeEnum) { StringExcelListener listener = new StringExcelListener(); ExcelReader excelReader = new ExcelReader(inputStream, excelTypeEnum, null, listener); excelReader.read(); return listener.getDatas(); } /** * 使用 StringList 來寫入Excel * @param outputStream Excel的輸出流 * @param data 要寫入的以StringList爲單位的數據 * @param table 配置Excel的表的屬性 * @param excelTypeEnum Excel的格式(XLS或XLSX) */
    public static void writeExcelWithStringList(OutputStream outputStream, List<List<String>> data, Table table,ExcelTypeEnum excelTypeEnum) { //這裏指定不須要表頭,由於String一般表頭已被包含在data裏
        ExcelWriter writer = new ExcelWriter(outputStream, excelTypeEnum,false); //寫第一個sheet, sheet1 數據全是List<String> 無模型映射關係,無表頭
        Sheet sheet1 = new Sheet(0, 0); writer.write0(data, sheet1,table); writer.finish(); } /** * 模型解析監聽器 -- 每解析一行會回調invoke()方法,整個excel解析結束會執行doAfterAllAnalysed()方法 */
    private static class ModelExcelListener<E> extends AnalysisEventListener<E> { private List<E> dataList = new ArrayList<E>(); @Override public void invoke(E object, AnalysisContext context) { dataList.add(object); } @Override public void doAfterAllAnalysed(AnalysisContext context) { } public List<E> getDataList() { return dataList; } @SuppressWarnings("unused") public void setDataList(List<E> dataList) { this.dataList = dataList; } } /** * 使用 模型 來讀取Excel * * @param inputStream Excel的輸入流 * @param clazz 模型的類 * @param excelTypeEnum Excel的格式(XLS或XLSX) * * @return 返回 模型 的列表 */
    public static <E> List<E> readExcelWithModel(InputStream inputStream, Class<? extends BaseRowModel> clazz, ExcelTypeEnum excelTypeEnum) { // 解析每行結果在listener中處理
        ModelExcelListener<E> listener = new ModelExcelListener<E>(); ExcelReader excelReader = new ExcelReader(inputStream, excelTypeEnum, null, listener); //默認只有一列表頭
        excelReader.read(new Sheet(1, 1, clazz)); return listener.getDataList(); } /** * 使用 模型 來寫入Excel * * @param outputStream Excel的輸出流 * @param data 要寫入的以 模型 爲單位的數據 * @param table 配置Excel的表的屬性 * @param clazz 模型的類 * @param excelTypeEnum Excel的格式(XLS或XLSX) */
    public static void writeExcelWithModel(OutputStream outputStream, List<? extends BaseRowModel> data, Class<? extends BaseRowModel> clazz, ExcelTypeEnum excelTypeEnum) { //這裏指定須要表頭,由於model一般包含表頭信息
        ExcelWriter writer = new ExcelWriter(outputStream, excelTypeEnum,true); //寫第一個sheet, sheet1 數據全是List<String> 無模型映射關係
        Sheet sheet1 = new Sheet(1, 0, clazz); writer.write(data, sheet1); writer.finish(); } } 

 

三、EasyExcelTest測試類api

package com.xinyartech.erp.system.service; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; import java.util.List; import com.alibaba.excel.support.ExcelTypeEnum; import com.xinyartech.erp.core.util.EasyExcelUtil; import com.xinyartech.erp.system.model.ExcelModel; /** * 單元測試 * * @author Lynch */
public class EasyExcelTest { public static void main(String[] args) throws Exception { writeExcel(); readExcel(); } /** * 寫入Excel * * @throws FileNotFoundException * @author Lynch */
    private static void writeExcel() throws FileNotFoundException { List<ExcelModel> excelModelList = new ArrayList<>(); for (int i = 0; i < 1000000; i++) { ExcelModel excelModel = new ExcelModel(); excelModel.setAddress("address" + i); excelModel.setAge(i + ""); excelModel.setEmail("email" + i); excelModel.setHeigh("heigh" + i); excelModel.setLast("last" + i); excelModel.setName("name" + i); excelModel.setSax("sax" + i); excelModelList.add(excelModel); } long beginTime = System.currentTimeMillis(); OutputStream out = new FileOutputStream("D:/aaa.xlsx"); EasyExcelUtil.writeExcelWithModel(out, excelModelList, ExcelModel.class, ExcelTypeEnum.XLSX); long endTime = System.currentTimeMillis(); System.out.println(String.format("總共耗時 %s 毫秒", (endTime - beginTime))); excelModelList = null; } /** * 讀取Excel * * @throws FileNotFoundException * @author Lynch */
    private static void readExcel() throws FileNotFoundException { try { InputStream inputStream=new FileInputStream("D:/aaa.xlsx"); //讀入文件,每一行對應一個 Model,獲取 Model 列表
            List<ExcelModel> objectList = EasyExcelUtil.readExcelWithModel(inputStream, ExcelModel.class, ExcelTypeEnum.XLSX); for(ExcelModel excelModel: objectList) { System.out.println(excelModel); } } catch (IOException e) { e.printStackTrace(); } } }

4、springMVC+easypoi作excel的導入導出app

package com.xinyartech.erp.system.web;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.excel.support.ExcelTypeEnum;
import com.xinyartech.erp.core.base.BaseUserController;
import com.xinyartech.erp.core.base.ResponseWrapper;
import com.xinyartech.erp.core.util.EasyExcelUtil;
import com.xinyartech.erp.system.model.ExcelModel;
import com.xinyartech.erp.system.model.SysUser;
import com.xinyartech.erp.system.service.UserService;
 
@Controller
@RequestMapping("/api/hello")
public class HelloWorldController extends BaseUserController<UserService, SysUser> {
    private static final Logger log = LogManager.getLogger(HelloWorldController.class);
    
    @Autowired
    protected HttpServletResponse response;
    /**
     * 導入Excel
     * 
     * @param file
     * @author Lynch
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    @ResponseBody
    public ResponseWrapper<?> importExcel(@RequestParam(value = "uploadFile", required = false) MultipartFile file) {
        try {
            List<ExcelModel> excelModelList = EasyExcelUtil.readExcelWithModel(file.getInputStream(), ExcelModel.class, ExcelTypeEnum.XLSX);
            return ResponseWrapper.ok(excelModelList);
        } catch (IOException e) {
            log.error("Excel導入失敗", e);
        }
        
        return ResponseWrapper.ok();
    }
    
    /**
     * 導出Excel
     * 
     * @param response
     * @author Lynch
     */
    @RequestMapping(value="/writeExcel")
  @ResponseBody
public ResponseWrapper<String>writeExcel(HttpServletResponse response){ //初始化模擬數據 List<ExcelModel> excelModelList = new ArrayList<>(); for (int i = 0; i < 700; i++) { ExcelModel excelModel = new ExcelModel(); excelModel.setAddress("address" + i); excelModel.setAge(i + ""); excelModel.setEmail("email" + i); excelModel.setHeigh("heigh" + i); excelModel.setLast("last" + i); excelModel.setName("name" + i); excelModel.setSax("sax" + i); excelModelList.add(excelModel); } return writeExcel("abc", excelModelList); } /** * 導出Excel工具類 * * @param fileName 導出文件名,不填默認"abbot.xlsx" * @param data 導出數據 * @author Lynch */ protected ResponseWrapper<String> writeExcel(String fileName, List<? extends BaseRowModel> data) { try { if(CollectionUtils.isEmpty(data)) { log.warn("Excel導出數據爲空"); return ResponseWrapper.ok(SystemEnum.ERROR_COLLECTION_EMPTY, "Excel導出數據爲空"); } if(StringUtils.isEmpty(fileName)) { fileName = "abbot.xlsx"; } String filename = new String(fileName.getBytes("UTF-8"), "ISO-8859-1"); response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE); response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE); response.setHeader("Content-disposition", String.format("attachment; filename=%s.xlsx", filename)); // 將文件輸出 EasyExcelUtil.writeExcelWithModel(response.getOutputStream(), data, data.get(0).getClass(), ExcelTypeEnum.XLSX); data = null; } catch (Exception e) { log.error("Excel導出失敗", e); return ResponseWrapper.error("Excel導出失敗"); } return ResponseWrapper.ok(); } }
相關文章
相關標籤/搜索