先看效果:
1、引入maven依赖
<!--EasyExcel --> <dependency> <groupId>com.alibaba</groupId> <artifactId>easyexcel</artifactId> <version>3.2.1</version> </dependency>
2、实体类
package com.huawei.it.domain;
import com.alibaba.excel.annotation.ExcelIgnore;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.ColumnWidth;
/**
* @description: 嫁功率考勤实体类
* @author: yeyong
* @create: 2024-01-16
*/
public class Jglkq{
@ExcelProperty({"考勤状态"})
private String kqzt; //考勤状态
@ExcelProperty({"日期"})
@ColumnWidth(20)
private String rq; //日期
@ExcelProperty({"工号"})
private String gh; //工号
@ExcelProperty({"姓名"})
private String xm; //姓名
@ExcelProperty({"应出勤"})
private String ycq; //应出勤
@ExcelProperty({"实出勤"})
private String scq; //实出勤
@ExcelProperty({"班制名称"})
private String bzmc; //班制名称
@ExcelProperty({"班一","上"})
private String sw; //上
@ExcelProperty({"班一","下"})
private String xw; //下
@ExcelProperty({"平时加班"})
private String psjb; //平时加班
@ExcelProperty({"假日加班"})
private String jrjb; //假日加班
@ExcelProperty({"周六排班8小时"})
private String str1; //周六排班8小时
@ExcelProperty({"周六超8小时"})
private String str2; //周六超8小时
//忽略这个字段
@ExcelIgnore
private String IsNight; //是否夜班 0:否 1:是
public String getKqzt() {
return kqzt;
}
public void setKqzt(String kqzt) {
this.kqzt = "正常";
}
public String getRq() {
return rq;
}
public void setRq(String rq) {
this.rq = rq;
}
public String getGh() {
return gh;
}
public void setGh(String gh) {
this.gh = gh;
}
public String getXm() {
return xm;
}
public void setXm(String xm) {
this.xm = xm;
}
public String getYcq() {
return ycq;
}
public void setYcq(String ycq) {
this.ycq = ycq;
}
public String getScq() {
return scq;
}
public void setScq(String scq) {
this.scq = scq;
}
public String getBzmc() {
return bzmc;
}
public void setBzmc(String bzmc) {
this.bzmc = bzmc;
}
public String getSw() {
return sw;
}
public void setSw(String sw) {
this.sw = sw;
}
public String getXw() {
return xw;
}
public void setXw(String xw) {
this.xw = xw;
}
public String getPsjb() {
return psjb;
}
public void setPsjb(String psjb) {
this.psjb = psjb;
}
public String getJrjb() {
return jrjb;
}
public void setJrjb(String jrjb) {
this.jrjb = jrjb;
}
public String getStr1() {
return str1;
}
public void setStr1(String str1) {
this.str1 = str1;
}
public String getStr2() {
return str2;
}
public void setStr2(String str2) {
this.str2 = str2;
}
public String getIsNight() {
return IsNight;
}
public void setIsNight(String isNight) {
IsNight = isNight;
}
}
3、web导出 controller文章来源:https://www.toymoban.com/news/detail-801351.html
package com.huawei.web.controller.it;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.merge.LoopMergeStrategy;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.alibaba.excel.write.style.column.SimpleColumnWidthStyleStrategy;
import com.huawei.common.core.controller.BaseController;
import com.huawei.common.core.page.TableDataInfo;
import com.huawei.common.utils.poi.MonthSheetWriteHandler;
import com.huawei.it.domain.DemoData;
import com.huawei.it.domain.Jglkq;
import com.huawei.it.service.JglkqService;
import org.apache.poi.ss.usermodel.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* @description:
* @author: yeyong
* @create: 2024-01-16
*/
@Controller
@RequestMapping("/hr/jglkq")
public class JglkqController extends BaseController {
private String prefix = "it/hr";
@Autowired
private JglkqService jglkqService;
//使用easyExcel导出
@GetMapping("/export")
public void easyExcelExport(HttpServletResponse response,Jglkq jglkq){
//备注:使用easyExcel导出多级表头
try {
//准备数据
List<Jglkq> kqList =new ArrayList<Jglkq>();
List<Jglkq> list = jglkqService.selectJglkqList(jglkq);
for (Jglkq kq1 : list) {
Jglkq kq = new Jglkq();
kq.setKqzt(kq1.getKqzt()); //考勤状态
kq.setRq(kq1.getRq()); //日期
kq.setGh(kq1.getGh()); //工号
kq.setXm(kq1.getXm()); //姓名
kq.setYcq("1"); //应出勤
kq.setScq("1"); //实出勤
kq.setBzmc(kq1.getBzmc()); //班制名称
kq.setSw(kq1.getSw()); //上班
kq.setXw(kq1.getXw()); //下班
kq.setPsjb(kq1.getPsjb()); //平时加班
kq.setJrjb(kq1.getJrjb()); //假日加班
kq.setStr1(kq1.getStr1()); //
kq.setStr2(kq1.getStr2()); //
kqList.add(kq);
}
//response.setContentType("application/vnd.ms-excel");
response.setContentType("application/vnd.ms-excel;charset=utf-8");
response.setCharacterEncoding("utf-8");
//StringBuffer bigTitle = new StringBuffer("考勤结果明细查询");
//String fileName = URLEncoder.encode(bigTitle.toString(), "UTF-8")+ ".xlsx";
String fileName = System.currentTimeMillis()+".xlsx";
response.setHeader("Content-disposition", "attachment;filename=" + fileName);
// 头的策略
WriteCellStyle headWriteCellStyle = new WriteCellStyle();
headWriteCellStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());
// 默认设置为水平居中
WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
contentWriteCellStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());
contentWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
contentWriteCellStyle.setBorderBottom(BorderStyle.THIN);
contentWriteCellStyle.setBorderLeft(BorderStyle.THIN);
contentWriteCellStyle.setBorderRight(BorderStyle.THIN);
contentWriteCellStyle.setBorderTop(BorderStyle.THIN);
// 这里需要设置不关闭流
EasyExcel.write(response.getOutputStream(), Jglkq.class)
//.autoCloseStream(Boolean.FALSE).registerWriteHandler(horizontalCellStyleStrategy)
.sheet("考勤结果明细查询")
.relativeHeadRowIndex(1)
//设置拦截器或自定义样式
.registerWriteHandler(new MonthSheetWriteHandler())
.registerWriteHandler(new HorizontalCellStyleStrategy(headWriteCellStyle,contentWriteCellStyle))
.doWrite(kqList);
} catch (IOException e) {
e.printStackTrace();
}
}
}
4、在拦截器中对表头的样式进行自定义的覆盖文章来源地址https://www.toymoban.com/news/detail-801351.html
package com.huawei.common.utils.poi;
import com.alibaba.excel.write.handler.SheetWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
/**
* @description: 表头设置拦截器
* @author: yeyong
* @create: 2024-01-17
*/
public class MonthSheetWriteHandler implements SheetWriteHandler {
@Override
public void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {
Workbook workbook = writeWorkbookHolder.getWorkbook();
Sheet sheet = workbook.getSheetAt(0);
Row row1 = sheet.createRow(0);
row1.setHeight((short) 800);
Cell cell = row1.createCell(0);
//设置标题
cell.setCellValue("考勤结果明细查询" );
CellStyle cellStyle = workbook.createCellStyle();
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
cellStyle.setAlignment(HorizontalAlignment.CENTER);
Font font = workbook.createFont();
font.setBold(true);
font.setFontHeight((short) 400);
font.setFontName("宋体");
cellStyle.setFont(font);
cell.setCellStyle(cellStyle);
sheet.addMergedRegionUnsafe(new CellRangeAddress(0, 0, 0, 15));
}
}
到了这里,关于使用easyexcel 导出多级表头demo的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!