控制层
//导出数据字典接口
@GetMapping("exportData")
public void exportDict(HttpServletResponse response) throws IOException {
dictService.exportDictData(response);
}
业务层
//导出数据字典接口
@Override
public void exportDictData(HttpServletResponse response) throws IOException {
//设置下载信息
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
String fileName = "dict";
response.setHeader("Content-disposition", "attachment;filename="+ fileName + ".xlsx");
//查询数据库
List<Dict> dictList = baseMapper.selectList(null);
// baseMapper.selectAllll("666","hdiuhaihda");
//Dict -- DictEeVo
List<DictEeVo> dictVoList = new ArrayList<>();
for(Dict dict:dictList) {
DictEeVo dictEeVo = new DictEeVo();
// dictEeVo.setId(dict.getId());
BeanUtils.copyProperties(dict,dictEeVo);
dictVoList.add(dictEeVo);}
//调用方法进行写操作
ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).build();
//获取sheet0对象
WriteSheet mainSheet = EasyExcel.writerSheet(0, "模型信息").head(DictEeVo.class).build();
//获取模型信息,向sheet0写入数据
excelWriter.write(dictList, mainSheet);
//获取sheet1对象
WriteSheet detailSheet = EasyExcel.writerSheet(1, "词条666信息").head(DictEeVo.class).build();
excelWriter.write(dictList, detailSheet);
//关闭流
excelWriter.finish();
/* try {
EasyExcel.write(response.getOutputStream(), DictEeVo.class).sheet(0,"dict")
.doWrite(dictVoList);
EasyExcel.write(response.getOutputStream(), DictEeVo.class).sheet(1,"dict1")
.doWrite(dictVoList);
} catch (IOException e) {
e.printStackTrace();
}*/
}
javabean
@Data
public class DictEeVo {
@ExcelProperty(value = "id" )
@DateTimeFormat
private Long id;
@ExcelProperty(value = "名称")
private String name;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package com.atguigu.yygh.model.cmn;
import com.atguigu.yygh.model.base.BaseEntity;
import com.baomidou.mybatisplus.annotation.*;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
/**
* <p>
* Dict
* </p>
*
* @author qy
*/
@Data
@ApiModel(description = "数据字典")
@TableName("t_test")
public class Dict {
private static final long serialVersionUID = 1L;
public Dict(Long id, String name, Date date) {
this.id = id;
this.name = name;
this.date = date;
}
@ApiModelProperty(value = "id")
private Long id;
public static long getSerialVersionUID() {
return serialVersionUID;
}
public Dict() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@ApiModelProperty(value = "名称")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@ApiModelProperty(value = "名称")
@TableField("name")
private String name;
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
@ApiModelProperty(value = "名称")
@TableField("date")
private Date date;
@Override
public String toString() {
return "Dict{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}