easyexcel导出自定义表头

  
            com.alibaba
            easyexcel
            2.2.3
        

package com.sunnysoft.transfer.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.util.DateUtils;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.sunnysoft.transfer.dto.QueryHealthCheckMorningCheckRec;
import com.sunnysoft.transfer.dto.QueryHealthCheckWatchRec;
import com.sunnysoft.transfer.entity.HealthCheckMorningCheckRec;
import com.sunnysoft.transfer.entity.HealthCheckWatchRec;
import com.sunnysoft.transfer.excel.HealthWatchExcelModel;
import com.sunnysoft.transfer.excel.MorningCheckExcelModel;
import com.sunnysoft.transfer.service.IHealthCheckMorningCheckRecService;
import com.sunnysoft.transfer.service.IHealthCheckWatchRecService;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Author:  dongzp
 * Time:    2020/5/25 15:47
 * Project: fastdfs-transfer-tencent-cos
 * Email:   [email protected]
 * Remarks:
 **/
@RestController
@RequestMapping(value = "export")
public class ExportController {


    private static Logger logger = LoggerFactory.getLogger(ExportController.class);

    @Autowired
    private IHealthCheckMorningCheckRecService healthCheckMorningCheckRecService;
    @Autowired
    private IHealthCheckWatchRecService healthCheckWatchRecService;


    /**
     * 获取晨午检信息
     * @return
     */
    private List getMorningCheckData(List checkList){
        List data = new ArrayList<>();
        for(HealthCheckMorningCheckRec rec : checkList){
            MorningCheckExcelModel model = new MorningCheckExcelModel();
            model.setCheckTime(rec.getMorningCheckTime());
            model.setClassName(rec.getClassName());
            model.setRemark(rec.getParComp());
            model.setSymptom(rec.getCheckException());
            model.setTemp(rec.getMorningCheckTemp() + "℃");
            model.setName(rec.getChildName());
            data.add(model);
        }
        return data;
    }

    /**
     * 获取健康观察信息
     * @return
     */
    private List getHelathWatchData(List watchRecList){
        List data = new ArrayList<>();
        for(HealthCheckWatchRec rec : watchRecList){
            HealthWatchExcelModel model = new HealthWatchExcelModel();
            model.setCheckTime(rec.getWatchTime());
            model.setClassName(rec.getClassName());
            model.setSymptom(rec.getHealthExceptionInfo());
            model.setOther(rec.getOtherInfo());
            model.setTemp(rec.getWatchTemp() + "℃");
            model.setName(rec.getChildName());
            data.add(model);
        }

        return data;
    }

    /**
     * 晨午检的头
     * @param bigTitle
     * @return
     */
    private  List> getMorningCheckHead(String bigTitle){
        List> head = new ArrayList>();
        List head0 = new ArrayList<>();
        head0.add(bigTitle);
        head0.add("班级");
        List head1 = new ArrayList<>();
        head1.add(bigTitle);
        head1.add("姓名");
        List head2 = new ArrayList<>();
        head2.add(bigTitle);
        head2.add("晨午检时间");
        List head3 = new ArrayList<>();
        head3.add(bigTitle);
        head3.add("体温");
        List head4 = new ArrayList<>();
        head4.add(bigTitle);
        head4.add("晨午检异常症状");
        List head5 = new ArrayList<>();
        head5.add(bigTitle);
        head5.add("备注");
        head.add(head0);
        head.add(head1);
        head.add(head2);
        head.add(head3);
        head.add(head4);
        head.add(head5);

        return head;
    }

