easyExcel导出
实现数据库查询导出,dto转model
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.2.2</version>
</dependency>
1、ExcelAdminDto
package com.macro.mall.dto;
import com.alibaba.excel.annotation.ExcelIgnore;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.format.DateTimeFormat;
import lombok.Data;
import java.util.Date;
@Data
public class ExcelAdminDto {
@ExcelIgnore
private Long id;
@ExcelProperty(value = "用户名",index = 0)
private String userName;
@ExcelProperty(value = "密码",index = 1)
private String passWord;
@ExcelProperty(value = "头像",index = 2)
private String icon;
@ExcelProperty(value = "邮件",index = 3)
private String email;
@ExcelProperty(value = "昵称",index = 4)
private String nickName;
@ExcelProperty(value = "备注信息",index = 5)
private String note;
@ExcelProperty(value = "创建时间",index = 6)
private String createTime;
@ExcelProperty(value = "登录时间",index = 7)
private String loginTime;
/**
* 0-禁用 1-启用
*/
@ExcelProperty(value = "状态",index = 8)
private String status;
}
###时间直接用string。将model的date直接转换成string。状态也用String
2、excel导出类
DownExcel
package com.macro.mall.config;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
public class DownExcel {
public static void download(HttpServletResponse response, Class t, List list) throws IOException, IllegalAccessException,InstantiationException {
response.setContentType("application/vnd.ms-excel");// 设置文本内省
response.setCharacterEncoding("utf-8");// 设置字符编码
response.setHeader("Content-disposition", "attachment;filename=demo.xlsx"); // 设置响应头
EasyExcel.write(response.getOutputStream(), t).sheet("模板")
//设置自适应列宽
.head(t).registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
.doWrite(list); //用io流来写入数据
}
}
3、controller层
UmsAdminController
//导出为Excel
@RequestMapping(value = "/downloadexcel",method = RequestMethod.GET)
@ApiOperation("导出用户")
public void getExcel(HttpServletResponse response) throws IllegalAccessException, IOException,
InstantiationException {
// 获取用户列表
List<ExcelAdminDto> list = adminService.getAllUser();
DownExcel.download(response, ExcelAdminDto.class,list);
}
UmsAdminService
List<ExcelAdminDto> getAllUser();
UmsAdminServiceImpl
@Override
public List<ExcelAdminDto> getAllUser() {
List<UmsAdmin> allUser = adminMapper.getAllUser();
List<ExcelAdminDto> excelAdminDtoList = new ArrayList<>();
if (CollUtil.isNotEmpty(allUser)){
allUser.forEach(
(item) -> {
ExcelAdminDto excelAdminDto = new ExcelAdminDto();
excelAdminDto.setId(item.getId());
excelAdminDto.setUserName(item.getUsername());
excelAdminDto.setPassWord(item.getPassword());
excelAdminDto.setNickName(item.getNickName());
Date loginTime = item.getLoginTime();
if (loginTime != null){
StringBuilder stringBuilder = new StringBuilder(" ");
Date loginTime1 = DateUtil.date(loginTime);
String format = DateUtil.format(loginTime1, "yyyy-MM-dd HH:mm:ss");
excelAdminDto.setLoginTime(stringBuilder.append(format).toString());
}
Date createTime = item.getCreateTime();
if (createTime != null){
StringBuilder stringBuilder = new StringBuilder(" ");
Date createTime1 = DateUtil.date(createTime);
String createTimeStr = DateUtil.format(createTime1,"yyyy-MM-dd HH:mm:ss");
excelAdminDto.setCreateTime(stringBuilder.append(createTimeStr).toString());
}
excelAdminDto.setStatus(item.getStatus() == 1 ? "启用" : "禁用");
excelAdminDto.setNote(item.getNote());
excelAdminDto.setIcon(item.getIcon());
excelAdminDto.setEmail(item.getEmail());
excelAdminDtoList.add(excelAdminDto);
}
);
}
return excelAdminDtoList;
}
}
UmsAdminMapper
List<UmsAdmin> getAllUser();
UmsAdminMapper.xml
<select id="getAllUser" resultMap="BaseResultMap">
select
<include refid="Base_Column_List"/>
from ums_admin
</select>
<sql id="Base_Column_List">
id, username, password, icon, email, nick_name, note, create_time, login_time, status
</sql>
###自适应列宽目前无对应优化算法。且行且用。