自定义的属性,用来判断顺序的

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface ExcelAttribute {
/** 对应的列名称 */
String name() default ""; /** 列序号 */
int sort(); /** 字段类型对应的格式 */
String format() default ""; }

导入excel的类

package com.ihrm.common.poi.utils;

import com.ihrm.domain.poi.ExcelAttribute;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.format.CellFormat;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook; import java.io.FileInputStream;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List; public class ExcelImportUtil<T> { private Class clazz;
private Field fields[]; public ExcelImportUtil(Class clazz) {
this.clazz = clazz;
fields = clazz.getDeclaredFields();
} /**
* 基于注解读取excel
*/
public List<T> readExcel(InputStream is, int rowIndex,int cellIndex) {
List<T> list = new ArrayList<T>();
T entity = null;
try {
XSSFWorkbook workbook = new XSSFWorkbook(is);
Sheet sheet = workbook.getSheetAt(0);
// 不准确
int rowLength = sheet.getLastRowNum(); System.out.println(sheet.getLastRowNum());
for (int rowNum = rowIndex; rowNum <= sheet.getLastRowNum(); rowNum++) {
Row row = sheet.getRow(rowNum);
entity = (T) clazz.newInstance();
System.out.println(row.getLastCellNum());
for (int j = cellIndex; j < row.getLastCellNum(); j++) {
Cell cell = row.getCell(j);
for (Field field : fields) {
if(field.isAnnotationPresent(ExcelAttribute.class)){
field.setAccessible(true);
ExcelAttribute ea = field.getAnnotation(ExcelAttribute.class);
if(j == ea.sort()) {
field.set(entity, covertAttrType(field, cell));
}
}
}
}
list.add(entity);
}
} catch (Exception e) {
e.printStackTrace();
}
return list;
} /**
* 类型转换 将cell 单元格格式转为 字段类型
*/
private Object covertAttrType(Field field, Cell cell) throws Exception {
String fieldType = field.getType().getSimpleName();
if ("String".equals(fieldType)) {
return getValue(cell);
}else if ("Date".equals(fieldType)) {
return new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse(getValue(cell)) ;
}else if ("int".equals(fieldType) || "Integer".equals(fieldType)) {
return Integer.parseInt(getValue(cell));
}else if ("double".equals(fieldType) || "Double".equals(fieldType)) {
return Double.parseDouble(getValue(cell));
}else {
return null;
}
} /**
* 格式转为String
* @param cell
* @return
*/
public String getValue(Cell cell) {
if (cell == null) {
return "";
}
switch (cell.getCellType()) {
case STRING:
return cell.getRichStringCellValue().getString().trim();
case NUMERIC:
if (DateUtil.isCellDateFormatted(cell)) {
Date dt = DateUtil.getJavaDate(cell.getNumericCellValue());
return new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(dt);
} else {
// 防止数值变成科学计数法
String strCell = "";
Double num = cell.getNumericCellValue();
BigDecimal bd = new BigDecimal(num.toString());
if (bd != null) {
strCell = bd.toPlainString();
}
// 去除 浮点型 自动加的 .0
if (strCell.endsWith(".0")) {
strCell = strCell.substring(0, strCell.indexOf("."));
}
return strCell;
}
case BOOLEAN:
return String.valueOf(cell.getBooleanCellValue());
default:
return "";
}
}
}

model的例子

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor; import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import java.io.Serializable;
import java.util.Date; //转正申请
@Entity
@Table(name = "em_positive")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class EmployeePositive implements Serializable {
private static final long serialVersionUID = 2391824518947910773L;
/**
* 员工ID
*/
@Id
private String userId;
/**
* 转正日期
*/
@ExcelAttribute(sort=1)
private Date dateOfCorrection;
/**
* 转正评价
*/
@ExcelAttribute(sort=2)
private String correctionEvaluation;
/**
* 附件
*/
private String enclosure;
/**
* 单据状态 1是未执行,2是已执行
*/
private Integer estatus;
/**
* 创建时间
*/
private Date createTime;
}

具体代码复制传智播客

最新文章

  1. jquery写的ajax
  2. this
  3. 【JavaEE企业应用学习记录】验证配置
  4. C++学习笔记6:多文件编程
  5. 关于web.config中customErrors
  6. centos在yum install报错:Another app is currently holding the yum lock解决方法
  7. linux内核之链表操作解析
  8. LeetCode---Container With Most Water(11)
  9. Linux(CentOS) 查看当前占用CPU或内存最多的K个进程
  10. [vue三部曲]第一部:vue脚手架的搭建和目录资源介绍,超详细!
  11. ORM映射和路由系统
  12. NLP 工具类库
  13. Java NIO系列教程(二) Channel通道介绍及FileChannel详解
  14. 使用Linux工作之Fedora KDE
  15. 应用jfinal发送微信模板消息的一个bug
  16. H5移动端视频问题(苹果全屏播放问题等)
  17. Spring 知识点总结
  18. Daily Scrum02 12.03
  19. C# 教程(基础理论部分出自网络,一些上机结果为原创)
  20. Poj 2096 (dp求期望 入门)

热门文章

  1. python学习-Day22
  2. netty系列之:netty中的frame解码器
  3. go-micro集成RabbitMQ实战和原理
  4. 论文解读(SAGPool)《Self-Attention Graph Pooling》
  5. apache tomcat 目录session应用信息漏洞
  6. 力扣算法:125-验证回文串,131-分割回文串---js
  7. yarn/npm 设置镜像地址
  8. 824. Goat Latin - LeetCode
  9. 【单片机】CH32V103v8t6开发板调试笔记
  10. 魔改了一下bootstrap-treeview组件,发布个NPM包体验一下