    /**
     * 健康观察的头
     * @param bigTitle
     * @return
     */
    private  List> getHealthWatchHead(String bigTitle){
        List> head = new ArrayList>();
        List head0 = new ArrayList<>();
        head0.add(bigTitle);
        head0.add("班级");
        List head1 = new ArrayList<>();
        head1.add(bigTitle);
        head1.add("姓名");
        List head2 = new ArrayList<>();
        head2.add(bigTitle);
        head2.add("观察时间");
        List head3 = new ArrayList<>();
        head3.add(bigTitle);
        head3.add("体温");
        List head4 = new ArrayList<>();
        head4.add(bigTitle);
        head4.add("健康异常症状");
        List head5 = new ArrayList<>();
        head5.add(bigTitle);
        head5.add("其他情况");
        List head6 = new ArrayList<>();
        head6.add(bigTitle);
        head6.add("备注");
        head.add(head0);
        head.add(head1);
        head.add(head2);
        head.add(head3);
        head.add(head4);
        head.add(head5);
        head.add(head6);
        return head;
    }

    /**
     * 导出晨午检信息
     * @param response
     * @throws Exception
     */
    @RequestMapping(value = "exportMorningCheck")
    public void exportMorningCheck(HttpServletResponse response, QueryHealthCheckMorningCheckRec rec) throws Exception{

        if(StringUtils.isEmpty(rec.getKdgId()))
        {
            return;
        }
        StringBuffer bigTitle = new StringBuffer();
        if(StringUtils.isEmpty(rec.getMorningCheckTime()) || rec.getMorningCheckTime().length() == 0){
            rec.setMorningCheckTime(DateUtils.format(new Date(),"yyyy-MM-dd"));
            bigTitle.append(DateUtils.format(new Date(),"yyyy年MM月dd日"));
        }else{
            Date date = DateUtils.parseDate(rec.getMorningCheckTime(),"yyyy-MM-dd");
            bigTitle.append(DateUtils.format(date,"yyyy年MM月dd日"));
        }

        List morningCheckList = healthCheckMorningCheckRecService.getMorningCheckRecInfo(rec);

        if(morningCheckList != null && morningCheckList.size() > 0){
            bigTitle.append(morningCheckList.get(0).getKdgName());
        }

        bigTitle.append("晨午检详单");

        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode(bigTitle.toString(), "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");


        // 头的策略
        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);

        HorizontalCellStyleStrategy horizontalCellStyleStrategy = new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);

        EasyExcel.write(response.getOutputStream(), MorningCheckExcelModel.class).head(getMorningCheckHead(bigTitle.toString())).registerWriteHandler(horizontalCellStyleStrategy).sheet("晨午检").doWrite(getMorningCheckData(morningCheckList));
    }



    /**
     * 导出健康观察信息
     * @param response
     * @throws Exception
     */
    @RequestMapping(value = "exportHealthWatch")
    public void exportHealthWatch(HttpServletResponse response, QueryHealthCheckWatchRec rec) throws Exception{
        if(StringUtils.isEmpty(rec.getKdgId()))
        {
            return;
        }
        StringBuffer bigTitle = new StringBuffer();

        if(StringUtils.isEmpty(rec.getWatchTime()) || rec.getWatchTime().length() == 0){
            rec.setWatchTime(DateUtils.format(new Date(),"yyyy-MM-dd"));
            bigTitle.append(DateUtils.format(new Date(),"yyyy年MM月dd日"));
        }else{
            Date date = DateUtils.parseDate(rec.getWatchTime(),"yyyy-MM-dd");
            bigTitle.append(DateUtils.format(date,"yyyy年MM月dd日"));
        }

        List healthCheckWatchRecList = healthCheckWatchRecService.getWatchRecInfo(rec);

        if(healthCheckWatchRecList != null && healthCheckWatchRecList.size() > 0){
            bigTitle.append(healthCheckWatchRecList.get(0).getKdgName());
        }

        bigTitle.append("全日健康观察详单");

        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode(bigTitle.toString(), "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");


        // 头的策略
        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);

        HorizontalCellStyleStrategy horizontalCellStyleStrategy = new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);

        EasyExcel.write(response.getOutputStream(), HealthWatchExcelModel.class).head(getHealthWatchHead(bigTitle.toString())).registerWriteHandler(horizontalCellStyleStrategy).sheet("全日健康观察").doWrite(getHelathWatchData(healthCheckWatchRecList));
    }
}

image.png

你可能感兴趣的:(easyexcel导出自定义表头)