官方文档地址:easypoi官网,官方仅供参考,部分描述有问题

excel模板预览

准备工作

事先将整理好的excel模板存在项目中,如图

excel模板预览代码

 @GetMapping("excel")@ApiOperation("excel预览")@NoLogpublic 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,"");//解析成htmlString excelToHtml = ExcelXorHtmlUtil.excelToHtml(params);response.getOutputStream().write(excelToHtml.getBytes());}

excel模板下载

准备工作

事先将整理好的excel模板存在项目中,如图

excel模板下载代码

 @GetMapping("downTemplate")@ApiOperation("下载模板")@NoLogpublic 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("模板导出数据-简单")@NoLogpublic 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("模板导出数据-复杂")@NoLogpublic 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;}
}

EasyPoi的excel模板预览与下载、导出简单/复杂数据相关推荐

  1. excel打印预览在哪里_简单实用的excel打印技巧 建议收藏

    打印也是需要技巧滴,一些简单实用的excel打印技巧送给你! 1.每页都打印相同标题 在打印时,第一页有标题,第二页及以后没有标题,造成阅读不便,如何使每页都打印相同标题呢? [页面布局]→[打印标题 ...

  2. 基于SpringMVC+EasyPoi,采用Excel模板方式实现Excel在线预览和导出(2021版)

    一.背景:某工厂需要查看指定年度的设备维护计划,一般需要把全年72个周,每个周做哪些维护工作排班排出以及工时统计出来,在正式生成维护工单之前,需要先确认下.维护计划大概是某一周要执行哪些维护项,大概长 ...

  3. vue移动端实现excel在线预览

    上篇博客我提到了ios手机不能实现下载功能,但是可以实现预览,图片预览和pdf预览我已经在前篇博客做了讲解,但是,在工作中大家上传最多的应该是excel的文件,今天我就讲解一下excel移动端的预览实 ...

  4. Vue+SpringBoot实现Excel在线预览功能(PS:添加样式比较费劲)

    ** 问题还原: ** 在做项目时,用户需要上传Excel模板,里面有对应的各种数据.我们拿到这个Excel后,定时的根据其中的数据去查对应的实时数据并进行计算,然后将实时数据和计算后的数据保存到Ex ...

  5. aspose实现word,excel在线预览

    aspose实现word,excel在线预览 一,项目中引入aspose依赖 <dependency><groupId>com.aspose</groupId>&l ...

  6. 前端-Excel在线预览

    前端-Excel在线预览 最近项目中有一个 Excel 预览的需求,就调研了一下 xls/xlsx.word.ppt 文件在线预览功能的实现 . 实现 xls/xlsx.word.ppt 在线预览功能 ...

  7. Java 利用EasyPoi做Excel模板的导入导出操作

    Java 利用EasyPoi做Excel模板的导入导出操作 项目背景 加入pom依赖 项目Excel模板图 代码实现 首先是实体类定义 Excel 实现导入 Excel的导出 结束语 项目背景 作为一 ...

  8. java通过POI和jacob实现word文档的在线预览和下载

    通过POI和jacob可以实现word文档的在线预览和下载. 首先,引入以下maven依赖. <dependency><groupId>org.apache.poi</g ...

  9. excel怎么设置打印区域_第六节 EXCEL打印预览和页眉页脚设置

    点击蓝字 关注我们 今天,老铁给大家分享Excel脱白基础教程的第六节--EXCEL打印预览和页眉页脚设置. 调整打印页面设置.分页预览中的蓝色虚线把整个文件需要打印的区域进行一个分割.用鼠标光标指向 ...

最新文章

  1. 科技发展给保险行业带来了什么改变?
  2. 浅谈最近发布的金融行业多方安全计算的技术标准
  3. 十几万人同时在线的直播间聊天,如何设计服务端架构?
  4. 抗炎饮食与混合坚果粉
  5. android blcr 编译,BLCR 基本环境搭建【zz~】
  6. 当一个对象实例作为一个参数被传递到方法中时,参数的值就是对该对象的引用。对象的内容可以在被调用的方法中改变,但对象的引用是永远不会改变的.
  7. JS 一张图理解prototype、proto和constructor的关系
  8. php上传文件测试代码,php 文件上传函数的超详细示例
  9. ccd后视摄像头_预计2021年全球车载摄像头总出货将达到1.43亿颗
  10. 利用github for windows 工具将本地的内容同步到github上
  11. 转载CentOS7 yum 安装与配置MySQL5.7
  12. FTP上传无文件以及0字节问题
  13. 烽火计划项目成果-目录索引
  14. 计算机三级网络技术 = =
  15. 黑冰客防骗子—常见网络骗子骗术防御要点
  16. 视频加水印怎么加?比较简单的方法
  17. 【小程序】常见系统API | 页面分享 | 位置信息 | 本地存储
  18. 服务器维护首先查看指示灯,HP ILO2 使用详细教程[图文]
  19. 【YAML】【YAML的实践】【YAML的使用学习记录】
  20. 你算过这笔账么?月薪5000在中国和美国的生活各是怎样?

热门文章

  1. ENVI经验|基于多源遥感影像的红树林范围提取3-监督分类
  2. oracle的cbo,Oracle CBO术语大集合
  3. 上海无印良品地理空间分布特征与选址策略可视化研究
  4. 强引用,软引用,弱引用和虚引用的说明
  5. 如何下载沧州市卫星地图高清版大图
  6. 「津津乐道播客」#195. 996笼罩下的互联网“民工”
  7. on-chip-bus(四)AXI总线:突发长度、突发大小以及非对齐传输的理解
  8. 如何让Python代码加速运行?
  9. MapAbc Ajax 周边Http请求
  10. 简易GIt-----SFile 10秒上手