官方文档地址:easypoi官网,官方仅供参考,部分描述有问题
excel模板预览
准备工作
事先将整理好的excel模板存在项目中,如图
 
excel模板预览代码
	@GetMapping("excel")
    @ApiOperation("excel预览")
    @NoLog
    public void excel07(HttpServletResponse response) throws IOException {
        //读取文件 templates/学生信息表.xlsx是相对路径
        InputStream inputStream = POICacheManager.getFile("templates/学生信息表.xlsx");
        //创建工作簿
        Workbook workbook = WorkbookFactory.create(inputStream);
        //设置为true防止中文乱码 sheetNum默认从0开始
        ExcelToHtmlParams params=new ExcelToHtmlParams(workbook,true,0,"");
        //解析成html
        String excelToHtml = ExcelXorHtmlUtil.excelToHtml(params);
        response.getOutputStream().write(excelToHtml.getBytes());
    }
excel模板下载
准备工作
事先将整理好的excel模板存在项目中,如图
 
excel模板下载代码
	@GetMapping("downTemplate")
    @ApiOperation("下载模板")
    @NoLog
    public void downTemplate(HttpServletResponse response) throws IOException {
        //指定下载模板的哪个sheet页 templates/学生信息表.xlsx是相对路径
        TemplateExportParams template=new TemplateExportParams("templates/学生信息表.xlsx","模板2");
        //保证模板里面没有域占位行
        HashMap hashMap = new HashMap();
        hashMap.put("mapList",Lists.newArrayList());
        Workbook workbook = ExcelExportUtil.exportExcel(template,hashMap);
        ExcelUtils.exportExcel(response,workbook,"学生信息模板表.xlsx");
    }
