当前位置: 代码迷 >> Web前端 >> 导出Excel聚合
  详细解决方案

导出Excel聚合

热度:616   发布时间:2013-12-17 12:06:34.0
导出Excel集合

?? 导出Excel集合

public void exportAll() {   
        if(getTemplateName()!=null){   
            String templateName=getTemplateName();             
            String sortstr=FacesUtils.getValueInHashtableOfSession("sortstr").toString();   
            String hql=FacesUtils.getValueInHashtableOfSession("hql").toString()+sortstr;   
            Map map=(Map)FacesUtils.getValueInHashtableOfSession("map");           
            List list = this.getService().getPageData(hql, map);               
            Map beans=new HashMap();   
            beans.put("list", list);   
            String filename=templateName+Tools.getNowTime()+".xls";            
            exportExcel(beans, templateName+".xls", filename);             
            //日志   
            String message=templateName+"报表导出";   
            saveOpinfo(message);   
        }   
    }   
  
public void exportExcel(Map beans,String templateName,String filename) {   
        try {   
            HttpServletResponse response = ServletActionContext.getResponse();   
            response.setContentType("application/vnd.ms-excel");   
            response.setHeader("Content-disposition", "attachment; filename=\"" + filename);   
            ServletOutputStream outStream = response.getOutputStream();   
            ExcelUtils.exportExcel(beans, this.getText("template.path") + System.getProperty("file.separator") + templateName, outStream);             
            response.getOutputStream().flush();   
            response.getOutputStream().close();            
            outStream.close();   
        } catch (FileNotFoundException e) {   
            e.printStackTrace();   
        } catch (UnsupportedEncodingException e) {   
            e.printStackTrace();   
        } catch (IOException e) {   
            e.printStackTrace();   
        }   
    }   
  
  
import net.sf.jxls.transformer.XLSTransformer;   
  
public static void exportExcel(Map beans, String templateName, OutputStream out) {   
        XLSTransformer transformer = new XLSTransformer();   
        try {   
            FileInputStream fis=new FileInputStream(templateName);   
            transformer.transformXLS(fis, beans).write(out);   
               
            fis.close();   
        } catch (ParsePropertyException e) {   
            e.printStackTrace();   
        } catch (FileNotFoundException e) {   
            e.printStackTrace();   
        } catch (IOException e) {   
            e.printStackTrace();   
        } catch (InvalidFormatException e) {   
            e.printStackTrace();   
        }   
    }  

?

  相关解决方案