首页 > 代码库 > SpringMVC 下载XLS文档的设置

SpringMVC 下载XLS文档的设置

页面设置参考上文。SpringMVC 下载文本文档的设置

此文是关于xls文档下载的,这个文档使用Apache的POI生成,需要的jar包可以从下面地址下载:

http://pan.baidu.com/s/1i3IJttF

 

下面是controller代码,比上一篇文本的少很多:

    @RequestMapping("/downloadAnnotatorListXls")    public ModelAndView downloadAnnotatorListXls(HttpServletRequest request,HttpServletResponse response){        String fileName="annotatorList.xls";                response.reset();// 不加这一句的话会出现下载错误         response.setHeader("Content-disposition", "attachment;filename=" + fileName);   // 设定输出文件头           response.setContentType("application/vnd.ms-excel");   // 定义输出类型                 try {                        HSSFWorkbook xls=service.getAnnotatorListInXls();            OutputStream ouputStream = response.getOutputStream();               xls.write(ouputStream);               ouputStream.flush();               ouputStream.close();                         } catch (Exception e) {            e.printStackTrace();            logger.error(e);                        request.setAttribute("error", e.getClass());            request.setAttribute("reason", e.getMessage());            StackTraceElement[] arr=e.getStackTrace();            request.setAttribute("stackTraceElements", arr);                        return new ModelAndView("pages/error/index.jsp");        }                        return null;    }

具体xls生成请参考:

public HSSFWorkbook getAnnotatorListInXls() throws Exception{        StringBuilder sb=new StringBuilder();        sb.append("    select");        sb.append("        t1.id,");        sb.append("        t1.ownerId,");        sb.append("        t1.ownerName,");        sb.append("        t1.annotatorId,");        sb.append("        t1.name,");        sb.append("        t1.fullName,");        sb.append("        t1.language,");        sb.append("        format(t1.planHour,2) as planHour");        sb.append("    from");        sb.append("        ownership t1 ");        sb.append("    order by");        sb.append("        id");        String sql=sb.toString();                List<?> ls=this.getJdbcTemplate().query(sql, new NameValueRowMapper());                        HSSFWorkbook wb = new HSSFWorkbook();        HSSFSheet sheet = wb.createSheet("sheet1");                // 表头颜色        HSSFCellStyle blueStyle = wb.createCellStyle();        blueStyle.setBorderTop(HSSFCellStyle.BORDER_THICK);        blueStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);        blueStyle.setFillForegroundColor(HSSFColor.SKY_BLUE.index);        blueStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);                // 表头部分"ID,OwnerId,OwnerName,AnnotatorId,Name,FullName,Language,PlanHour\r\n"        HSSFRow row = sheet.createRow(0);//建立新行                HSSFCell cell =row.createCell((short)0);        cell.setCellValue("ID");        cell.setCellStyle(blueStyle);                cell =row.createCell((short)1);        cell.setCellValue("OwnerId");        cell.setCellStyle(blueStyle);                cell =row.createCell((short)2);        cell.setCellValue("OwnerName");        cell.setCellStyle(blueStyle);                cell =row.createCell((short)3);        cell.setCellValue("AnnotatorId");        cell.setCellStyle(blueStyle);                cell =row.createCell((short)4);        cell.setCellValue("Name");        cell.setCellStyle(blueStyle);                cell =row.createCell((short)5);        cell.setCellValue("FullName");        cell.setCellStyle(blueStyle);                cell =row.createCell((short)6);        cell.setCellValue("Language");        cell.setCellStyle(blueStyle);                cell =row.createCell((short)7);        cell.setCellValue("PlanHour");        cell.setCellStyle(blueStyle);                short rowNum=1;        short columnNum=0;        for(Object obj:ls){            row = sheet.createRow(rowNum);//建立新行            columnNum=0;                        List<NameValue> lsTemp=(List<NameValue>)obj;                        for(NameValue nv:lsTemp){                cell =row.createCell(columnNum);                cell.setCellValue(nv.getValue());                columnNum++;            }                        rowNum++;        }                        return wb;    }

 

SpringMVC 下载XLS文档的设置