1、封装方法
package com.skybird.iot.base.utils;
import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.skybird.iot.base.utils.bean.Custemhandler;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.VerticalAlignment;
public class EasyExcelUtil {
public static void download(
HttpServletRequest request, HttpServletResponse response, Class t, List list, String name)
throws IOException, IllegalAccessException, InstantiationException {
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
response.setHeader("Content-disposition", "attachment;fileName=name.xlsx");
try (OutputStream outStream = response.getOutputStream()) {
EasyExcel.write(outStream, t)
.excelType(ExcelTypeEnum.XLSX)
.registerWriteHandler(new Custemhandler())
.registerWriteHandler(getStyleStrategy())
public static void trendsDownload(
HttpServletRequest request,
HttpServletResponse response,
try (OutputStream outStream = response.getOutputStream()) {
EasyExcel.write(outStream)
.registerWriteHandler(new Custemhandler())
.registerWriteHandler(getStyleStrategy())
public static String downloadCommFileName(String fileOut, HttpServletRequest request)
String userAgent = request.getHeader("user-agent").toLowerCase();
if (!StrUtil.contains(userAgent, "chrome")
&& (userAgent.contains("msie") || userAgent.contains("like gecko"))) {
fileOut = URLEncoder.encode(fileOut, "UTF-8");
fileOut = new String(fileOut.getBytes("utf-8"), "iso-8859-1");
public static HorizontalCellStyleStrategy getStyleStrategy() {
WriteCellStyle headWriteCellStyle = new WriteCellStyle();
headWriteCellStyle.setFillForegroundColor(IndexedColors.AUTOMATIC.getIndex());
WriteFont headWriteFont = new WriteFont();
headWriteFont.setFontHeightInPoints((short) 12);
headWriteFont.setFontName("宋体");
headWriteFont.setBold(false);
headWriteCellStyle.setWriteFont(headWriteFont);
headWriteCellStyle.setWrapped(false);
headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
headWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
contentWriteCellStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());
WriteFont contentWriteFont = new WriteFont();
contentWriteFont.setFontHeightInPoints((short) 12);
contentWriteFont.setFontName("Calibri");
contentWriteCellStyle.setWriteFont(contentWriteFont);
return new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
2、Java请求接口
@RequestMapping("/excel")
HttpServletRequest request,
HttpServletResponse response,
@RequestParam(value = "dateRange", required = false) Integer dateRange)
List list = getList(dateRange);
List> head = ListUtils.newArrayList();
List dtoList = DocuLib.getList(list.get(0), "causeMalfunctionDetails");
for (Document item : dtoList) {
List head1 = ListUtils.newArrayList();
head1.add(DocuLib.getStr(item, "causeMalfunctionName"));
List> dataList = ListUtils.newArrayList();
for (Document item : list) {
List
data.add(DocuLib.getStr(item, "yearMonth"));
data.add(DocuLib.getStr(item, "totalMonthly"));
data.add(DocuLib.getStr(item, "auditMonthly"));
data.add(DocuLib.getStr(item, "completedMonthly"));
List rowList = DocuLib.getList(item, "causeMalfunctionDetails");
for (Document dto : rowList) {
data.add(DocuLib.getStr(dto, "count"));
EasyExcelUtil.trendsDownload(request, response, head, dataList, "销售退货统计");
private void getHead(List> head)
{
List head1 = ListUtils.newArrayList();
List head2 = ListUtils.newArrayList();
List head3 = ListUtils.newArrayList();
List head4 = ListUtils.newArrayList();
3、html关键代码
<div class='card-body' style='height: 100%;'>
<div style="display: flex;flex-wrap: wrap; align-items: center;">
style="width:100px;margin-bottom: 10px; display: block;"
ng-model="ctrl.filter.dateRange"
<button class="btn btn-default btn-sm"
style="margin-left: 8px; margin-bottom: 10px; display: block;" type="button"
ng-click="ctrl.loadData()">
<i class="bi bi-search">i>
<button class='btn btn-outline-primary btn-sm' ng-click='ctrl.export()'
style="margin-left: 5px;margin-bottom: 10px;"
ng-if="''| SecurityFilter: 'saleReturnOrderStatistics':'export'"
<div style="overflow: scroll;overflow-x: auto; width: 100%;height: calc(100% - 80px);"
<table class="table table-bordered" style="width: 1640px;table-layout: fixed;">
<thead style="position: sticky;top: -1px;z-index: 1;">
<th width="150px;">退货单数th>
<th width="150px;">审核通过th>
<th width="150px;">完成单数th>
<th ng-repeat="dailyDetail in entity[0].causeMalfunctionDetails"
ng-style="{width: dailyDetail.causeMalfunctionName.length>=5?dailyDetail.causeMalfunctionName.length*17:100}">
{{dailyDetail.causeMalfunctionName}}
<tr ng-repeat="row in entity">
<td>{{row.totalMonthly}}td>
<td>{{row.auditMonthly}}td>
<td>{{row.completedMonthly}}td>
<td ng-repeat="dailyDetail in row.causeMalfunctionDetails">{{dailyDetail.count}}td>
<div style="overflow: scroll; width: 100%;height: calc(100% - 80px);" ng-if="check === true">
<table class="table table-bordered" style="width: auto;table-layout: fixed;">
<thead style="position: sticky;top: -1px;z-index: 1;">
<th width="150px;">退货单数th>
<th width="150px;">审核通过th>
<th width="150px;">完成单数th>
<th ng-repeat="dailyDetail in entity[0].causeMalfunctionDetails">
{{dailyDetail.causeMalfunctionName}}
<tr ng-repeat="row in entity">
<td>{{row.totalMonthly}}td>
<td>{{row.auditMonthly}}td>
<td>{{row.completedMonthly}}td>
<td ng-repeat="dailyDetail in row.causeMalfunctionDetails">{{dailyDetail.count}}td>
4、js关键代码
http.getFile('saleReturnOrderStatistics/excel', {
dateRange: ctrl.filter.dateRange
}).then(function (response) {
const url = window.URL.createObjectURL(new Blob([response.data]));
const link = document.createElement('a');
link.setAttribute('download', '销售退货统计.xlsx');
document.body.appendChild(link);
5、效果
