首页 > 分享 > 基于注解形式Excel导入功能

基于注解形式Excel导入功能

qq_37705525 已于 2022-03-17 17:19:06 修改

于 2022-03-17 17:18:05 首次发布

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

/** * @ClassName: Excel * @Description: * @Author: 88578 * @Date: 2022/3/17 14:16 */ @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) public @interface Excel { String value() default ""; } 1234567891011

@Data public class CbestSiteInfoBody implements Serializable { private static final long serialVersionUID = -4721795934875251002L; /** * 考点名称 */ @Excel("考点名称") private String siteName; } 12345678910

import com.techhero.project.emergency.annotation.Excel; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.springframework.util.Assert; import org.springframework.util.StringUtils; import org.springframework.web.multipart.MultipartFile; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.lang.reflect.Type; import java.text.SimpleDateFormat; import java.util.*; import java.util.concurrent.ConcurrentHashMap; /** * @ClassName: AnnExcelUtils * @Description: Excel基于注解形式解析工具类 * @Author: 88578 * @Date: 2022/3/17 16:10 */ public class AnnExcelUtils<T> { /** * 2003版本的excel */ private final static String excel2003L = "xls"; /** * 2007版本的excel */ private final static String excel2007U = "xlsx"; Class<T> clazz; public AnnExcelUtils(Class<T> clazz) { this.clazz = clazz; } public Collection<T> importExcel(MultipartFile file, String... pattern) { Assert.hasText(file.getOriginalFilename(), "未选择选择文件"); String type = StringUtils.getFilenameExtension(file.getOriginalFilename()); Collection<T> out = new ArrayList<T>(); try { Field filed[] = clazz.getDeclaredFields(); Map<String, Method> conMap = new ConcurrentHashMap<String, Method>(); for (int i = 0; i < filed.length; i++) { Field f = filed[i]; Excel ex = f.getAnnotation(Excel.class); if (ex != null) { String fieldname = f.getName(); String setMethodName = "set" + fieldname.substring(0, 1).toUpperCase() + fieldname.substring(1); Method setMethod = clazz.getMethod(setMethodName, new Class[]{f.getType()}); conMap.put(ex.value(), setMethod); } } Workbook book = null; if (excel2003L.equals(type)) { book = new HSSFWorkbook(file.getInputStream()); } else if (excel2007U.equals(type)) { book = new XSSFWorkbook(file.getInputStream()); } else { throw new Exception("解析的文件格式有误!"); } Sheet sheet = book.getSheetAt(0); Iterator<Row> row = sheet.rowIterator(); Row title = row.next(); Iterator<Cell> cellTitle = title.cellIterator(); Map<Integer, String> conTitle = new ConcurrentHashMap<Integer, String>(); int i = 0; while (cellTitle.hasNext()) { Cell cell = cellTitle.next(); String value = cell.getStringCellValue(); conTitle.put(i, value); i = i + 1; } //用来格式化日期 SimpleDateFormat sf; if (pattern.length < 1) { sf = new SimpleDateFormat("yyyy-MM-dd"); } else { sf = new SimpleDateFormat(pattern[0]); } while (row.hasNext()) { Row rown = row.next(); Iterator<Cell> cellbody = rown.cellIterator(); // 得到传入类的实例 T tObject = clazz.newInstance(); int k = 0; while (cellbody.hasNext()) { Cell cell = cellbody.next(); String titleString = (String) conTitle.get(k); if (conMap.containsKey(titleString)) { Method setMethod = (Method) conMap.get(titleString); Type[] ts = setMethod.getGenericParameterTypes(); String xclass = ts[0].toString(); cell.setCellType(CellType.STRING); if (xclass.equals("class java.lang.String")) { setMethod.invoke(tObject, cell.getStringCellValue()); } else if (xclass.equals("class java.util.Date")) { setMethod.invoke(tObject, sf.parse(cell.getStringCellValue())); } else if (xclass.equals("class java.lang.Boolean")) { Boolean boolname = true; if (cell.getStringCellValue().equals("否")) { boolname = false; } setMethod.invoke(tObject, boolname); } else if (xclass.equals("class java.lang.Integer")) { setMethod.invoke(tObject, new Integer(cell.getStringCellValue())); } else if (xclass.equals("class java.lang.Long")) { setMethod.invoke(tObject, new Long(cell.getStringCellValue())); } } k = k + 1; } out.add(tObject); } } catch (Exception e) { e.printStackTrace(); return null; } return out; } }

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124

相关知识

数据分析入门:15种EXCEL数据分析功能
excel在统计中的应用PPT.ppt
鸢尾花 Excel数据分析
库管易Excel库存管理系统220618版
初中信息技术会考Excel复习提纲
通用Excel库存管理系统,最好用的Excel出入库管理表格
EXCEL VBA培训班
基于知识图谱的植物功能性状与环境研究前沿态势分析
触电的几种形式?
Excel下拉菜单自动颜色设置指南(excel下拉菜单自动颜色)

网址: 基于注解形式Excel导入功能 https://m.huajiangbk.com/newsview854480.html

所属分类:花卉
上一篇: 以鸢尾花数据集为例,用Pytho
下一篇: 机器学习之KNN算法