版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_39220472/article/details/81952668
编译工具:eclipse
项目:maven+ssm
springmvc使用easypoi导出导入Excel表(2):导入Excel表
在pom.xml引入easypoi的依赖包:
<!--easypoi -->
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-base</artifactId>
<version>3.0.3</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-web</artifactId>
<version>3.0.3</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-annotation</artifactId>
<version>3.0.3</version>
</dependency>
导出Excel表:
前端页面:
<a href="exceportExcel">【导出】</a>
controller层:
/**
* 导出车位信息
* @param response
* @param page
*/
@RequestMapping("/exceportExcel")
public void exceportExcel(HttpServletResponse response,Page<CustomSpaceVo>page) {
if(page==null) {
page=new Page<CustomSpaceVo>();
}
try {
List<CustomSpaceVo>list=customSpaceService.resultSet(page);
Workbook workbook=ExcelExportUtil.exportExcel(new ExportParams("车位信息","车位租赁信息"), CustomSpaceVo.class,list);
String fileName="cars.xls";
//设置返回响应头
response.setContentType("application/xls;charset=UTF-8");
response.setHeader("Content-Disposition", "attachment;filename="+fileName);
OutputStream out = response.getOutputStream();
workbook.write(out);
out.flush();
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
service层:
// 获取租赁车位列表(分页)
List<CustomSpaceVo> resultSet(Page<CustomSpaceVo> page);
serviceImpl:
@Override
public List<CustomSpaceVo> resultSet(Page<CustomSpaceVo> page) {
return customSpaceMapper.resultSet(page);
}
mapper.xml:(如果你只是单表查询的,是要导出整个表的所有数据到Excel表的,那就直接select * from table)
<select id="resultSet"
resultType="com.st.eleventh.paramVo.CustomSpaceVo"
parameterType="com.st.eleventh.tools.Page">
select cs.*,p.*,w.w_name from t_ctms2space cs
LEFT JOIN
s_worker w
on cs.w_id=w.w_id
LEFT JOIN t_parking p
on cs.park=p.cg_id and cs.cs_state is null
<where>
<if test="params.type!=null and params.type=='csname'">
and cs.csname like concat('%', #{params.key} , '%')
</if>
<if test="params.type!=null and params.type=='cg_name'">
and p.cg_name like concat('%', #{params.key} , '%')
</if>
</where>
order by cs.cs_id desc
</select>
注意点:就是实体类要加注解:在要导出属性上加 @Excel(name="数据库字段的意思或自己随意命名")
model层:
(我要导出的实体类是vo类,vo类就是用来展示给用户需要看到的信息,但并不仅仅是对应一张表的信息,简单说就是查询多张表,把一些重要的字段返回给用户看)
package com.st.eleventh.model;
import java.io.Serializable;
import java.util.Date;
import cn.afterturn.easypoi.excel.annotation.Excel;
public class CustomSpace implements Serializable {
private Integer cs_id;
@Excel(name = "车主")
private String csname;
@Excel(name = "联系电话")
private String phone;
private String carid;
private String park;
@Excel(name = "车位编号")
private String parknum;
private Integer w_id;
@Excel(name = "租用类型")
private Integer renttype;
@Excel(name = "租用时间")
private Integer renttime;
@Excel(name = "开始时间")
private Date starttime;
@Excel(name = "到期时间")
private Date endtime;
private Integer cs_state;
//自己加setter,getter方法
}
package com.st.eleventh.paramVo;
import com.st.eleventh.model.CustomSpace;
import cn.afterturn.easypoi.excel.annotation.Excel;
public class CustomSpaceVo extends CustomSpace {
@Excel(name = "停车场")
private String cg_name;
@Excel(name = "月费")
private Float mz_cost;
@Excel(name = "年费")
private Float yz_cost;
@Excel(name = "工作人员")
private String w_name;
//自己加setter,getter方法
}
好了,导出Excel表就是这么简单。
思路就是:前端发送一个导出请求到controller,controller直接调用service层拿到想要导出的数据,然后直接把该数据放到已经ExcelExportUtil工具类中就好了,因为一切easypoi已经封装好了。唯一注意的就是:你要那个类负责封装要导出的数据,那个类的属性就要加上注解:
@Excel(name = "停车场")
private String cg_name;
效果图: