用spring MVC 生成Excel和PDF


HTML页面并不总是向用户显示数据输出的最好方式,Spring支持从数据动态生成PDF或Excel文件,并使这一过程变得简单。文档本身就是视图,从服务器以流的方式加上内容类型返回文档,客户端PC只要运行电子表格软件或PDF浏览软件就可以浏览。下面是一个简单实现的例子:界面两个button,一个实现excel ,一个pdf.为了使用Excel电子表格,你需要在你的classpath中加入‘poi’库文件,而对PDF文件,则需要iText.jar文件。它们都包含在Spring的主发布包中。另外经过测试还需要加入包spring- beans.jar,spring-context.jar,spring-web.jar,spring-webmvc.jar。

1、web.xml配置文件,主要配置相应的servlet即可

<!-- Spring MVC 的Servlet,它将加载WEB-INF/spring-servlet.xml 以启动Spring MVC模块-->  
<servlet>  
<servlet-name>springmvc</servlet-name>  
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-
class>  
<load-on-startup>1</load-on-startup>  
</servlet>  
<servlet-mapping>  
<servlet-name>springmvc</servlet-name>  
<url-pattern>*.form</url-pattern>  
</servlet-mapping>

2、控制文件及spring-servlet.xml配置,增加ViewController的配置

<!-- 生成excel或pdf视图 -->  
<bean class="com.lzk.controller.ViewController">  
</bean>

3、ViewController的代码

package com.lzk.controller;  

import java.util.ArrayList;  
import java.util.HashMap;  
import java.util.List;  
import java.util.Map;  
import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse;  
import org.springframework.stereotype.Controller;  
import org.springframework.web.bind.annotation.RequestMapping;  
import org.springframework.web.bind.annotation.RequestMethod;  
import org.springframework.web.servlet.ModelAndView;  
import com.supcon.electronic.its.common.ViewExcel;  
import com.supcon.electronic.its.common.ViewPDF;  
/**  
* 生成excel或PDF类型试图  
* 根据参数进行数据组装,并跳转到相应的视图页面  
* View Controller Bean<br>  
*  
* @author Tony Lin Created on 2008-10-22  
* @version Version 1.0  
*/  

@Controller  
@RequestMapping("/view.form")  
public class ViewController extends BaseController {  

@RequestMapping(params="method=exceltest",method=RequestMethod.GET)  
public ModelAndView viewExcel(HttpServletRequest request, HttpServletResponse
response){  
    log.debug("ViewController.viewExcel is started......");  
    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);  
}  
}

4、用于Excel视图的视图子类

package com.lzk.common;  
import java.util.Date;  
import java.util.Map;  
import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse;  
import org.apache.poi.hssf.usermodel.HSSFCell;  
import org.apache.poi.hssf.usermodel.HSSFCellStyle;  
import org.apache.poi.hssf.usermodel.HSSFDataFormat;  
import org.apache.poi.hssf.usermodel.HSSFRow;  
import org.apache.poi.hssf.usermodel.HSSFSheet;  
import org.apache.poi.hssf.usermodel.HSSFWorkbook;  
import org.springframework.web.servlet.view.document.AbstractExcelView;  
/**  
* 生成excel视图,可用excel工具打开或者保存  
* 由ViewController的return new ModelAndView(viewExcel, model)生成  
* @author Tony Lin Created on 2008-10-22  
* @version Version 1.0  
*/  
public class ViewExcel extends AbstractExcelView {  

    public void buildExcelDocument(Map model, HSSFWorkbook workbook,  
    HttpServletRequest request, HttpServletResponse response)  
    throws Exception {  

        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);  
    }  

}  
}

5、用于PDF视图的视图子类

package lzk.common;  
import java.util.List;  
import java.util.Map;  
import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse;  
import org.springframework.web.servlet.view.document.AbstractPdfView;  
import com.lowagie.text.Document;  
import com.lowagie.text.Paragraph;  
import com.lowagie.text.pdf.PdfWriter;  
/**  
* 生成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)));  
        }  
    }

6、JSP页面调用

<a href="<%=path %>/view.form?method=pdftest" target="blank">保存pdf</a>  
<input name="button" type="button" id="button" value="保存excel"  οnclick="javascript:window.open('<%=path %>/view.form?method=exceltest');"/>

以上代码是基于spring2.5,JDK1.5,tomcat1.5测试通过。


原文链接:https://blog.csdn.net/linlzk/article/details/3389925