package com.cinc.ecmp.utils;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List; import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile; import com.cinc.ecmp.annotation.ExcelField;
import com.cinc.ecmp.enums.BackResultEnum;
import com.cinc.ecmp.exception.BasException; import lombok.extern.slf4j.Slf4j; /**
* @author j
* 操作方式:
* 根据Excel格式编写vo,vo编写方式参照com.cinc.ecmp.demo.vo.ExcelMaterialVo
*/
@Slf4j
public class ExcelParseUtils { public static <T> List<T> parse(MultipartFile file,Class<T> clazz){
Workbook hwb=null;
InputStream in = null;
try{
if(null == file){
throw new BasException(BackResultEnum.DATAPARSEERR);
}
in = file.getInputStream();
hwb=WorkbookFactory.create(in);
Sheet sheet=hwb.getSheetAt(0);
if(sheet==null) {
return null;
}
List<T> excelVoList=new ArrayList<T>();
Field[] fields=clazz.getDeclaredFields();
for(int i=sheet.getFirstRowNum()+1;i<sheet.getPhysicalNumberOfRows();i++) {
Row row = sheet.getRow(i);
if(null==row.getCell(1)||row.getCell(1).getCellType()==CellType.BLANK) {
break;//如果这一行的第一列为空,则终止解析
}
T object = clazz.newInstance();
for(Field field:fields) {
//获取该字段的列号
ExcelField excelField=field.getAnnotation(ExcelField.class);
if(null == excelField) {
continue;
}
int index = excelField.index();
String value="";
Cell cell = row.getCell(index);
if(null == cell) {
continue;
}
CellType cellType = cell.getCellType();
switch(cellType) {
case STRING:
value = String.valueOf(cell.getRichStringCellValue());
break;
case FORMULA:
try {
value = String.valueOf(cell.getNumericCellValue());
}catch (Exception e) {
value = String.valueOf(cell.getRichStringCellValue());
}
break;
case NUMERIC:
if(HSSFDateUtil.isCellDateFormatted(cell)) {
Date date=HSSFDateUtil.getJavaDate(cell.getNumericCellValue());
value=DateUtil.getDateTimeStringToDb(date);
}else {
DecimalFormat df = new DecimalFormat();
value = df.format(cell.getNumericCellValue());
}
break;
case BLANK:
break;
default:
throw new BasException(BackResultEnum.DATAPARSEERR);
}
//非空判断
if(excelField.required()&&StringUtils.isEmpty(value)) {
log.error("【{}不能为空】",excelField.desc());
throw new BasException(BackResultEnum.DATAPARSEERR);
}
//数据合法性判断
String[] dataScopes=excelField.inList();
if(!CollectionUtils.isEmpty(Arrays.asList(dataScopes))&&!Arrays.asList(dataScopes).contains(value)) {
log.error("【{}不合法】",excelField.desc());
throw new BasException(BackResultEnum.DATAPARSEERR);
}
//字段类型设置
Class<?> typeClass=field.getType();
Method method = clazz.getMethod("set" + Character.toUpperCase(field.getName().charAt(0)) + field.getName().substring(1), typeClass);
//将字段值设置到对象中
try {
if(typeClass==Integer.class) {
method.invoke(object, value.indexOf(".")>0?Integer.parseInt(value.substring(0, value.indexOf("."))):Integer.parseInt(value));
}else if(typeClass==BigDecimal.class) {
method.invoke(object, new BigDecimal(value.indexOf(",")>0?value.replaceAll(",", ""):value));
}else {
method.invoke(object, value.indexOf(",")>0?value.replaceAll(",", ""):value);
}
}catch (Exception e) {
log.error("【{}设置错误】",excelField.desc(),e);
throw new BasException(BackResultEnum.DATAPARSEERR);
}
}
if(excelVoList.contains(object)) {
log.error("【导入失败,数据存在重复】");
throw new BasException(BackResultEnum.DATAREPEATERR);
}
excelVoList.add(object);
}
return excelVoList;
}catch (Exception e) {
log.error("【文件上传失败】",e);
throw new BasException(BackResultEnum.FILEUPLOADERR);
}finally {
if(null != hwb){
try {
hwb.close();
} catch (IOException e) {
log.error("【文件流关闭失败】",e);
}
}
if(null != in){
try {
in.close();
} catch (IOException e) {
log.error("【文件流关闭失败】",e);
}
}
}
}
}

  vo实体类的编写:

package com.cinc.ecmp.demo.vo;

import java.math.BigDecimal;

import com.cinc.ecmp.annotation.ExcelField;

import lombok.Data;

/**
* @author
* @time 2019年7月2日 下午7:59:26
*/
@Data
public class ExcelMaterialVo { @ExcelField(index=0,required=true,desc="材料名称")
private String name; @ExcelField(index=1,required=true,desc="价格")
private BigDecimal price; @ExcelField(index=2,required=true,desc="采购数量")
private Integer count;
}

注解的编写:

package com.cinc.ecmp.annotation;

