实体类 (我用的事mybatis-plus)
package gov.pbc.xjcloud.provider.information.entity;
import com.alibaba.excel.annotation.ExcelIgnore;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.format.DateTimeFormat;
import com.alibaba.excel.annotation.write.style.ColumnWidth;
import com.baomidou.mybatisplus.enums.IdType;
import java.util.Date;
import com.baomidou.mybatisplus.annotations.TableId;
import com.baomidou.mybatisplus.activerecord.Model;
import java.io.Serializable;
import lombok.*;
import lombok.experimental.Accessors;
/**
* <p>
* 证件领用备案表
* </p>
*
* @author panpengbo
* @since 2020-01-16
*/
@Data
@Accessors(chain = true)
@AllArgsConstructor
@NoArgsConstructor
public class AbroadReceive extends Model<AbroadReceive> {
private static final long serialVersionUID = 1L;
@TableId(value = "receive_id", type = IdType.AUTO)
@ExcelIgnore
private Long receiveId;
/**
* 姓名
*/
@ExcelProperty(value = "姓名",index = 0)
private String receiveName;
/**
* 性别 0 男 1 女
*/
@ExcelProperty(value = "性别",index = 1)
private String sex;
/**
* 证件领用类型
*/
@ExcelProperty(value = "证件领用类型",index = 2)
@ColumnWidth(15)
private String documentType;
/**
* 申请日期
*/
@ColumnWidth(18)
@ExcelProperty(value = "申请日期",index = 3)
@DateTimeFormat("yyyy-MM-dd")
private Date dateOfApplication;
/**
* 出国(境)时间
*/
@ColumnWidth(18)
@DateTimeFormat("yyyy-MM-dd")
@ExcelProperty(value = "出国(境)时间",index = 4)
private Date goAbroadTime;
/**
* 回国(境)时间
*/
@ColumnWidth(18)
@DateTimeFormat("yyyy-MM-dd")
@ExcelProperty(value = "回国(境)时间",index = 5)
private Date homeTime;
/**
* 出国(境)天数
*/
@ColumnWidth(20)
@ExcelProperty(value = "出国(境)天数",index = 6)
private Integer goAbroadDays;
/**
* 出国事由
*/
@ExcelProperty(value = "出国事由",index = 7)
@ColumnWidth(20)
private String goAbroadCause;
/**
* 到达国家(地区)
*/
@ColumnWidth(15)
@ExcelProperty(value = "到达国家(地区)",index = 8)
private String arrivalStateRegion;
/**
* 创建时间
*/
@ExcelIgnore
private Date createTime;
/**
* 修改时间
*/
@ExcelIgnore
private Date updateTime;
/**
* 删除标志
*/
@ExcelIgnore
private String deleteFlag;
@Override
protected Serializable pkVal() {
return this.receiveId;
}
}
下载的工具类()
public void download(HttpServletResponse response, List<?> list,Class clazz,String fileName) {
try {
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
// 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
String filename = URLEncoder.encode(fileName,"utf-8");
response.setHeader("Content-disposition", "attachment;filename=" + filename + ".xls");
// 这里需要设置不关闭流
EasyExcel.write(response.getOutputStream(), clazz).autoCloseStream(Boolean.FALSE).sheet("全行")
.doWrite(list);
} catch (Exception e) {
// 重置response
response.reset();
response.setContentType("application/json");
response.setCharacterEncoding("utf-8");
R<String> r = new R<>();
r.setCode(CommonConstants.FAIL);
r.setMsg("导出失败!");
r.setData(e.getMessage());
try {
response.getWriter().println(r);
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
控制层
@GetMapping("/abroadExport")
public void export(HttpServletResponse response,AbroadReceive abroadReceive) throws IOException {
EntityWrapper<AbroadReceive> wrapper = selectAbroad(abroadReceive);
List<AbroadReceive> list = baseService.selectList(wrapper);
System.out.println(list);
//ExcelUtils.exportModule("证件信息",response,AbroadReceive.class,baseService.selectList(wrapper));
download(response,baseService.selectList(wrapper),AbroadReceive.class,"www");
}
基本就完成了@@@@