tyty
2 years ago
12 changed files with 827 additions and 104 deletions
@ -0,0 +1,33 @@
|
||||
package com.hnac.hzims.spare.utils; |
||||
|
||||
import com.alibaba.excel.context.AnalysisContext; |
||||
import com.alibaba.excel.event.AnalysisEventListener; |
||||
import com.alibaba.fastjson.JSONObject; |
||||
import lombok.Data; |
||||
import lombok.extern.slf4j.Slf4j; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.Collections; |
||||
import java.util.List; |
||||
|
||||
@Slf4j |
||||
@Data |
||||
public class ExcelToolListener extends AnalysisEventListener { |
||||
|
||||
/**保存数据**/ |
||||
private List<Object> dataList = Collections.synchronizedList(new ArrayList<>()); |
||||
|
||||
public ExcelToolListener() { |
||||
} |
||||
|
||||
@Override |
||||
public void invoke(Object data, AnalysisContext context) { |
||||
dataList.add(data); |
||||
} |
||||
|
||||
@Override |
||||
public void doAfterAllAnalysed(AnalysisContext context) { |
||||
log.info("解析完成"); |
||||
log.info("------结果集为:{}------", JSONObject.toJSONString(dataList)); |
||||
} |
||||
} |
@ -0,0 +1,347 @@
|
||||
package com.hnac.hzims.spare.utils; |
||||
|
||||
import com.alibaba.excel.EasyExcel; |
||||
import com.alibaba.excel.EasyExcelFactory; |
||||
import com.alibaba.excel.event.AnalysisEventListener; |
||||
import com.alibaba.excel.write.handler.WriteHandler; |
||||
import org.apache.poi.ss.formula.functions.T; |
||||
|
||||
import java.io.File; |
||||
import java.io.InputStream; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
import java.util.Set; |
||||
|
||||
/** |
||||
* @author hx |
||||
*/ |
||||
public class ExcelUtil { |
||||
/** |
||||
* 同步无模型读(默认读取sheet0,从第2行开始读) |
||||
* @param filePath |
||||
* @return |
||||
*/ |
||||
public static List<Map<Integer, String>> syncRead(String filePath){ |
||||
return EasyExcelFactory.read(filePath).sheet().doReadSync(); |
||||
} |
||||
|
||||
/** |
||||
* 同步无模型读(默认表头占一行,从第2行开始读) |
||||
* @param filePath |
||||
* @param sheetNo sheet页号,从0开始 |
||||
* @return |
||||
*/ |
||||
public static List<Map<Integer, String>> syncRead(String filePath, Integer sheetNo){ |
||||
return EasyExcelFactory.read(filePath).sheet(sheetNo).doReadSync(); |
||||
} |
||||
|
||||
/** |
||||
* 同步无模型读(指定sheet和表头占的行数) |
||||
* @param inputStream |
||||
* @param sheetNo sheet页号,从0开始 |
||||
* @param headRowNum 表头占的行数,从0开始(如果要连表头一起读出来则传0) |
||||
* @return List<Map<colNum, cellValue>> |
||||
*/ |
||||
public static List<Map<Integer, String>> syncRead(InputStream inputStream, Integer sheetNo, Integer headRowNum){ |
||||
return EasyExcelFactory.read(inputStream).sheet(sheetNo).headRowNumber(headRowNum).doReadSync(); |
||||
} |
||||
|
||||
/** |
||||
* 同步无模型读(指定sheet和表头占的行数) |
||||
* @param file |
||||
* @param sheetNo sheet页号,从0开始 |
||||
* @param headRowNum 表头占的行数,从0开始(如果要连表头一起读出来则传0) |
||||
* @return List<Map<colNum, cellValue>> |
||||
*/ |
||||
public static List<Map<Integer, String>> syncRead(File file, Integer sheetNo, Integer headRowNum){ |
||||
return EasyExcelFactory.read(file).sheet(sheetNo).headRowNumber(headRowNum).doReadSync(); |
||||
} |
||||
|
||||
/** |
||||
* 同步无模型读(指定sheet和表头占的行数) |
||||
* @param filePath |
||||
* @param sheetNo sheet页号,从0开始 |
||||
* @param headRowNum 表头占的行数,从0开始(如果要连表头一起读出来则传0) |
||||
* @return List<Map<colNum, cellValue>> |
||||
*/ |
||||
public static List<Map<Integer, String>> syncRead(String filePath, Integer sheetNo, Integer headRowNum){ |
||||
return EasyExcelFactory.read(filePath).sheet(sheetNo).headRowNumber(headRowNum).doReadSync(); |
||||
} |
||||
|
||||
/** |
||||
* 同步按模型读(默认读取sheet0,从第2行开始读) |
||||
* @param filePath |
||||
* @param clazz 模型的类类型(excel数据会按该类型转换成对象) |
||||
* @return |
||||
*/ |
||||
public static List<T> syncReadModel(String filePath, Class clazz){ |
||||
return EasyExcelFactory.read(filePath).sheet().head(clazz).doReadSync(); |
||||
} |
||||
|
||||
/** |
||||
* 同步按模型读(默认表头占一行,从第2行开始读) |
||||
* @param filePath |
||||
* @param clazz 模型的类类型(excel数据会按该类型转换成对象) |
||||
* @param sheetNo sheet页号,从0开始 |
||||
* @return |
||||
*/ |
||||
public static List<T> syncReadModel(String filePath, Class clazz, Integer sheetNo){ |
||||
return EasyExcelFactory.read(filePath).sheet(sheetNo).head(clazz).doReadSync(); |
||||
} |
||||
|
||||
/** |
||||
* 同步按模型读(指定sheet和表头占的行数) |
||||
* @param inputStream |
||||
* @param clazz 模型的类类型(excel数据会按该类型转换成对象) |
||||
* @param sheetNo sheet页号,从0开始 |
||||
* @param headRowNum 表头占的行数,从0开始(如果要连表头一起读出来则传0) |
||||
* @return |
||||
*/ |
||||
public static List<T> syncReadModel(InputStream inputStream, Class clazz, Integer sheetNo, Integer headRowNum){ |
||||
return EasyExcelFactory.read(inputStream).sheet(sheetNo).headRowNumber(headRowNum).head(clazz).doReadSync(); |
||||
} |
||||
|
||||
/** |
||||
* 同步按模型读(指定sheet和表头占的行数) |
||||
* @param file |
||||
* @param clazz 模型的类类型(excel数据会按该类型转换成对象) |
||||
* @param sheetNo sheet页号,从0开始 |
||||
* @param headRowNum 表头占的行数,从0开始(如果要连表头一起读出来则传0) |
||||
* @return |
||||
*/ |
||||
public static List<T> syncReadModel(File file, Class clazz, Integer sheetNo, Integer headRowNum){ |
||||
return EasyExcelFactory.read(file).sheet(sheetNo).headRowNumber(headRowNum).head(clazz).doReadSync(); |
||||
} |
||||
|
||||
/** |
||||
* 同步按模型读(指定sheet和表头占的行数) |
||||
* @param filePath |
||||
* @param clazz 模型的类类型(excel数据会按该类型转换成对象) |
||||
* @param sheetNo sheet页号,从0开始 |
||||
* @param headRowNum 表头占的行数,从0开始(如果要连表头一起读出来则传0) |
||||
* @return |
||||
*/ |
||||
public static List<T> syncReadModel(String filePath, Class clazz, Integer sheetNo, Integer headRowNum){ |
||||
return EasyExcelFactory.read(filePath).sheet(sheetNo).headRowNumber(headRowNum).head(clazz).doReadSync(); |
||||
} |
||||
|
||||
/** |
||||
* 异步无模型读(默认读取sheet0,从第2行开始读) |
||||
* @param excelListener 监听器,在监听器中可以处理行数据LinkedHashMap,表头数据,异常处理等 |
||||
* @param filePath 表头占的行数,从0开始(如果要连表头一起读出来则传0) |
||||
* @return |
||||
*/ |
||||
public static void asyncRead(String filePath, AnalysisEventListener<T> excelListener){ |
||||
EasyExcelFactory.read(filePath, excelListener).sheet().doRead(); |
||||
} |
||||
|
||||
/** |
||||
* 异步无模型读(默认表头占一行,从第2行开始读) |
||||
* @param filePath 表头占的行数,从0开始(如果要连表头一起读出来则传0) |
||||
* @param excelListener 监听器,在监听器中可以处理行数据LinkedHashMap,表头数据,异常处理等 |
||||
* @param sheetNo sheet页号,从0开始 |
||||
* @return |
||||
*/ |
||||
public static void asyncRead(String filePath, AnalysisEventListener<T> excelListener, Integer sheetNo){ |
||||
EasyExcelFactory.read(filePath, excelListener).sheet(sheetNo).doRead(); |
||||
} |
||||
|
||||
/** |
||||
* 异步无模型读(指定sheet和表头占的行数) |
||||
* @param inputStream |
||||
* @param excelListener 监听器,在监听器中可以处理行数据LinkedHashMap,表头数据,异常处理等 |
||||
* @param sheetNo sheet页号,从0开始 |
||||
* @param headRowNum 表头占的行数,从0开始(如果要连表头一起读出来则传0) |
||||
* @return |
||||
*/ |
||||
public static void asyncRead(InputStream inputStream, AnalysisEventListener<T> excelListener, Integer sheetNo, Integer headRowNum){ |
||||
EasyExcelFactory.read(inputStream, excelListener).sheet(sheetNo).headRowNumber(headRowNum).doRead(); |
||||
} |
||||
|
||||
/** |
||||
* 异步无模型读(指定sheet和表头占的行数) |
||||
* @param file |
||||
* @param excelListener 监听器,在监听器中可以处理行数据LinkedHashMap,表头数据,异常处理等 |
||||
* @param sheetNo sheet页号,从0开始 |
||||
* @param headRowNum 表头占的行数,从0开始(如果要连表头一起读出来则传0) |
||||
* @return |
||||
*/ |
||||
public static void asyncRead(File file, AnalysisEventListener<T> excelListener, Integer sheetNo, Integer headRowNum){ |
||||
EasyExcelFactory.read(file, excelListener).sheet(sheetNo).headRowNumber(headRowNum).doRead(); |
||||
} |
||||
|
||||
/** |
||||
* 异步无模型读(指定sheet和表头占的行数) |
||||
* @param filePath |
||||
* @param excelListener 监听器,在监听器中可以处理行数据LinkedHashMap,表头数据,异常处理等 |
||||
* @param sheetNo sheet页号,从0开始 |
||||
* @param headRowNum 表头占的行数,从0开始(如果要连表头一起读出来则传0) |
||||
* @return |
||||
*/ |
||||
public static void asyncRead(String filePath, AnalysisEventListener<T> excelListener, Integer sheetNo, Integer headRowNum){ |
||||
EasyExcelFactory.read(filePath, excelListener).sheet(sheetNo).headRowNumber(headRowNum).doRead(); |
||||
} |
||||
|
||||
/** |
||||
* 异步按模型读取(默认读取sheet0,从第2行开始读) |
||||
* @param filePath |
||||
* @param excelListener 监听器,在监听器中可以处理行数据LinkedHashMap,表头数据,异常处理等 |
||||
* @param clazz 模型的类类型(excel数据会按该类型转换成对象) |
||||
*/ |
||||
public static void asyncReadModel(String filePath, AnalysisEventListener<T> excelListener, Class clazz){ |
||||
EasyExcelFactory.read(filePath, clazz, excelListener).sheet().doRead(); |
||||
} |
||||
|
||||
/** |
||||
* 异步按模型读取(默认表头占一行,从第2行开始读) |
||||
* @param filePath |
||||
* @param excelListener 监听器,在监听器中可以处理行数据LinkedHashMap,表头数据,异常处理等 |
||||
* @param clazz 模型的类类型(excel数据会按该类型转换成对象) |
||||
* @param sheetNo sheet页号,从0开始 |
||||
*/ |
||||
public static void asyncReadModel(String filePath, AnalysisEventListener<T> excelListener, Class clazz, Integer sheetNo){ |
||||
EasyExcelFactory.read(filePath, clazz, excelListener).sheet(sheetNo).doRead(); |
||||
} |
||||
|
||||
/** |
||||
* 异步按模型读取 |
||||
* @param inputStream |
||||
* @param excelListener 监听器,在监听器中可以处理行数据LinkedHashMap,表头数据,异常处理等 |
||||
* @param clazz 模型的类类型(excel数据会按该类型转换成对象) |
||||
* @param sheetNo sheet页号,从0开始 |
||||
* @param headRowNum 表头占的行数,从0开始(如果要连表头一起读出来则传0) |
||||
*/ |
||||
public static void asyncReadModel(InputStream inputStream, AnalysisEventListener<T> excelListener, Class clazz, Integer sheetNo, Integer headRowNum){ |
||||
EasyExcelFactory.read(inputStream, clazz, excelListener).sheet(sheetNo).headRowNumber(headRowNum).doRead(); |
||||
} |
||||
|
||||
/** |
||||
* 异步按模型读取 |
||||
* @param file |
||||
* @param excelListener 监听器,在监听器中可以处理行数据LinkedHashMap,表头数据,异常处理等 |
||||
* @param clazz 模型的类类型(excel数据会按该类型转换成对象) |
||||
* @param sheetNo sheet页号,从0开始 |
||||
* @param headRowNum 表头占的行数,从0开始(如果要连表头一起读出来则传0) |
||||
*/ |
||||
public static void asyncReadModel(File file, AnalysisEventListener<T> excelListener, Class clazz, Integer sheetNo, Integer headRowNum){ |
||||
EasyExcelFactory.read(file, clazz, excelListener).sheet(sheetNo).headRowNumber(headRowNum).doRead(); |
||||
} |
||||
|
||||
/** |
||||
* 异步按模型读取 |
||||
* @param filePath |
||||
* @param excelListener 监听器,在监听器中可以处理行数据LinkedHashMap,表头数据,异常处理等 |
||||
* @param clazz 模型的类类型(excel数据会按该类型转换成对象) |
||||
* @param sheetNo sheet页号,从0开始 |
||||
* @param headRowNum 表头占的行数,从0开始(如果要连表头一起读出来则传0) |
||||
*/ |
||||
public static void asyncReadModel(String filePath, AnalysisEventListener<T> excelListener, Class clazz, Integer sheetNo, Integer headRowNum){ |
||||
EasyExcelFactory.read(filePath, clazz, excelListener).sheet(sheetNo).headRowNumber(headRowNum).doRead(); |
||||
} |
||||
|
||||
/** |
||||
* 无模板写文件 |
||||
* @param filePath |
||||
* @param head 表头数据 |
||||
* @param data 表内容数据 |
||||
*/ |
||||
public static void write(String filePath, List<List<String>> head, List<List<Object>> data){ |
||||
EasyExcel.write(filePath).head(head).sheet().doWrite(data); |
||||
} |
||||
|
||||
/** |
||||
* 无模板写文件 |
||||
* @param filePath |
||||
* @param head 表头数据 |
||||
* @param data 表内容数据 |
||||
* @param sheetNo sheet页号,从0开始 |
||||
* @param sheetName sheet名称 |
||||
*/ |
||||
public static void write(String filePath, List<List<String>> head, List<List<Object>> data, Integer sheetNo, String sheetName){ |
||||
EasyExcel.write(filePath).head(head).sheet(sheetNo, sheetName).doWrite(data); |
||||
} |
||||
|
||||
/** |
||||
* 根据excel模板文件写入文件 |
||||
* @param filePath |
||||
* @param templateFileName |
||||
* @param headClazz |
||||
* @param data |
||||
*/ |
||||
public static void writeTemplate(String filePath, String templateFileName, Class headClazz, List data){ |
||||
EasyExcel.write(filePath, headClazz).withTemplate(templateFileName).sheet().doWrite(data); |
||||
} |
||||
|
||||
/** |
||||
* 根据excel模板文件写入文件 |
||||
* @param filePath |
||||
* @param templateFileName |
||||
* @param data |
||||
*/ |
||||
public static void writeTemplate(String filePath, String templateFileName, List data){ |
||||
EasyExcel.write(filePath).withTemplate(templateFileName).sheet().doWrite(data); |
||||
} |
||||
|
||||
/** |
||||
* 按模板写文件 |
||||
* @param filePath |
||||
* @param headClazz 表头模板 |
||||
* @param data 数据 |
||||
*/ |
||||
public static void write(String filePath, Class headClazz, List data){ |
||||
EasyExcel.write(filePath, headClazz).sheet().doWrite(data); |
||||
} |
||||
|
||||
/** |
||||
* 按模板写文件 |
||||
* @param filePath |
||||
* @param headClazz 表头模板 |
||||
* @param data 数据 |
||||
* @param sheetNo sheet页号,从0开始 |
||||
* @param sheetName sheet名称 |
||||
*/ |
||||
public static void write(String filePath, Class headClazz, List data, Integer sheetNo, String sheetName){ |
||||
EasyExcel.write(filePath, headClazz).sheet(sheetNo, sheetName).doWrite(data); |
||||
} |
||||
|
||||
/** |
||||
* 按模板写文件 |
||||
* @param filePath |
||||
* @param headClazz 表头模板 |
||||
* @param data 数据 |
||||
* @param writeHandler 自定义的处理器,比如设置table样式,设置超链接、单元格下拉框等等功能都可以通过这个实现(需要注册多个则自己通过链式去调用) |
||||
* @param sheetNo sheet页号,从0开始 |
||||
* @param sheetName sheet名称 |
||||
*/ |
||||
public static void write(String filePath, Class headClazz, List data, WriteHandler writeHandler, Integer sheetNo, String sheetName){ |
||||
EasyExcel.write(filePath, headClazz).registerWriteHandler(writeHandler).sheet(sheetNo, sheetName).doWrite(data); |
||||
} |
||||
|
||||
/** |
||||
* 按模板写文件(包含某些字段) |
||||
* @param filePath |
||||
* @param headClazz 表头模板 |
||||
* @param data 数据 |
||||
* @param includeCols 过滤包含的字段,根据字段名称过滤 |
||||
* @param sheetNo sheet页号,从0开始 |
||||
* @param sheetName sheet名称 |
||||
*/ |
||||
public static void writeInclude(String filePath, Class headClazz, List data, Set<String> includeCols, Integer sheetNo, String sheetName){ |
||||
EasyExcel.write(filePath, headClazz).includeColumnFiledNames(includeCols).sheet(sheetNo, sheetName).doWrite(data); |
||||
} |
||||
|
||||
/** |
||||
* 按模板写文件(排除某些字段) |
||||
* @param filePath |
||||
* @param headClazz 表头模板 |
||||
* @param data 数据 |
||||
* @param excludeCols 过滤排除的字段,根据字段名称过滤 |
||||
* @param sheetNo sheet页号,从0开始 |
||||
* @param sheetName sheet名称 |
||||
*/ |
||||
public static void writeExclude(String filePath, Class headClazz, List data, Set<String> excludeCols, Integer sheetNo, String sheetName){ |
||||
EasyExcel.write(filePath, headClazz).excludeColumnFiledNames(excludeCols).sheet(sheetNo, sheetName).doWrite(data); |
||||
} |
||||
|
||||
|
||||
} |
@ -0,0 +1,77 @@
|
||||
package com.hnac.hzims.spare.utils; |
||||
|
||||
import com.alibaba.excel.write.handler.SheetWriteHandler; |
||||
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder; |
||||
import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder; |
||||
import org.apache.poi.ss.usermodel.DataValidation; |
||||
import org.apache.poi.ss.usermodel.DataValidationConstraint; |
||||
import org.apache.poi.ss.usermodel.DataValidationHelper; |
||||
import org.apache.poi.ss.usermodel.Sheet; |
||||
import org.apache.poi.ss.util.CellRangeAddressList; |
||||
import org.springblade.system.cache.DictCache; |
||||
import org.springblade.system.entity.Dict; |
||||
|
||||
import java.util.HashMap; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
import java.util.stream.Collectors; |
||||
|
||||
public class SheetWriteStyleHandler implements SheetWriteHandler { |
||||
|
||||
@Override |
||||
public void beforeSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) { |
||||
|
||||
} |
||||
|
||||
@Override |
||||
public void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) { |
||||
|
||||
// 定义一个Map key 是需要添加下拉框的列的index value是下拉框的数据
|
||||
Map<Integer,String[]> mapDropDown=new HashMap<>(); |
||||
// 变量类型 单选 多选 日期 时间 日期时间 数值 字符 字符数值 字符日期 字符时间 长字符
|
||||
List<Dict> spareUnit = DictCache.getList("spare_unit"); |
||||
List<String> unitList = spareUnit.stream().map(s -> s.getDictValue()).collect(Collectors.toList()); |
||||
String[] strings = unitList.toArray(new String[unitList.size()]); |
||||
// //只读 是否
|
||||
// String[] readOnly={"是","否"};
|
||||
// // 文本区只读 是否
|
||||
// String[] textReadOnly={"是","否"};
|
||||
// //必填项 是否
|
||||
// String[] require={"是","否"};
|
||||
//
|
||||
// // 精确度 精确到时 精确到分 精确到秒 精确到日 精确到月
|
||||
// String[] accuracy={"精确到时","精确到分","精确到秒","精确到日","精确到月",};
|
||||
// //编码排列方式 单行Radio 多行Radio 下拉菜单
|
||||
// String[] codeArrangement={"单行Radio","多行Radio","下拉菜单"};
|
||||
// // 是否生效 是否
|
||||
// String[] operation={"是","否"};
|
||||
|
||||
// 下拉框在excel中对应的值
|
||||
mapDropDown.put(5,strings); |
||||
String[] modifyEntryDateYesNo={"是","否"}; |
||||
mapDropDown.put(9,modifyEntryDateYesNo); |
||||
// mapDropDown.put(11,readOnly);
|
||||
// mapDropDown.put(12,textReadOnly);
|
||||
// mapDropDown.put(13,require);
|
||||
// mapDropDown.put(17,accuracy);
|
||||
// mapDropDown.put(20,codeArrangement);
|
||||
// mapDropDown.put(24,operation);
|
||||
|
||||
//获取工作簿sheet
|
||||
Sheet sheet = writeSheetHolder.getSheet(); |
||||
// 开始设置下拉框
|
||||
DataValidationHelper helper = sheet.getDataValidationHelper(); |
||||
//设置下拉框
|
||||
for(Map.Entry<Integer,String[]> entry:mapDropDown.entrySet()){ |
||||
/*起始行、终止行、起始列、终止列 起始行为1即表示表头不设置**/ |
||||
CellRangeAddressList addressList = new CellRangeAddressList(1, 65535, entry.getKey(), entry.getKey()); |
||||
/*设置下拉框数据**/ |
||||
DataValidationConstraint constraint = helper.createExplicitListConstraint(entry.getValue()); |
||||
DataValidation dataValidation = helper.createValidation(constraint, addressList); |
||||
sheet.addValidationData(dataValidation); |
||||
|
||||
} |
||||
|
||||
|
||||
} |
||||
} |
Loading…
Reference in new issue