1.封装后台数据

hql=" select o from Zjsymxb o where 1=1 and (o.user.department.name like '%"+depunit+"%' or o.user.department.fdep.id like '"+depNameId+"')"+hql;
        String hql2015=hql+"and o.fillDate=2015";
        String hql2016=hql+"and o.fillDate=2016";
        String hql2017=hql+"and o.fillDate=2017";
        String hql2018=hql+"and o.fillDate=2018";
        List<Zjsymxb> list2015 = downService.findByList(hql2015);
        List<Zjsymxb> list2016 = downService.findByList(hql2016);
        List<Zjsymxb> list2017 = downService.findByList(hql2017);
        List<Zjsymxb> list2018 = downService.findByList(hql2018);
        Map<String, Object> map = new HashMap<String, Object>();  
        map.put("list2015", list2015);
        map.put("list2016", list2016);
        map.put("list2017", list2017);
        map.put("list2018", list2018);
        ArrayList<List> objects = new ArrayList<List>();  
        objects.add(list2015);  
        objects.add(list2016);  
        objects.add(list2017);  
        objects.add(list2018);  
         //sheet的名称  
        List<String> listSheetNames = new ArrayList<String>();  
        listSheetNames.add("2015");  
        listSheetNames.add("2016");  
        listSheetNames.add("2017");  
        listSheetNames.add("2018");  
        String templateName="资金明细.xls";
        //生成文件
        String fileto = doFile("Zjsymxb",templateName,objects,listSheetNames);

 

    /**
     * 
     * @param filename 文件夹名
     * @param templateName 模板名字
     * @param listSheetNames 
     * @param objects 
     * @param beans  数据
     * @throws IOException 
     * @throws InvalidFormatException 
     * @throws ParsePropertyException 
     */
    private String doFile(String str,String templateName, ArrayList<List> objects, List<String> listSheetNames) throws ParsePropertyException, InvalidFormatException, IOException {
        String basepath = ServletActionContext.getServletContext().getRealPath(File.separator) +"WEB-INF\\templates"+File.separator+str+File.separator;
        String basepathto = ServletActionContext.getServletContext().getRealPath(File.separator) + "Excel";
        File filepath = new File(basepath);  
           // 如果指定的路径没有就创建  
           if (!filepath.exists()) { 
               filepath.mkdirs();  
           }
        long currentTime = System.currentTimeMillis();
        String realname = currentTime + "@" +templateName;//文件名称
        String datePath = new SimpleDateFormat("\\yyyy\\MM\\dd\\").format(new Date());
        File file1 = new File(basepathto + datePath );//文件存放目录
           if (!file1.exists()) { 
               file1.mkdirs(); 
           }
        String filefrom=basepath+templateName;//模板文件地址
        String fileto=file1+File.separator+realname;//存放生成文件的地址 
        doExcel(filefrom,fileto,objects,listSheetNames);
        return fileto;
    }
/**
 * 
 * @param filefrom 模板地址
 * @param fileto  生成后存放的地址
 * @param objects  用于输出的文件,是一个list<sheet>的数据,一个sheet代表一个页面.
 * @param listSheetNames sheet名称,注意:list的size决定了sheet的个数
 * @throws ParsePropertyException
 * @throws InvalidFormatException
 * @throws IOException
 */
    private void doExcel(String filefrom,String fileto, ArrayList<List> objects, List<String> listSheetNames) throws ParsePropertyException, InvalidFormatException, IOException{
            FileInputStream is = new FileInputStream(filefrom); 
            XLSTransformer transformer = new XLSTransformer();  
            Workbook workbook = transformer.transformMultipleSheetsList(is, objects, listSheetNames, "list", new HashMap(), 0);  
            workbook.write(new FileOutputStream(fileto));
    }

 

---恢复内容结束---

posted on 2016-08-22 16:18  夏晴  阅读(729)  评论(0编辑  收藏  举报