zoukankan      html  css  js  c++  java
  • Java net.sf.jxls 生成模板 并导出excel

    如果是 maven项目需要引入下面这个就可以

    <dependency>
              <groupId>net.sf.jxls</groupId>
              <artifactId>jxls-core</artifactId>
              <version>1.0.3</version>
          </dependency>
    
    
    1. 首先应该先把模板格式定好。
    2. 将数据传输过去
    3. 遍历数据,之后再深层嵌套遍历

    直接上代码

         /**
         * 导出采购订单
         */
        @RequiresPermissions("tool:gen:code")
        @BussinessLog(title = "导出采购订单", businessType = BusinessType.GENCODE)
        @GetMapping("/exportToProveExcel/{id}")
        public void exportToProveExcel(HttpServletResponse response,@PathVariable("id") String id) throws IOException {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String modelsPath = Global.getTemplatePath();
    
    
            PurchaseBillsEntity billsEntity = purchaseBillsService.findPurchaseBillsById(id);
            billsEntity.setPurInvs(purchaseInvsService.findPurchaseInvsByBillId(id));
            Map<String, PurchaseBillsEntity> beans = Maps.newHashMap();
            String sDate=sdf.format(billsEntity.getPurTime());
            billsEntity.setPurTime1(sDate);
            billsEntity.setExcelDetails(purchaseBillsService.collateSpecialty(billsEntity.getPurInvs()));
            beans.put("purchase", billsEntity);
    
    
            XLSTransformer transformer = new XLSTransformer();
            String xlsTemplateFileName = modelsPath+"/purchaseTemplate.xls";
            String outputFileName = "purchase-" + sdf.format(new Date()) + ".xls";
    
            InputStream in=null;
            OutputStream out=null;
            //设置响应
            response.setHeader("Content-Disposition", "attachment;filename=" + outputFileName);
            response.setContentType("application/vnd.ms-excel");
            try {
                in=new BufferedInputStream(new FileInputStream(xlsTemplateFileName));
                Workbook workbook=transformer.transformXLS(in, beans);
                out=response.getOutputStream();
                //将内容写入输出流并把缓存的内容全部发出去
                workbook.write(out);
                out.flush();
            } catch (InvalidFormatException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (in!=null){try {in.close();} catch (IOException e) {}}
                if (out!=null){try {out.close();} catch (IOException e) {}}
            }
        }
    
     @TableField(exist = false)
        private List<ExcelPlanDetailsEntity> excelDetails;
    
    
    @Data
    
    
    public class ExcelPlanDetailsEntity implements  Serializable {
    
    private  String specialty;
    
    private List<PurchaseInvsEntity> invsEntityList; //这里直接用的是嵌套循环(每一个专业对应多个个物料)
    
    }
    
    

    下面模板会显示出来

    前段点击链接 跳转的方式进行链接

     function getFilePath(id) {
            location.href =baseURL + "pos/purchaseBills/exportToProveExcel/"+id;
            opt.modal.msg('执行成功,正在生成采购订单请稍后…');
        }
    
  • 相关阅读:
    算法总结之 两个链表生成相加链表
    算法总结之 复制含有随机指针节点的链表
    算法总结之 将单向链表按某值划分成左边小、中间相等、右边大的形式
    在PHP5.3以上版本运行ecshop和ecmall出现的问题及解决方案
    windows下配置nginx+php环境
    ecmall程序结构图与数据库表分析
    ecmall数据字典
    Ecmall二次开发-增删改查操作
    PHP7:10件事情你需要知道的
    PHP命名空间规则解析及高级功能3
  • 原文地址:https://www.cnblogs.com/jxl123456/p/14889503.html
Copyright © 2011-2022 走看看