1、首先得先引用easyExcel的版本依赖,我那
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.2.6</version>
</dependency>
2、然后得弄直接的实体类,(这个实体类是关键,主要的功能就是为了导出的表头还有大标题做准备)
解释: @ExcelProperty(value ={"${bigHead}","区划名称"},index = 0)
这个主要 ${bigHead} 这个是 大标题 ,这个标题是动态生成的,
package com.inspur.grp.hrss.labour.project.labourReport;
import com.alibaba.excel.annotation.ExcelIgnore;
import com.alibaba.excel.annotation.ExcelProperty;
import com.inspur.grp.hrss.labour.framework.base.BaseDomain;
import lombok.EqualsAndHashCode;
/**
* 每日更新统计记录(这个是是抽取出来的)对象 labour_day_updatetime
*
*/
@EqualsAndHashCode
public class LabourDayUpdatetime extends BaseDomain {
/** 主键 */
@ExcelIgnore
private String id;
@ExcelProperty(value ={"${bigHead}","区划名称"},index = 0)
private String mc;
/** 区的编码 */
@ExcelIgnore
private String area;
/** 镇的编码 */
@ExcelIgnore
private String town;
/** 村的编码 */
@ExcelIgnore
private String community;
/** 就业去向 */
@ExcelIgnore
private String employDestination;
/** 人员类别 */
@ExcelIgnore
private String category;
/** 是否就业 */
@ExcelIgnore
private String isEmployed;
/** 农村劳动力人数 */
@ExcelProperty(value ={"${bigHead}","农村劳动力人数"},index = 1)
private String ncldlrs;
/** 更新人数 */
@ExcelProperty(value ={"${bigHead}","农村劳动力更新人数"},index = 2)
private String gxrsa;
@ExcelProperty(value ={"${bigHead}","更新率"},index = 3)
private String gxla;
/** 就业人数 */
@ExcelProperty(value ={"${bigHead}","就业人数"},index = 4)
private String jyrsa;
@ExcelProperty(value ={"${bigHead}","国外务工(劳动力)"},index = 5)
private String guowaiwug;
@ExcelProperty(value ={"${bigHead}","省外务工(劳动力)"},index = 6)
private String shengwai;
@ExcelProperty(value ={"${bigHead}","省外市内务工(劳动力)"},index = 7)
private String shiwaishengnei;
@ExcelProperty(value ={"${bigHead}","县外市内务工(劳动力)"},index = 8)
private String xianwaishinwug;
@ExcelProperty(value ={"${bigHead}","乡(镇)外县内务工(劳动力)"},index = 9)
private String xiangzhenwaixianneiwug;
@ExcelProperty(value ={"${bigHead}","乡(镇)内务工(劳动力)"},index = 10)
private String xiangzhennwug;
@ExcelProperty(value ={"${bigHead}","就业率(劳动力)"},index = 11)
private String jyla;
/** 脱贫劳动力人数 */
@ExcelProperty(value ={"${bigHead}","脱贫劳动力人数"},index = 12)
private String tpldlrs;
@ExcelProperty(value ={"${bigHead}","更新人数(脱贫)"},index = 13)
private String gxrsb;
@ExcelProperty(value ={"${bigHead}","更新率(脱贫)"},index = 14)
private String gxlb;
@ExcelProperty(value ={"${bigHead}","就业人数(脱贫)"},index = 15)
private String jyrsb;
@ExcelProperty(value ={"${bigHead}","国外务工(脱贫)"},index = 16)
private String tpguowaiwug;
@ExcelProperty(value ={"${bigHead}","省外务工(脱贫)"},index = 17)
private String tpshengwai;
@ExcelProperty(value ={"${bigHead}","市外省内务工(脱贫)"},index = 18)
private String tpshiwaishengnei;
@ExcelProperty(value ={"${bigHead}","县外市内务工(脱贫)"},index = 19)
private String tpxianwaishinwug;
@ExcelProperty(value ={"${bigHead}","乡(镇)外县内务工(脱贫)"},index = 20)
private String tpxiangzhenwaixianneiwug;
@ExcelProperty(value ={"${bigHead}","乡(镇)内务工(脱贫)"},index = 21)
private String tpxiangzhennwug;
@ExcelProperty(value ={"${bigHead}","就业率(脱贫)"},index = 22)
private String jylb;
@ExcelProperty(value ={"${bigHead}","易迁劳动力人数"},index = 23)
private String yqldlrs;
@ExcelProperty(value ={"${bigHead}","更新人数(易迁)"},index = 24)
private String gxrsc;
@ExcelProperty(value ={"${bigHead}","更新率(易迁)"},index = 25)
private String gxlc;
@ExcelProperty(value ={"${bigHead}","就业人数(易迁)"},index = 26)
private String jyrsc;
@ExcelProperty(value ={"${bigHead}","国外务工(易迁)"},index = 27)
private String yqguowaiwug;
@ExcelProperty(value ={"${bigHead}","省外务工(易迁)"},index = 28)
private String yqshengwai;
@ExcelProperty(value ={"${bigHead}","市外省内(易迁)"},index = 29)
private String yqshiwaishengnei;
@ExcelProperty(value ={"${bigHead}","县外市内务工(易迁)"},index = 30)
private String yqxianwaishinwug;
@ExcelProperty(value ={"${bigHead}","乡(镇)外县内务工(易迁)"},index = 31)
private String yqxiangzhenwaixianneiwug;
@ExcelProperty(value ={"${bigHead}","乡(镇)内务工(易迁)"},index = 32)
private String yqxiangzhennwug;
@ExcelProperty(value ={"${bigHead}","就业率(易迁)"},index = 33)
private String jylc;
@ExcelProperty(value ={"${bigHead}","未更新的人数"},index = 34)
private String xtzwgxdldlrs;
public String getMc() {
return mc;
}
public void setMc(String mc) {
this.mc = mc;
}
public String getGxla() {
return gxla;
}
public void setGxla(String gxla) {
this.gxla = gxla;
}
public String getJyla() {
return jyla;
}
public void setJyla(String jyla) {
this.jyla = jyla;
}
public String getGxlb() {
return gxlb;
}
public void setGxlb(String gxlb) {
this.gxlb = gxlb;
}
public String getJylb() {
return jylb;
}
public void setJylb(String jylb) {
this.jylb = jylb;
}
public String getGxlc() {
return gxlc;
}
public void setGxlc(String gxlc) {
this.gxlc = gxlc;
}
public String getJylc() {
return jylc;
}
public void setJylc(String jylc) {
this.jylc = jylc;
}
public String getXtzwgxdldlrs() {
return xtzwgxdldlrs;
}
public void setXtzwgxdldlrs(String xtzwgxdldlrs) {
this.xtzwgxdldlrs = xtzwgxdldlrs;
}
public void setId(String id)
{
this.id = id;
}
public String getId()
{
return id;
}
public void setArea(String area)
{
this.area = area;
}
public String getArea()
{
return area;
}
public void setTown(String town)
{
this.town = town;
}
public String getTown()
{
return town;
}
public void setCommunity(String community)
{
this.community = community;
}
public String getCommunity()
{
return community;
}
public void setEmployDestination(String employDestination)
{
this.employDestination = employDestination;
}
public String getEmployDestination()
{
return employDestination;
}
public void setCategory(String category)
{
this.category = category;
}
public String getCategory()
{
return category;
}
public void setIsEmployed(String isEmployed)
{
this.isEmployed = isEmployed;
}
public String getIsEmployed()
{
return isEmployed;
}
public void setNcldlrs(String ncldlrs)
{
this.ncldlrs = ncldlrs;
}
public String getNcldlrs()
{
return ncldlrs;
}
public void setGxrsa(String gxrsa)
{
this.gxrsa = gxrsa;
}
public String getGxrsa()
{
return gxrsa;
}
public void setJyrsa(String jyrsa)
{
this.jyrsa = jyrsa;
}
public String getJyrsa()
{
return jyrsa;
}
public void setTpldlrs(String tpldlrs)
{
this.tpldlrs = tpldlrs;
}
public String getTpldlrs()
{
return tpldlrs;
}
public void setGxrsb(String gxrsb)
{
this.gxrsb = gxrsb;
}
public String getGxrsb()
{
return gxrsb;
}
public void setJyrsb(String jyrsb)
{
this.jyrsb = jyrsb;
}
public String getJyrsb()
{
return jyrsb;
}
public void setYqldlrs(String yqldlrs)
{
this.yqldlrs = yqldlrs;
}
public String getYqldlrs()
{
return yqldlrs;
}
public void setGxrsc(String gxrsc)
{
this.gxrsc = gxrsc;
}
public String getGxrsc()
{
return gxrsc;
}
public void setJyrsc(String jyrsc)
{
this.jyrsc = jyrsc;
}
public String getJyrsc()
{
return jyrsc;
}
public void setShengwai(String shengwai)
{
this.shengwai = shengwai;
}
public String getShengwai()
{
return shengwai;
}
public void setShiwaishengnei(String shiwaishengnei)
{
this.shiwaishengnei = shiwaishengnei;
}
public String getShiwaishengnei()
{
return shiwaishengnei;
}
public void setTpshengwai(String tpshengwai)
{
this.tpshengwai = tpshengwai;
}
public String getTpshengwai()
{
return tpshengwai;
}
public void setTpshiwaishengnei(String tpshiwaishengnei)
{
this.tpshiwaishengnei = tpshiwaishengnei;
}
public String getTpshiwaishengnei()
{
return tpshiwaishengnei;
}
public void setYqshengwai(String yqshengwai)
{
this.yqshengwai = yqshengwai;
}
public String getYqshengwai()
{
return yqshengwai;
}
public void setYqshiwaishengnei(String yqshiwaishengnei)
{
this.yqshiwaishengnei = yqshiwaishengnei;
}
public String getYqshiwaishengnei()
{
return yqshiwaishengnei;
}
public void setGuowaiwug(String guowaiwug)
{
this.guowaiwug = guowaiwug;
}
public String getGuowaiwug()
{
return guowaiwug;
}
public void setXianwaishinwug(String xianwaishinwug)
{
this.xianwaishinwug = xianwaishinwug;
}
public String getXianwaishinwug()
{
return xianwaishinwug;
}
public void setXiangzhennwug(String xiangzhennwug)
{
this.xiangzhennwug = xiangzhennwug;
}
public String getXiangzhennwug()
{
return xiangzhennwug;
}
public void setXiangzhenwaixianneiwug(String xiangzhenwaixianneiwug)
{
this.xiangzhenwaixianneiwug = xiangzhenwaixianneiwug;
}
public String getXiangzhenwaixianneiwug()
{
return xiangzhenwaixianneiwug;
}
public void setTpguowaiwug(String tpguowaiwug)
{
this.tpguowaiwug = tpguowaiwug;
}
public String getTpguowaiwug()
{
return tpguowaiwug;
}
public void setTpxianwaishinwug(String tpxianwaishinwug)
{
this.tpxianwaishinwug = tpxianwaishinwug;
}
public String getTpxianwaishinwug()
{
return tpxianwaishinwug;
}
public void setTpxiangzhennwug(String tpxiangzhennwug)
{
this.tpxiangzhennwug = tpxiangzhennwug;
}
public String getTpxiangzhennwug()
{
return tpxiangzhennwug;
}
public void setTpxiangzhenwaixianneiwug(String tpxiangzhenwaixianneiwug)
{
this.tpxiangzhenwaixianneiwug = tpxiangzhenwaixianneiwug;
}
public String getTpxiangzhenwaixianneiwug()
{
return tpxiangzhenwaixianneiwug;
}
public void setYqguowaiwug(String yqguowaiwug)
{
this.yqguowaiwug = yqguowaiwug;
}
public String getYqguowaiwug()
{
return yqguowaiwug;
}
public void setYqxianwaishinwug(String yqxianwaishinwug)
{
this.yqxianwaishinwug = yqxianwaishinwug;
}
public String getYqxianwaishinwug()
{
return yqxianwaishinwug;
}
public void setYqxiangzhennwug(String yqxiangzhennwug)
{
this.yqxiangzhennwug = yqxiangzhennwug;
}
public String getYqxiangzhennwug()
{
return yqxiangzhennwug;
}
public void setYqxiangzhenwaixianneiwug(String yqxiangzhenwaixianneiwug)
{
this.yqxiangzhenwaixianneiwug = yqxiangzhenwaixianneiwug;
}
public String getYqxiangzhenwaixianneiwug()
{
return yqxiangzhenwaixianneiwug;
}
}
3、然后定义一个接口接收的DTO类
package com.inspur.grp.hrss.labour.project.labourReport.dto;
import com.inspur.grp.hrss.labour.project.labourReport.LabourDayUpdatetime;
import java.util.List;
public class ExportDataRequestDto {
private List<LabourDayUpdatetime> labourDayUpdatetimeList;
private String addressName; // 这个是前端传递的过了的标题名称
// Getters and Setters
public List<LabourDayUpdatetime> getLabourDayUpdatetimeList() {
return labourDayUpdatetimeList;
}
public void setLabourDayUpdatetimeList(List<LabourDayUpdatetime> labourDayUpdatetimeList) {
this.labourDayUpdatetimeList = labourDayUpdatetimeList;
}
public String getAddressName() {
return addressName;
}
public void setAddressName(String addressName) {
this.addressName = addressName;
}
}
4、封装标题工具类
package com.inspur.grp.hrss.labour.util.IDCardUtils;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.write.handler.CellWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.springframework.util.PropertyPlaceholderHelper;
import java.util.List;
import java.util.Properties;
public class ExcelTitleHandler implements CellWriteHandler {
/**
错误信息处理时正则表达式的格式
*/
private final String EXCEL_ERROR_REG = "^(.*)(\\(错误:)(.*)(\\))$";
private String bigHead;
PropertyPlaceholderHelper placeholderHelper = new PropertyPlaceholderHelper("${", "}");
public ExcelTitleHandler( String bigHead) {
this.bigHead = bigHead; //表头1
}
@Override
public void beforeCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Head head, Integer columnIndex, Integer relativeRowIndex, Boolean isHead) {
// 动态设置表头字段
if (!ObjectUtil.isEmpty(head)) {
List<String> headNameList = head.getHeadNameList();
if (CollectionUtil.isNotEmpty(headNameList)) {
Properties properties = new Properties();
properties.setProperty("bigHead", bigHead);
for (int i = 0 ; i < headNameList.size() ; i++){
// 循环遍历替换
headNameList.set(i, placeholderHelper.replacePlaceholders(headNameList.get(i), properties));
}
}
}
}
@Override
public void afterCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
}
@Override
public void afterCellDataConverted(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, CellData cellData, Cell cell, Head head, Integer integer, Boolean aBoolean) {
}
@Override
public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, List<CellData> list, Cell cell, Head head, Integer integer, Boolean aBoolean) {
}
}
5、开始接口请求
//每日更新统计导出
@PostMapping("/exportDataUpdate")
public void exportDataUpdate(@RequestBody ExportDataRequestDto exportDataRequestDto, HttpServletResponse response) {
labourDayUpdatetimeService.exportDataUpdate(exportDataRequestDto, response);
}
6、service层
void exportDataUpdate(ExportDataRequestDto exportDataRequestDto, HttpServletResponse response);
7、serviceImpl实现类
private void setIntValueFromDoubleString(LabourDayUpdatetime recode, String value, Consumer<String> setter) {
if (value != null && !value.isEmpty()) {
try {
int intValue = (int) Double.parseDouble(value);
setter.accept(String.valueOf(intValue));
} catch (NumberFormatException e) {
setter.accept("0");
}
} else {
setter.accept("0");
}
}
private void setFormattedPercentage(LabourDayUpdatetime recode, String value, Consumer<String> setter) {
if (value != null && !value.isEmpty()) {
try {
double doubleValue = Double.parseDouble(value);
String formattedValue = String.format("%.2f%%", doubleValue * 100);
setter.accept(formattedValue);
} catch (NumberFormatException e) {
setter.accept("0.00%");
}
} else {
setter.accept("0.00%");
}
}
@Override
public void exportDataUpdate(ExportDataRequestDto exportDataRequestDto, HttpServletResponse response) {
String bigHead=exportDataRequestDto.getAddressName()+"劳动力更新报表";
List<LabourDayUpdatetime> labourDayUpdatetimeList = exportDataRequestDto.getLabourDayUpdatetimeList();
labourDayUpdatetimeList.stream().forEach(recode->{
setIntValueFromDoubleString(recode, recode.getNcldlrs(), recode::setNcldlrs);
setIntValueFromDoubleString(recode, recode.getGxrsa(), recode::setGxrsa);
setFormattedPercentage(recode, recode.getGxla(), recode::setGxla);
setIntValueFromDoubleString(recode, recode.getJyrsa(), recode::setJyrsa);
setIntValueFromDoubleString(recode, recode.getGuowaiwug(), recode::setGuowaiwug);
setIntValueFromDoubleString(recode, recode.getShengwai(), recode::setShengwai);
setIntValueFromDoubleString(recode, recode.getShiwaishengnei(), recode::setShiwaishengnei);
setIntValueFromDoubleString(recode, recode.getXianwaishinwug(), recode::setXianwaishinwug);
setIntValueFromDoubleString(recode, recode.getXiangzhennwug(), recode::setXiangzhenwaixianneiwug);
setIntValueFromDoubleString(recode, recode.getXiangzhennwug(), recode::setXiangzhennwug);
setFormattedPercentage(recode, recode.getJyla(), recode::setJyla);
setIntValueFromDoubleString(recode, recode.getTpldlrs(), recode::setTpldlrs);
setIntValueFromDoubleString(recode, recode.getGxrsb(), recode::setGxrsb);
setFormattedPercentage(recode, recode.getGxlb(), recode::setGxlb);
setIntValueFromDoubleString(recode, recode.getJyrsb(), recode::setJyrsb);
setIntValueFromDoubleString(recode, recode.getTpguowaiwug(), recode::setTpguowaiwug);
setIntValueFromDoubleString(recode, recode.getTpshengwai(), recode::setTpshengwai);
setIntValueFromDoubleString(recode, recode.getTpshiwaishengnei(), recode::setTpshiwaishengnei);
setIntValueFromDoubleString(recode, recode.getTpxianwaishinwug(), recode::setTpxianwaishinwug);
setIntValueFromDoubleString(recode, recode.getTpxiangzhenwaixianneiwug(), recode::setTpxiangzhenwaixianneiwug);
setIntValueFromDoubleString(recode, recode.getTpxiangzhennwug(), recode::setTpxiangzhennwug);
setFormattedPercentage(recode, recode.getJylb(), recode::setJylb);
setIntValueFromDoubleString(recode, recode.getYqldlrs(), recode::setYqldlrs);
setIntValueFromDoubleString(recode, recode.getGxrsc(), recode::setGxrsc);
setFormattedPercentage(recode, recode.getGxlc(), recode::setGxlc);
setIntValueFromDoubleString(recode, recode.getJyrsc(), recode::setJyrsc);
setIntValueFromDoubleString(recode, recode.getYqguowaiwug(), recode::setYqguowaiwug);
setIntValueFromDoubleString(recode, recode.getYqshengwai(), recode::setYqshengwai);
setIntValueFromDoubleString(recode, recode.getYqshiwaishengnei(), recode::setYqshiwaishengnei);
setIntValueFromDoubleString(recode, recode.getYqxianwaishinwug(), recode::setYqxianwaishinwug);
setIntValueFromDoubleString(recode, recode.getYqxiangzhenwaixianneiwug(), recode::setYqxiangzhenwaixianneiwug);
setIntValueFromDoubleString(recode, recode.getYqxiangzhennwug(), recode::setYqxiangzhennwug);
setFormattedPercentage(recode, recode.getJylc(), recode::setJylc);
setIntValueFromDoubleString(recode, recode.getXtzwgxdldlrs(), recode::setXtzwgxdldlrs);
});
try {
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
response.setHeader("Content-disposition", "attachment;filename=" + "数据" + ".xlsx");
WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
contentWriteCellStyle.setBorderLeft(BorderStyle.THIN);
contentWriteCellStyle.setBorderTop(BorderStyle.THIN);
contentWriteCellStyle.setBorderRight(BorderStyle.THIN);
contentWriteCellStyle.setBorderBottom(BorderStyle.THIN);
contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
WriteCellStyle headWriteCellStyle = new WriteCellStyle();
headWriteCellStyle.setBorderLeft(BorderStyle.THIN);
headWriteCellStyle.setBorderTop(BorderStyle.THIN);
headWriteCellStyle.setBorderRight(BorderStyle.THIN);
headWriteCellStyle.setBorderBottom(BorderStyle.THIN);
//设置头部标题居中
headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
// 这个策略是 头是头的样式 内容是内容的样式 其他的策略可以自己实现
HorizontalCellStyleStrategy horizontalCellStyleStrategy =
new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
EasyExcel.write(response.getOutputStream(), LabourDayUpdatetime.class)
.sheet("数据")
.registerWriteHandler(new ExcelTitleHandler(bigHead))
.registerWriteHandler(new SimpleColumnWidthStyleStrategy(20)).registerWriteHandler(horizontalCellStyleStrategy)
.doWrite(labourDayUpdatetimeList);
}catch (Exception e){
throw new RuntimeException(e);
}
}
7、前端vue的接口
具体代码就不列出了,这个 daochu 就是一个按钮button
daochu(){
if(this.tableData.length==0){
this.$message({ message: '请确保有数据进行导出', type: 'warning' });return;
}else{
const loading = this.$loading({lock: true,text: '导出中',spinner: 'el-icon-loading',background: ' rgba(255, 255, 255, 0.6)'});
var data = {
labourDayUpdatetimeList: this.tableData, // 这个tableData 这个是你要导出的数据
addressName: this.addressName // 这个是你要给你的excel设置的大标题
}
//发送接口请求
labourReportApi.exportDataUpdate(data).then(res => {
const url = window.URL.createObjectURL(new Blob([res]));
const link = document.createElement('a');
link.href = url;
link.setAttribute('download', '每日更新情况调度统计.xlsx');
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
window.URL.revokeObjectURL(url);
this.$message({message: '导出数据成功', type: 'success' });
setTimeout(() => {loading.close();}, 1000);
})
}
},
8、具体vue请求
这个是一个js,你可以赋值到你自己的接口js里面去
import request from "@/util/request";
var prefixD = customConfig.VUE_APP_CLIENT_ID + "/labourReport/updatetimeDayTj";
export default {
//每日更新导出
exportDataUpdate: async function (params) {
return request
({
url: prefixD + "/exportDataUpdate",
method: "post",
data: params,
responseType: "arraybuffer",
headers: {
'Content-Type': 'application/json'
}
})
},
}
9、导出已经完成了,展示效果
因为数据量很多,就展示部分