58 lines
2.0 KiB
Java
58 lines
2.0 KiB
Java
|
|
package com.gunshi.project.ss.service;
|
|||
|
|
|
|||
|
|
|
|||
|
|
import cn.hutool.poi.excel.ExcelUtil;
|
|||
|
|
import cn.hutool.poi.excel.ExcelWriter;
|
|||
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|||
|
|
import com.gunshi.project.ss.mapper.StEvpoMapper;
|
|||
|
|
import com.gunshi.project.ss.model.StEvpo;
|
|||
|
|
import jakarta.servlet.http.HttpServletResponse;
|
|||
|
|
import lombok.extern.slf4j.Slf4j;
|
|||
|
|
import org.springframework.stereotype.Service;
|
|||
|
|
import org.springframework.transaction.annotation.Transactional;
|
|||
|
|
|
|||
|
|
import java.io.IOException;
|
|||
|
|
import java.net.URLEncoder;
|
|||
|
|
import java.util.List;
|
|||
|
|
|
|||
|
|
@Service
|
|||
|
|
@Slf4j
|
|||
|
|
@Transactional(rollbackFor = Exception.class)
|
|||
|
|
public class StEvpoService extends ServiceImpl<StEvpoMapper, StEvpo> {
|
|||
|
|
public void export(HttpServletResponse response) {
|
|||
|
|
List<StEvpo> list = this.lambdaQuery().orderByAsc(StEvpo::getMonth).list();
|
|||
|
|
list.stream().forEach(o ->{
|
|||
|
|
o.setEvaporation(o.getEvaporation().setScale(2));
|
|||
|
|
});
|
|||
|
|
// 通过工具类创建writer
|
|||
|
|
ExcelWriter writer = ExcelUtil.getWriter();
|
|||
|
|
|
|||
|
|
// 自定义标题别名(只包含需要导出的字段)
|
|||
|
|
writer.addHeaderAlias("month", "月份");
|
|||
|
|
writer.addHeaderAlias("evaporation", "日蒸发量(万m³)");
|
|||
|
|
|
|||
|
|
// 只写出设置了别名的字段,默认写出所有字段
|
|||
|
|
writer.setOnlyAlias(true);
|
|||
|
|
|
|||
|
|
// 一次性写出内容,使用默认样式
|
|||
|
|
writer.write(list, true);
|
|||
|
|
|
|||
|
|
// 设置响应内容类型
|
|||
|
|
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
|
|||
|
|
|
|||
|
|
try {
|
|||
|
|
// 设置响应头信息
|
|||
|
|
String fileName = URLEncoder.encode("蒸发量数据", "UTF-8");
|
|||
|
|
response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");
|
|||
|
|
|
|||
|
|
// 将writer对象刷新到响应输出流中
|
|||
|
|
writer.flush(response.getOutputStream(), true);
|
|||
|
|
} catch (IOException e) {
|
|||
|
|
e.printStackTrace();
|
|||
|
|
} finally {
|
|||
|
|
// 关闭writer,释放内存
|
|||
|
|
writer.close();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|