1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- package org.jeecg.modules.excelutil;
- import com.alibaba.excel.EasyExcelFactory;
- import com.alibaba.excel.ExcelReader;
- import com.alibaba.excel.ExcelWriter;
- import com.alibaba.excel.event.AnalysisEventListener;
- import com.alibaba.excel.metadata.BaseRowModel;
- import com.alibaba.excel.metadata.Sheet;
- import com.alibaba.excel.support.ExcelTypeEnum;
- import java.io.*;
- import java.util.List;
- public class ExcelUtils {
- /**
- * @param is 导入文件输入流
- * @param clazz Excel实体映射类
- * @return
- */
- public static Boolean readExcel(InputStream is, Class clazz, AnalysisEventListener listener) {
- BufferedInputStream bis = null;
- try {
- bis = new BufferedInputStream(is);
- // 解析每行结果在listener中处理
- ExcelReader excelReader = EasyExcelFactory.getReader(bis, listener);
- excelReader.read(new Sheet(1, 1, clazz));
- } catch (Exception e) {
- e.printStackTrace();
- return false;
- } finally {
- if (bis != null) {
- try {
- bis.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- return true;
- }
- /**
- * @param os 文件输出流
- * @param clazz Excel实体映射类
- * @param data 导出数据
- * @return
- */
- public static Boolean writeExcel(OutputStream os, Class clazz, List<? extends BaseRowModel> data) {
- BufferedOutputStream bos = null;
- try {
- bos = new BufferedOutputStream(os);
- ExcelWriter writer = new ExcelWriter(bos, ExcelTypeEnum.XLSX);
- //写第一个sheet, sheet1 数据全是List<String> 无模型映射关系
- Sheet sheet1 = new Sheet(1, 0, clazz);
- writer.write(data, sheet1);
- writer.finish();
- } catch (Exception e) {
- e.printStackTrace();
- return false;
- } finally {
- if (bos != null) {
- try {
- bos.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- return true;
- }
- }
|