hibernate+servlet+mysql 实现easypoi_springboot项目整合easypoi实现导入导出功能

导入依赖

在导入依赖钱看是否项目中存在poi相关的版本。直接从pom文件中去除exclusion。java

本次使用的是4.2.0 。maven依赖以下:spring

cn.afterturn

easypoi-spring-boot-starter

4.2.0

org.hibernate.validator

hibernate-validator

6.1.6.Final

org.apache.poi

poi

4.1.0

cn.afterturn

easypoi-base

4.0.0

com.google.guava

guava

commons-io

commons-io

2.6

貌似没用到easypoi-base 。先无论了sql

按照官方说明。建立实体

官方文档;数据库

public class MaterialExport implements Serializable {

// 物资名称

@Excel(name = "物资名称", width = 15)

private String name;

// 物资规格

@Excel(name = "物资规格")

private String specs;

// 物资类型

@Excel(name = "物资类型")

private String type;

// 单位

@Excel(name = "单位")

private String unit;

// 库存

@Excel(name = "库存")

private long num;

//单价

@Excel(name = "单价")

private BigDecimal unitprice;

// 厂家

@Excel(name = "厂家", width = 20)

private String manufactor;

// 库存位置

@Excel(name = "库存位置", width = 20)

private String address;

// 用途

@Excel(name = "用途", width = 20)

private String purpose;

@Excel(name = "入库时间",format = "yyyy年MM月dd日", width = 25)

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss")

@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")

private Date createTime;

// 备注

@Excel(name = "备注", width = 40)

private String remarks;

//getter setter方法省略。

在导入时须要注意:实体中用到的属性为java基本属性或者是在“java.lang”、“java.math”、“java.sql”、“java.util”包中具体easypoi中逻辑代码为:apache

/**

* 是否是java基础类

*

* @param field

* @return

*/

public static boolean isJavaClass(Field field) {

Class> fieldType = field.getType();

boolean isBaseClass = false;

if (fieldType.isArray()) {

isBaseClass = false;

} else if (fieldType.isPrimitive() || fieldType.getPackage() == null

|| "java.lang".equals(fieldType.getPackage().getName())

|| "java.math".equals(fieldType.getPackage().getName())

|| "java.sql".equals(fieldType.getPackage().getName())

|| "java.util".equals(fieldType.getPackage().getName())) {

isBaseClass = true;

}

return isBaseClass;

}

导出功能

获取数据后直接调用Util方法

controller:app

public Result export(HttpServletRequest request, HttpServletResponse response) {

System.out.println(sdf.format(new Date()));

//此处为从数据库获取list

List materialExports = materialService.export();

response.setContentType("application/vnd.ms-excel");

OutputStream out = null;

try {

response.setHeader("Content-disposition",

"attachment;filename=" + URLEncoder.encode("物资导出" + sdf.format(new Date()) + ".xls","ISO-8859-1" ));

Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("物资库存信息", "物资"),

MaterialExport.class, materialExports);

out = response.getOutputStream();

workbook.write(out);

} catch (Exception e) {

System.out.println(e.getMessage());

System.out.println(e);

} finally {

try {

if (out != null)

out.close();

} catch (IOException e) {

}

}

return Response.success();

}

导入

从文件流中获取数据用easypoi解析数据:maven

public Result inport(HttpServletRequest request,@RequestParam("file") MultipartFile fileItem) {

ImportParams importParams = new ImportParams();

importParams.setHeadRows(2);

// 获取模板

ExcelImportResult data = null;

try {

data = ExcelImportUtil.importExcelMore(fileItem.getInputStream(), MaterialExport.class, importParams);

} catch (Exception e) {

e.printStackTrace();

}

// 获取模板数据

List successList = data.getList();

int total = successList.size();

// 循环数据

System.out.println(total);

for (MaterialExport excelDTO : successList) {

System.out.println(excelDTO.toString());

}

return Response.success();

}

完毕。spring-boot

本文由博客群发一文多发等运营工具平台 OpenWrite 发布工具

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值