Ver Mensaje Individual
  #2 (permalink)  
Antiguo 10/07/2013, 16:24
abulon81
 
Fecha de Ingreso: mayo-2010
Mensajes: 99
Antigüedad: 13 años, 11 meses
Puntos: 5
Respuesta: generar excel desde spring mvc

La segunda forma dentro del controlador es esta

Código PHP:

 
@RequestMapping(value ="/list/excel")
    public 
View listExcell() {
        return new 
AbstractExcelView() {
            @
Override
            
protected void buildExcelDocument(Map<StringObjectmodelHSSFWorkbook workbook,
                    
HttpServletRequest requestHttpServletResponse responsethrows Exception {
                
                
response.setContentType ("APPLICATION / OCTET-STREAM"); 
                
response.addHeader("Content-Disposition""attachment; filename=\"employee-list.xls\"");
                
logger.info("generando reporte excel..");

                
                
genericoBean stu1 = new genericoBean ("gaoxiang1""male1""20060101""1"); 
                
genericoBean stu2 = new genericoBean ("gaoxiang2""male2""20060102""2"); 
                
genericoBean stu3 = new genericoBean ("gaoxiang3""male3""20060103""3"); 
                
genericoBean stu4 = new genericoBean ("gaoxiang4""male4""20060104""4"); 
                
genericoBean stu5 = new genericoBean ("gaoxiang5""male5""20060105""5"); 
                
ArrayList stuList = new ArrayList (); 
                
stuList.add (stu1); 
                
stuList.add (stu2); 
                
stuList.add (stu3); 
                
stuList.add (stu4); 
                
stuList.add (stu5); 

                
// Excel header 
                
HSSFSheet sheet workbook.createSheet("studentList"); 
                
HSSFRow header sheet.createRow (0); // row 0 
                // Generate the title bar 
                
header.createCell ((short0).setCellValue ("name"); 
                
header.createCell ((short1).setCellValue ("sex"); 
                
header.createCell ((short2).setCellValue ("date"); 
                
header.createCell ((short3).setCellValue ("count"); 
                
HSSFCellStyle cellStyle workbook.createCellStyle (); 
                
cellStyle.setDataFormat (HSSFDataFormat.getBuiltinFormat ("mm / dd / yyyy")); 

                
// Fill data 
                         
int rowNum 1
                for (
Iterator iter stuList.iterator (); iter.hasNext () ;) {
                
genericoBean element = (genericoBeaniter.next (); 
                
HSSFRow row sheet.createRow (rowNum ++); 
                
row.createCell ((short0). setCellValue (element.getAttr1(). toString ()); 
                
row.createCell ((short1). setCellValue (element.getAttr2(). toString ()); 
                
row.createCell ((short2). setCellValue (element.getAttr3(). toString ()); 
                
row.getCell ((short2). setCellStyle (cellStyle); 
                
row.createCell ((short3). setCellValue (element.getAttr4()); 
                } 

                
// Column is calculated as the sum of 
                
HSSFRow row sheet.createRow (rowNum); 
                
row.createCell ((short0). setCellValue ("TOTAL:"); 
                
String formual "SUM (D2: D" rowNum ")"// D2 to D [rownum] cell Ki (count data) 
                
row.createCell ((short3). setCellFormula (formual); 

                
//} 
            
}
        };
    } 
Y en el jsp podemos poner lo siguiente

Código PHP:
<input type "button" onclick "javascript:window.location.href ='<%=request.getContextPath()%>/list/excel.htm'" value "download excel" /> 

<
a href="<%=request.getContextPath()%>/excelmvc.htm" >Download in Excel doc</a>
<
br>
<
a href="<%=request.getContextPath()%>/list/excel.htm" >Download in Excel2</a
:distraido