**
将数据导出xlsx
**
//EasyExcel 导出
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.0.5</version>
</dependency>
public void export(HttpServletResponse response) throws Exception {
try {
//查询需要导出的数据
List<Info> listInfo =infoMapper.query();
if (CollectionUtils.isEmpty(listInfo)) {
response.setStatus(HttpStatus.NOT_FOUND.value());
} else {
response.setHeader("content-encoding","gzip");
GZIPOutputStream os = new GZIPOutputStream(response.getOutputStream());
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
response.setHeader("Content-disposition", "attachment;filename=列表.xlsx");
EasyExcel.write(os, infoVo.class).sheet().doWrite(listInfo);
}
}catch (Exception e) {
log.error("获取列表失败", e);
throw new Exception("获取列表失败");
}
}
@Data
public class infoVo{
@ExcelProperty("省份")
private String provinceName;
@ExcelProperty("市")
private String cityName;
@ExcelProperty("区/县")
private String districtName;
@ExcelProperty("大区")
private String regionalName
@ExcelProperty("时间")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone="GMT+8")
private Date time;
}
//第二种方式:输出流导出
public void export(HttpServletResponse response) {
Map<String, Object> map = new HashMap<>();
map.put("forestryInspectionVo", forestryInspectionVo);
List<Info> list=infoMapper.query();
try {
String fileName = "表.xls"; // 文件名
String sheetName = "列表";// sheet名
String[] title = new String[]{"位置","名称","类型"
};
String content[][] = new String[list.size()][];
for (int i = 0; i < list.size(); i++) {
content[i] = new String[title.length];
content[i][0] = list.get(i).getPositioning()==null?null:list.get(i).getPositioning().toString();
content[i][1] = list.get(i).getUserName()==null?null:list.get(i).getUserName().toString();
content[i][2] = list.get(i).getTypeName()==null?null:list.get(i).getTypeName().toString();
}
HSSFWorkbook wb = ExcelUtilPro.getHSSFWorkbook(sheetName, title, content, null);
// 输出Excel文件
OutputStream output = response.getOutputStream();
response.setHeader("Content-Disposition", "attachment; filename="
+ new String(fileName.getBytes("utf-8"),"ISO-8859-1"));
wb.write(output);
output.flush();
output.close();
} catch (Exception e) {
e.printStackTrace();
}
}