用spring MVC 生成Excel和PDF

jsp页面:

<input name="button" type="button" id="button" value="保存excel"
 onclick="javascript:window.open('../demo/viewExcel.xhtml');"/>

 

@Controller

    @RequestMapping(value="/viewExcel")
    public ModelAndView viewExcel(HttpServletRequest request){
        List list = new ArrayList(); 
        Map model = new HashMap(); 
        list.add("test1"); 
        list.add("test2"); 
        model.put("list", list); 
        ViewExcel viewExcel = new ViewExcel(); 
//        log.debug("ViewController.viewExcel is ended......");
        return new ModelAndView(viewExcel, model);
    }

 

@RequestMapping(params="method=pdftest",method=RequestMethod.GET)
public ModelAndView viewPDF(HttpServletRequest request,  
            HttpServletResponse response) throws Exception {  
        List list = new ArrayList();  
        Map model = new HashMap();  
        list.add("test1");  
        list.add("test2");  
        model.put("list", list);  
        ViewPDF viewPDF = new ViewPDF();  
        return new ModelAndView(viewPDF, model);  
    }  
}

 

 生成excel视图,可用excel工具打开或者保存

ViewExcel类(继承AbstractExcelView)

public class ViewExcel extends AbstractExcelView {
    @Override
    protected void buildExcelDocument(Map<String, Object> arg0,
            HSSFWorkbook workbook, HttpServletRequest arg2, HttpServletResponse arg3)
            throws Exception {
        // TODO Auto-generated method stub
          HSSFSheet sheet = workbook.createSheet("list"); 
            sheet.setDefaultColumnWidth((short) 12); 
     
            HSSFCell cell = getCell(sheet, 0, 0); 
            setText(cell, "Spring Excel test"); 
     
            HSSFCellStyle dateStyle = workbook.createCellStyle(); 
            //dateStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("mm/dd/yyyy")); 
            cell = getCell(sheet, 1, 0); 
            cell.setCellValue("日期:2008-10-23"); 
            //cell.setCellStyle(dateStyle); 
            getCell(sheet, 2, 0).setCellValue("测试1"); 
            getCell(sheet, 2, 1).setCellValue("测试2");


            HSSFRow sheetRow = sheet.createRow(3); 
            for (short i = 0; i < 10; i++) { 
                sheetRow.createCell(i).setCellValue(i * 10); 
            }  
    }
}

 

/**
* 生成PDF视图,可用PDF浏览器打开或者保存
* 由ViewController的return new ModelAndView(viewPDF, model)生成
* @author Tony Lin Created on 2008-10-22
* @version Version 1.0
*/
public class ViewPDF extends AbstractPdfView {  
    public void buildPdfDocument(Map model, Document document,  
            PdfWriter writer, HttpServletRequest request,  
            HttpServletResponse response) throws Exception {  
  
        List list = (List) model.get("list");  
  
        for (int i = 0; i < list.size(); i++)  
            document.add(new Paragraph((String) list.get(i)));  
    }  
}

MAVEN pom.xml文件

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.6</version>
        </dependency>
        <dependency>
            <groupId>javax.mail</groupId>
            <artifactId>mail</artifactId>
            <version>1.4</version>
        </dependency>

 

你可能感兴趣的:(spring mvc)