使用easypoi进行excel简单导出
1、项目环境配置好后,在pom文件中添加EasyPoi依赖
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-base</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-web</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-annotation</artifactId>
<version>4.1.0</version>
</dependency>
2、在要导出的表对应的实体类中添加注解
public class Applys {
@ExcelIgnore
private Integer id;
@Excel(name="考试批次")
private String examNo;
@Excel(name="姓名")
private String realName;
@Excel(name="性别")
private String sex;
@Excel(name="身份证号")
private String idNo;
@Excel(name="籍贯")
private String homeTown;
@Excel(name="文化程度")
private String highestDegree;
@Excel(name="从业年限")
private String workYears;
@Excel(name="职业工种")
private String workTypeName;
@ExcelIgnore
private String workName;
@Excel(name="工种代码")
private Integer workTypeCode;
@Excel(name="技能等级")
private String skillGrade;
@Excel(name="等级代码")
private String skillGradeCode;
@Excel(name="工作单位")
private String workCompany;
@ExcelIgnore
private Date createDate;
@ExcelIgnore
private String phoneNo;
@ExcelIgnore
private Integer createUserId;
@ExcelIgnore
private String state;
}
@Excel(name="考试批次")
该注解说明该字段对应Excel表格中的表头名称
@ExcelIgnore
该注解表示该字段不进行导入导出
3、dao层
/**
* 查询所有数据
* @return
* @author
*/
List<Applys> queryAll();
4、dao实现层
<sql id="Base_Column_List">
id, real_name, sex, id_no, home_town, highest_degree, work_years, work_type_name,
work_name, work_type_code, skill_grade, skill_grade_code, work_company, exam_no,
create_date, phone_no, create_user_id, state
</sql>
<!--查询所有-->
<select id="queryAll" resultType="com.example.excel.entity.Applys">
select
<include refid="Base_Column_List" />
from apply
</select>
5、service 及service实现
/**
* 查询所有数据
* @return
* @author
*/
List<Applys> queryAll();
@Override
public List<Applys> queryAll() {
return dao.queryAll();
}
6、controller层
/**
* 导出数据
* @param response
*/
@PostMapping(value = "/export",produces = "application/octet-stream")
public void exportTsgz(HttpServletResponse response){
List<Applys> list = ApplyService.queryAll();
ExportParams params = new ExportParams("报名表","报名表", ExcelType.HSSF);
Workbook workbook = ExcelExportUtil.exportExcel(params, Applys.class, list);
ServletOutputStream out = null;
try {
//流的形式传输数据
response.setHeader("content-type","application/octet-stream");
//防止中文乱码
response.setHeader("content-disposition","attachment;filename="+ URLEncoder.encode("报名表.xls","UTF-8"));
out=response.getOutputStream();
workbook.write(out);
}catch (IOException e){
e.printStackTrace();
}finally {
if (null!=out){
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
7、前端页面
<div class="layui-btn-container">
<button type="button" class="layui-btn btdisabled" id="export">开始导出</button>
</div>
js代码
var postDownLoadFile = function (options) {
var config = $.extend(true, {
method: 'post' }, options);
var $iframe = $('<iframe id="down-file-iframe" />');
var $form = $('<form target="down-file-iframe" method="' + config.method + '" />');
$form.attr('action', config.url);
for (var key in config.data) {
$form.append('<input type="hidden" name="' + key + '" value="' + config.data[key] + '" />');
}
$iframe.append($form);
$(document.body).append($iframe);
$form[0].submit();
$iframe.remove();
}
$("#export").click(function() {
var url = 'excel/export';
var param={
};
postDownLoadFile({
url:url,
data:param,
method:'post'
});
});