import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.RetentionPolicy.RUNTIME; import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.Target; @Retention(RUNTIME)
@Target(FIELD)
@Inherited
public @interface ExcelField { /**
* Excel表格列顺序,起始为0
* @return
*/
int index(); /***
* 数据范围
* @return
*/
String[] inList() default {}; /***
* 正则
* @return
*/
String pattern() default ""; /***
* 字段描述
* @return
*/
String desc() default ""; /**
* 是否是必须的
*
* @return
*/
boolean required() default false;
}

  

@Override
public List<ExcelMaterialVo> upload(MultipartFile file){
List<ExcelMaterialVo> excelMaterialVoList=ExcelParseUtils.parse(file, ExcelMaterialVo.class);
return excelMaterialVoList;
}

excel转换成实体的更多相关文章

  1. Epplus下的一个将Excel转换成List的范型帮助类

    因为前一段时间公司做项目的时候,用到了Excel导入和导出,然后自己找了个插件Epplus进行操作,自己将当时的一些代码抽离出来写了一个帮助类. 因为帮助类是在Epplus基础之上写的,项目需要引用E ...

  2. C# 将DataTable数据源转换成实体类

    using System; using System.Collections.Generic; using System.Data; using System.Reflection; /// < ...

  3. 字符串js编码转换成实体html编码的方法(防范XSS攻击)

    js代码在html页面中转换成实体html编码的方法一: <!DOCTYPE html><html> <head>    <title>js代码转换成实 ...

  4. C# DataTable转换成实体列表 与 实体列表转换成DataTable

    /// <summary> /// DataTable转换成实体列表 /// </summary> /// <typeparam name="T"&g ...

  5. sql hibernate查询转换成实体或对应的VO Transformers

    sql查询转换成实体或对应的VO Transformers //addScalar("id") 默认查询出来的id是全部大写的(sql起别名也无效,所以使用.addScalar(& ...

  6. hibernate查询部分字段转换成实体bean

    //hibernate查询部分字段转换成实体bean /** * 查询线路信息 */ @Override public List<Line> getSimpleLineListByTj(M ...

  7. How to cast List<Object> to List<MyClass> Object集合转换成实体集合

    List<Object> list = getList(); return (List<Customer>) list; Compiler says: cannot cast  ...

  8. Table转换成实体、Table转换成实体集合(可转换成对象和值类型)

    /// <summary> /// Table转换成实体 /// </summary> /// <typeparam name="T">< ...

  9. DataTable转换成实体

    public static class DataTableToEntity { /// <summary> /// 将DataTable数据源转换成实体类 /// </summary ...

随机推荐

  1. iOS9 CASpringAnimation 弹簧动画详解

    http://blog.csdn.net/zhao18933/article/details/47110469 1. CASpringAnimation iOS9才引入的动画类,它继承于CABaseA ...

  2. 【转】Sprague-Grundy函数

    http://www.cnitblog.com/weiweibbs/articles/42735.html 上一期的文章里我们仔细研究了Nim游戏,并且了解了找出必胜策略的方法.但如果把Nim的规则略 ...

  3. 2018-10-19-Roslyn-使用-Directory.Build.props-文件定义编译

    title author date CreateTime categories Roslyn 使用 Directory.Build.props 文件定义编译 lindexi 2018-10-19 18 ...

  4. MAYA安装失败怎样卸载重新安装MAYA,解决MAYA安装失败的方法总结

    技术帖:MAYA没有按照正确方式卸载,导致MAYA安装失败.楼主也查过网上关于如何解决MAYA安装失败的一些文章,是说删除几个MAYA文件和MAYA软件注册表就可以解决MAYA安装失败的问题,实际的情 ...

  5. oracle RANK() dense_rank()

    [语法]RANK ( ) OVER ( [query_partition_clause] order_by_clause ) dense_RANK ( ) OVER ( [query_partitio ...

  6. qt开发ROS gui界面环境配置过程总结

    这段时间花了点时间配置了在qtcreator5.9.1上开发ros gui界面的环境,终于可以实现导入工程,插断点调试了.总结起来需要注意以下几点: 1.安装插件ros_qtc_plugin,ROS与 ...

  7. 突然想起一个有趣的问题:FAT32&NTFS?

    在大学的时候老师提过一个有意思的问题4G的程序存储在什么格式以上的电脑? 首先普及一下两种格式的区别 FAT32:此硬盘格式不支持4GB以上大文件,使用32位文件分配表. NTFS:微软最新文件格式, ...

  8. poj1741 树上距离小于等于k的对数 点分治 入门题

    #include <iostream> #include <stdio.h> #include <string.h> #include <algorithm& ...

  9. 小程序中使用threejs

    webgl调试 起初使用threejs 在小程序里面调试,明明是按着官方的文档来,但是会发现开发者工具上面会提示getContext,经过一翻摸索,发现webgl调试只能在手机端调试. 总结:webg ...

  10. eclipse maven项目导出所使用的jar包

    在eclipse中定位到maven项目的pom.xml文件右击pom.xml文件,选择Run As-->Maven build…在打开的页面中,GOLAS栏输入“dependency:copy- ...