前端我用的是vue写的代码
导出只需要这一行代码
<el-button @click="exportExcel"> 导出</el-button>
这里面我导出是根据查询的条件导出查询的内容
exportExcel() {
这行代码是要获取城市的名称
const city = this.formItem.busiRegCityCode || '';
这行是要获取取区县的名称
const county = this.formItem.busiRegCountyCode || '';
这里是获取网格
const grid = this.formItem.busiRegGridCode || '';
这里是获取日期的年月
let date = new Date(this.formItem.createMonth);
const year = date.getFullYear();
let month = date.getMonth() + 1;
month = month < 10 ? '0' + month : month.toString();
const formatDate =`${year}-${month}`;
以上的是我自己的业务逻辑代码 仅供参考 具体需要自己的业务逻辑去改
获取到自己url路径 路径需要自己配 我的路径是放到路由里面了 所以这个根据自己的url来配
let url = this.$api.url.businessDetail.export;
// 创建一个 Map 对象来存储查询参数
let params = new Map();
params.set('city', city);
params.set('county', county);
params.set('grid', grid);
params.set('createMonth', formatDate);
// 使用 URLSearchParams 对象来构建查询字符串
let urlParams = new URLSearchParams(params);
// 将查询字符串附加到 URL
url += '?' + urlParams.toString();
// 重定向到 URL
window.location = url;
},
以上是我写的业务前端代码
接下来是后端代码
Controller层 我是用的最笨的方法 把条件全都拿出来了 然后一个个去请求 练手的可以试试
@RequestMapping("/exportExcel")
public void exportExcel( @RequestParam("city") String city,
@RequestParam("county") String county,
@RequestParam("grid") String grid,
@RequestParam("createMonth") String createMonth,HttpServletResponse response ) throws IOException {
iBusinessDetailService.generateExcel(city,county,grid,createMonth,response);
}
service层
public interface IBusinessDetailService {
void generateExcel(String city, String county, String grid,String createMonth ,HttpServletResponse response) throws IOException;
}
serviceImpl层
@Override
public void generateExcel(String city, String county, String grid, String createMonth, HttpServletResponse response) throws IOException {
//这下面都是我的业务需求 可以不必看 我只是把2024-12这种格式拆成2024和12放到年和月里面了
String[] parts = createMonth.split("-");
if (parts.length != 2) {
throw new IllegalArgumentException("Invalid createMonth format: " + createMonth);
}
String year = parts[0];
String month = parts[1];
//去数据库里查数据放到list里面
List<BusinessDetailVO> dataList = businessDetailMapper.getExPost(city, county, grid, year, month, response);
// 定义表头样式
WriteCellStyle headWriteCellStyle = new WriteCellStyle();
headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER); // 居中对齐
headWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER); // 垂直居中对齐
headWriteCellStyle.setBorderBottom(BorderStyle.THIN); // 下边框
headWriteCellStyle.setBorderTop(BorderStyle.THIN); // 上边框
headWriteCellStyle.setBorderLeft(BorderStyle.THIN); // 左边框
headWriteCellStyle.setBorderRight(BorderStyle.THIN); // 右边框
headWriteCellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex()); // 背景颜色
// 定义内容样式
WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.LEFT); // 水平左对齐
contentWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER); // 垂直居中对齐
contentWriteCellStyle.setBorderBottom(BorderStyle.THIN); // 下边框
contentWriteCellStyle.setBorderTop(BorderStyle.THIN); // 上边框
contentWriteCellStyle.setBorderLeft(BorderStyle.THIN); // 左边框
contentWriteCellStyle.setBorderRight(BorderStyle.THIN); // 右边框
// 创建样式策略
HorizontalCellStyleStrategy styleStrategy = new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
以上从 // 定义表头样式到创建样式策略是可以不用要的 就是为了让表格变得更好看而已,如果看不明白 可以去EasyExcel官方文档去看看
// 设置响应头
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setCharacterEncoding("utf-8");
String fileName = URLEncoder.encode("网格通业务办理量明细", "UTF-8").replaceAll("\\+", "%20");
response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
// 写入数据并应用样式策略 里面你需要定义一个实体类 BusinessDetailVO.class这种的 定义是为了把你需要的数据通过实体类导出去
EasyExcel.write(response.getOutputStream(), BusinessDetailVO.class)
.registerWriteHandler(styleStrategy)
.sheet("网格通业务办理量明细")
.doWrite(dataList);
}
BusinessDetailVO 实体类 @ExcelProperty(value = "地市")注解是用来定义表头的
public class BusinessDetailVO implements Serializable {
private static final long serialVersionUID = -2756710144226695139L;
private String id;
@ExcelProperty(value = "地市")
private String eparchyName;
@ExcelProperty(value = "区县")
private String countryName;
@ExcelProperty(value = "网格")
private String gridName;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getEparchyName() {
return eparchyName;
}
public void setEparchyName(String eparchyName) {
this.eparchyName = eparchyName;
}
public String getCountryName() {
return countryName;
}
public void setCountryName(String countryName) {
this.countryName = countryName;
}
public String getGridName() {
return gridName;
}
public void setGridName(String gridName) {
this.gridName = gridName;
}
Mapper层
List<BusinessDetailVO> getExPost( @RequestParam("city") String city,
@RequestParam("county") String county,
@RequestParam("grid") String grid,
@RequestParam("year") String year,
@RequestParam("month") String month,
HttpServletResponse response);
后面你就根据你的需求去mapper.xml里面去写sql就行了