excel模板导出简单数据代码
可以用模板指令设置导出内容的
准备工作

 注:模板指令如下:
 空格分割
 三目运算 {{test ? obj:obj2}}
 n: 表示 这个cell是数值类型 {{n:}}
 le: 代表长度{{le:()}} 在if/else 运用{{le:() > 8 ? obj1 : obj2}}
 fd: 格式化时间 {{fd:(obj;yyyy-MM-dd)}}
 fn: 格式化数字 {{fn:(obj;###.00)}}
 fe: 遍历数据,创建row
 !fe: 遍历数据不创建row
 $fe: 下移插入,把当前行,下面的行全部下移.size()行,然后插入
 #fe: 横向遍历
 v_fe: 横向遍历值
 !if: 删除当前列 {{!if:(test)}}
 单引号表示常量值 ‘’ 比如’1’ 那么输出的就是 1
 &NULL& 空格
 &INDEX& 表示循环中的序号,自动添加
 ]] 换行符 多行遍历导出
 sum: 统计数据
 cal: 基础的±X% 计算
 dict: 字典
 i18n: 国际化
excel模板导出简单数据代码
	@GetMapping("exportDataSimple")
    @ApiOperation("模板导出数据-简单")
    @NoLog
    public void exportDataSimple(HttpServletResponse response) throws IOException {
        TemplateExportParams template=new TemplateExportParams("templates/学生信息表.xlsx");
        String [] sexArr=new String[]{"男","女"};
        String [] subArr=new String[]{"语文","数学","英语"};
        List<Map<String,Object>> list= Lists.newArrayList();
        Map<String,Object> contentMap;
        for (int i = 0; i < NUM; i++) {
            contentMap=Maps.newHashMap();
            contentMap.put("name",UUID.randomUUID().toString());
            contentMap.put("sex",sexArr[i%2]);
            contentMap.put("age", new Random().nextInt(90)+10);
            contentMap.put("subject",subArr[i%3]);
            contentMap.put("score", ThreadLocalRandom.current().nextInt(40)+60);
            list.add(contentMap);
        }
        Map<String,Object> map= Maps.newHashMap();
        map.put("mapList", list);
        map.put("class", "一年级");
        map.put("date", new Date());
        Workbook workbook = ExcelExportUtil.exportExcel(template, map);
        ExcelUtils.exportExcel(response,workbook,"学生数据.xlsx");
    }
一些模板导出知识参考
注意事项以及常见错误参考
 springboot集成easypoi并使用其模板导出功能和遇到的坑
 详细easypoi导出参考
 EasyPoi基本用法
excel模板导出复杂数据
用不了模板指令设置导出内容的,样式中性别那一列有下拉框,通过模板指令设置不了,所以考虑手动插入数据
excel模板导出复杂数据代码
	@GetMapping("exportDataComplex")
    @ApiOperation("模板导出数据-复杂")
    @NoLog
    public void exportDataComplex(HttpServletResponse response) throws IOException {
        //读取模板
        TemplateExportParams template=new TemplateExportParams("templates/学生信息表.xlsx",1);
        //模拟数据
        String [] sexArr=new String[]{"男","女"};
        String [] subArr=new String[]{"语文","数学","英语"};
        List<StudentTemplate> list=Lists.newArrayList();
        StudentTemplate student;
        for (int i = 0; i < NUM; i++) {
            student=new StudentTemplate();
            student.setId(i+1);
            student.setName(UUID.randomUUID().toString());
            student.setAge(new Random().nextInt(90)+10);
            student.setSex(sexArr[i%2]);
            student.setSubject(subArr[i%3]);
            student.setScore(ThreadLocalRandom.current().nextInt(40)+60);
            list.add(student);
        }
        Map<String,Object> map= Maps.newHashMap();
        map.put("class", "一年级");
        map.put("date", new Date());
        //导出工作簿
        Workbook workbook = ExcelExportUtil.exportExcel(template, map);
        //获取第一个sheet页
        Sheet sheet = workbook.getSheetAt(0);
        //设置列宽自适应
        for (int i = 0; i < 6 ; i++) {
            sheet.autoSizeColumn(i);
            sheet.setColumnWidth(i,sheet.getColumnWidth(i)*17/10);
        }
        //设置指定列宽
        sheet.setColumnWidth(1,21*256);
        //数据首行
        int num = sheet.getLastRowNum();
        //行
        Row row;
        //列
        Cell cell;
        StudentTemplate studentTemplate;
        //单元格样式
        CellStyle cellStyle = ExcelUtils.setCellStyle(workbook);
        //写入数据
        for (int i = num; i < NUM+num; i++) {
            row= sheet.createRow(i);
            studentTemplate = list.get(i - num);
            for (int j = 0; j < 6; j++) {
                cell= row.createCell(j);
                cell.setCellStyle(cellStyle);
                if (j==0) {
                    cell.setCellValue(studentTemplate.getId());
                }else if(j==1){
                    cell.setCellValue(studentTemplate.getName());
                }else if(j==2){
                    cell.setCellValue(studentTemplate.getAge());
                }else if(j==3){
                    cell.setCellValue(studentTemplate.getSex());
                }else if(j==4){
                    cell.setCellValue(studentTemplate.getSubject());
                }else if(j==5){
                    cell.setCellValue(studentTemplate.getScore());
                }
            }
        }
        ExcelUtils.exportExcel(response,workbook,"学生数据.xlsx");
    }
附录
ExcelUtils类
import org.apache.poi.ss.usermodel.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
/**
 * excel工具类
 * @author leishen
 */
public class ExcelUtils {
    /**
     * 下载文件到客户端
     * @param response
     * @param workbook
     * @param fileName 文件名
     * @throws IOException
     */
    public static void exportExcel(HttpServletResponse response, Workbook workbook, String fileName) throws IOException {
        response.setCharacterEncoding("UTF-8");
        // 设置响应输出的头类型
        response.setHeader("content-Type", "application/vnd.ms-excel");
        // 下载文件的默认名称
        response.setHeader("Content-Disposition", "attachment;filename="+ URLEncoder.encode(fileName, "UTF-8"));
        OutputStream out = response.getOutputStream();
        workbook.write(out);
        out.flush();
        out.close();
    }
    /**
     * 设置单元格样式
     * @param workbook
     */
     public static CellStyle setCellStyle(Workbook workbook){
        CellStyle cellStyle = workbook.createCellStyle();
        //水平居中
        cellStyle.setAlignment(HorizontalAlignment.CENTER);
        //垂直居中
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        //上边框
        cellStyle.setBorderTop(BorderStyle.THIN);
        //下边框
        cellStyle.setBorderBottom(BorderStyle.THIN);
        //左边框
        cellStyle.setBorderLeft(BorderStyle.THIN);
        //右边框
        cellStyle.setBorderRight(BorderStyle.THIN);
        //设置字体
        Font font = workbook.createFont();
        font.setFontName("宋体");
        //设置样式
        cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        cellStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());
        cellStyle.setFont(font);
        //设置自动换行
        cellStyle.setWrapText(true);
        return cellStyle;
    }
}



















