POI下载模板功能

来源:互联网 发布:fm2016爆棚汉化mac 编辑:程序博客网 时间:2024/04/20 01:00

不解释,直接上源码

jsp:

    function mouldexport(){
        window.open( path+'/platform/batch/mouldexport?rsId=${param.rsId}');
    }


java:

    @RequestMapping("mouldexport")
    @ResponseBody
    public String mouldexport(HttpServletRequest request,HttpServletResponse response,String rsId) {
        List fileds = ShResourceCatalogService.findFileds(rsId);
        String str = ClobConvertString.ClobToString((Clob)fileds.get(0));
        ServiceConfigXmlObj xml = XmlUtil.toBean(str, ServiceConfigXmlObj.class);
        List<String> input = xml.getInputFields();
        // 第一步,创建一个webbook,对应一个Excel文件  
        HSSFWorkbook wb = new HSSFWorkbook();  
        // 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet  
        HSSFSheet sheet = wb.createSheet("sheet1");  
        // 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short  
        HSSFRow row = sheet.createRow((int) 0);  
        // 第四步,创建单元格,并设置值表头 设置表头居中  
        HSSFCellStyle style = wb.createCellStyle();  
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式  
        HSSFCell cell = row.createCell(0);
        List<String> srList = ShResourceCatalogService.findShDataFieldAndShRelDataField(rsId);
        if(srList == null || srList.size() < 1){
            return null;
        }
        Map<String,String> srMap = ListToMap.getAttribute(srList);//1.name 2.name_cn 3.type
        if(input != null && input.size()>0){
            String inputKey = "";
            for(int i = 0; i < input.size(); i++){
                inputKey = String.valueOf(input.get(i));
                sheet.setColumnWidth(i, 5000);
                cell.setCellValue(srMap.get(inputKey).split(",")[1]);  
                cell.setCellStyle(style);
                cell = row.createCell(i+1);
            }
        }
        try {
            String filename= "mouldexport.xls";
            response.setContentType("utf-8");
            response.setHeader("Content-Disposition", "attachment;fileName="
                    + URLEncoder.encode(filename, "UTF-8"));
            OutputStream os = response.getOutputStream();
            wb.write(os);
            os.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

0 0
原创粉丝点击