aliEasyExcel导入MySQL&&导出功能
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>3.1.0</version>
</dependency>
以继承mysqlPLUS为例




写出功能
interface
public interface HuntingKillService extends IService<HuntingKill> {
void exportData(HttpServletResponse response);
}
impl
package com.withyou.huntingkill.Service.impl;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.withyou.huntingkill.Entity.HuntingKill;
import com.withyou.huntingkill.Mapper.HuntingKillMapper;
import com.withyou.huntingkill.Service.HuntingKillService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
@Service
public class HuntingKillServiceImpl extends ServiceImpl<HuntingKillMapper, HuntingKill> implements HuntingKillService {
@Autowired
private HuntingKillMapper huntingKillMapper;
@Override
public void exportData(HttpServletResponse response) {
// 设置下载信息
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
// 设置指定名字
String name="KillName";
response.setHeader("Content-disposition","attachment;filename="+name+".xlsx");
// QueryDataBase huntingKillMapper也继承了baseMapper,随便选一个
List<HuntingKill> list =huntingKillMapper.selectList(null);
list.add(new HuntingKill());
try {
EasyExcel.write(response.getOutputStream(),HuntingKill.class).sheet("KillName").doWrite(list);
}catch (Exception e){
e.printStackTrace();
}
}
}
Controller
package com.withyou.huntingkill.Controller;
import com.withyou.huntingkill.Service.HuntingKillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
@RestController
public class HuntingKillController {
@Autowired
private HuntingKillService huntingKillService;
@GetMapping("exportData")
public void exportData(HttpServletResponse response){
huntingKillService.exportData(response);
}
}
mapper就继承mybatisPlus的或者自己写个查询全部的方法
//注意实体首字母一定要小写,要不然导出数据会是空的!

浙公网安备 33010602011771号