zoukankan      html  css  js  c++  java
  • poi 实战代码---导出Excel(根据模板导出)

    /**

         * 导出excel

         * @param request  

         * @param response

         * @return

         * @throws Exception

         */

        @RequestMapping("exportExcel")

        @Action(description="导出excel")

        public void exportExcel(HttpServletRequest request, HttpServletResponse response) throws Exception

        {

           //获取实体类,并取得时刻点的值,存放到map里面

               String  id = RequestUtil.getString(request,"id");

                LoadDate ld = new LoadDate();

                ld = loadDateService.getById(Long.parseLong(id));

                String mapVal = ld.getValue();

                JSONObject json = JSONObject.fromObject(mapVal);

                Map<String,String> map = new HashMap<String,String>();

                if (BeanUtils.isNotEmpty(json)) {

                Iterator<String> iterator = json.keys();

                    while (iterator.hasNext()) {

                   String ie = (String) iterator.next();

                   map.put(ie, json.getString(ie));

                    }

                }

                String dirPath = FileUtil.getRootPath() + File.separator+"commons" +File.separator+"template"+File.separator+"exportMode"+File.separator;

               String fileName="loadTemplate.xls";

                FileInputStream inStream = new FileInputStream(new File(dirPath+fileName));

                //读取excel模板 

                HSSFWorkbook wb = new HSSFWorkbook(inStream);    //读取excel模板  

                //读取了模板内所有sheet内容 

                HSSFSheet sheet = wb.getSheetAt(0); 

                HSSFCell cell = null;

                //在相应的单元格进行赋值 

                for(int i=2;i<=97;i++){

                    cell = sheet.getRow(i).getCell(1);

                    String tmp = cell.getStringCellValue();

                    cell.setCellValue(map.get(tmp)); 

                }

               

                for(int i=98;i<=102;i++){

                    cell = sheet.getRow(i).getCell(1);

                    String tmp = cell.getStringCellValue();

                   

                    if("ycgfdl".equals(tmp)){

                       if(BeanUtils.isNotEmpty(ld.getYcgfdl())){

                           cell.setCellValue(ld.getYcgfdl()); 

                       }else{

                           cell.setCellValue(""); 

                       }

                    }else if("ycqdl".equals(tmp)){

                       if(BeanUtils.isNotEmpty(ld.getYcqdl())){

                           cell.setCellValue(ld.getYcqdl()); 

                       }else{

                           cell.setCellValue(""); 

                       }

                    }else if("yccfdLi".equals(tmp)){

                       if(BeanUtils.isNotEmpty(ld.getYccfdLi())){

                           cell.setCellValue(ld.getYccfdLi()); 

                       }else{

                           cell.setCellValue(""); 

                       }

                    }else if("yccfdLl".equals(tmp)){

                       if(BeanUtils.isNotEmpty(ld.getYccfdLl())){

                           cell.setCellValue(ld.getYccfdLl()); 

                       }else{

                           cell.setCellValue(""); 

                       }

                    }else if("ycdgdl".equals(tmp)){

                       if(BeanUtils.isNotEmpty(ld.getYcdgdl())){

                           cell.setCellValue(ld.getYcdgdl()); 

                       }else{

                           cell.setCellValue(""); 

                       }

                    }

                }

               

                response.setContentType("application/octet-stream;charset=UTF-8");

                response.setHeader("Content-Type","application/vnd.ms-excel");

                response.setHeader( "Content-Disposition", "attachment;filename=" + new String( fileName.getBytes("GB2312"), "8859_1" ));

                response.addHeader("Pargam", "no-cache"); 

                response.addHeader("Cache-Control", "no-cache");

                OutputStream out = response.getOutputStream();   

                wb.write(out);

                out.flush();   

                out.close();

            }

  • 相关阅读:
    统一前后台数据交互格式
    volatile关键字 学习记录2
    利用AOP与ToStringBuilder简化日志记录
    JAVA的那些数据结构实现总结,实现,扩容说明
    JAVA中的数据结构
    对把JDK源码的一些注解,笔记
    分析下为什么spring 整合mybatis后为啥用不上session缓存
    JAVA内存关注总结,作为个程序员需要对自己系统的每块内存做到了如指掌
    写颗搜索二叉树动动脑,开启今年的旅程,新年快乐
    内存快照排查OOM,加密时错误方法指定provider方式错误引起的OOM
  • 原文地址:https://www.cnblogs.com/rdchen/p/10245142.html
Copyright © 2011-2022 走看看