• 按照模板导出复杂样式的excel


    导出excel通常使用的是apache poi,但是poi的api相当复杂,所以当导出的excel样式比较复杂时,写起来就比较头疼了,这里推荐使用easypoi, 可以很方便的根据模板来导出复杂excel

    文档地址: 1.1 介绍 - Powered by MinDoc

    我们要实现如图所示效果,该怎么实现呢

     第一步,导入依赖

    1. <dependency>
    2. <groupId>cn.afterturngroupId>
    3. <artifactId>easypoi-spring-boot-starterartifactId>
    4. <version>4.5.0version>
    5. dependency>

    第二步,新建excel模板,然后写入内容

    如果是变量,就用双大括号包裹变量,如果是列表,则使用$fe指令,maplist是变量名,t.id是要输出的字段,最后以t.hdje}}结果表示结束

    模板指令文档:EasyPoi教程_V1.0

    示例模板下载地址:  src/test/resources/WEB-INF/doc/专项支出用款申请书_map.xls · 悟耘开源/easypoi-test - Gitee.com

     

    第三步,获取数据,写入输出流

    1. public class ExportController extends BaseController {
    2. @Operation(summary = "导出支出用款申请书")
    3. @GetMapping("exportPayForm")
    4. public void exportPayForm(HttpServletRequest request, HttpServletResponse response) throws IOException {
    5. TemplateExportParams params = new TemplateExportParams(
    6. "template/专项支出用款申请书_map.xls");
    7. Map map = new HashMap();
    8. map.put("date", "2014-12-25");
    9. map.put("money", 2000000.00);
    10. map.put("upperMoney", "贰佰万");
    11. map.put("company", "执笔潜行科技有限公司");
    12. map.put("bureau", "财政局");
    13. map.put("person", "JueYue");
    14. map.put("phone", "1879740****");
    15. List> listMap = new ArrayList>();
    16. for (int i = 0; i < 4; i++) {
    17. Map lm = new HashMap();
    18. lm.put("id", i + 1 + "");
    19. lm.put("zijin", i * 10000 + "");
    20. lm.put("bianma", "A001");
    21. lm.put("mingcheng", "设计");
    22. lm.put("xiangmumingcheng", "EasyPoi " + i + "期");
    23. lm.put("quancheng", "开源项目");
    24. lm.put("sqje", i * 10000 + "");
    25. lm.put("hdje", i * 10000 + "");
    26. listMap.add(lm);
    27. }
    28. map.put("maplist", listMap);
    29. Workbook workbook = ExcelExportUtil.exportExcel(params, map);
    30. addFileHeader(request, response, "专项支出用款申请书.xls", "application/octet-stream");
    31. workbook.write(response.getOutputStream());
    32. }
    33. }
    34. public void addFileHeader(HttpServletRequest request, HttpServletResponse response,
    35. String fileName, String contentType) {
    36. try {
    37. String agent = request.getHeader("USER-AGENT").toLowerCase();
    38. response.setContentType(contentType);
    39. String codedFileName = java.net.URLEncoder.encode(fileName, "UTF-8");
    40. if (agent.contains("firefox")) {
    41. response.setCharacterEncoding("utf-8");
    42. response.setHeader("content-disposition", "attachment;filename=" + new String(fileName.getBytes(), "ISO8859-1"));
    43. } else {
    44. response.setHeader("content-disposition", "attachment;filename=" + codedFileName);
    45. }
    46. } catch (Exception e) {
    47. e.printStackTrace();
    48. }
    49. }

     

     

  • 相关阅读:
    Scala 高阶:Scala中的模式匹配
    来看看老旧物件这样与现代空间结合完美结合
    SpringSecurity_权限管理
    JS:原型-原型链-ES5继承
    [Qt]窗口
    iview框架menu菜单展开的问题
    如何优雅的使用contorller层
    求质数的方法
    java计算机毕业设计在线问答平台源码+系统+mysql数据库+lw文档+部署
    符合 EN55022B 规格、LTM4613EY、LTM4613MPV直流µModule稳压器【RG500Q 5G Sub-6 GHz 模块】
  • 原文地址:https://blog.csdn.net/ting4937/article/details/138084429