用JBuilder2005开发spring MVC应用-显示PDF/Excel文档

类别:Java 点击:0 评论:0 推荐:
用JBuilder2005开发spring MVC应用-显示PDF/Excel文档 高科华  

作者简介:高科华,南京航空学院计算数学专业硕士,有十年以上的企业信息化工作经验。目前的研究兴趣,J2EE企业应用、ERP软件研发、数据仓库系统研发。

1.   按照“用JBuilder2005开发spring MVC应用” -与表示层技术xslt集成一文建立spring应用
2.    增加poi、iText类库
3.    修改views.properties文件

/web-inf/classes/views.properties

home.class=xslt.HomePage

home.stylesheetLocation=/WEB-INF/xsl/home.xslt

home.root=words

 

xl.class=excel.HomePage

pdf.class=pdf.PDFPage

4.       增加两个类文件

 

/src/excel/HomePage.java

package excel;

 

import java.util.*;

 

import javax.servlet.http.*;

 

import org.apache.poi.hssf.usermodel.*;

import org.springframework.web.servlet.view.document.*;

 

 

public class HomePage extends AbstractExcelView {

    protected void buildExcelDocument(

            Map model,

            HSSFWorkbook wb,

            HttpServletRequest req,

            HttpServletResponse resp) throws Exception {

        HSSFSheet sheet;

        HSSFRow sheetRow;

        HSSFCell cell;

// Go to the first sheet

// getSheetAt: only if wb is created from an existing document

//sheet = wb.getSheetAt( 0 );

        sheet = wb.createSheet("Spring");

        sheet.setDefaultColumnWidth((short) 12);

// write a text at A1

        cell = getCell(sheet, 0, 0);

        setText(cell, "Spring-Excel test");

        List words = (List) model.get("wordList");

        for (int i = 0; i < words.size(); i++) {

            cell = getCell(sheet, 2 + i, 0);

            setText(cell, (String) words.get(i));

        }

    }

}

 

 

 

 

 

 

 

 

/web-inf/pdf/PDFPage.java

package pdf;

 

import java.util.*;

import java.util.List;

 

import javax.servlet.http.*;

 

import com.lowagie.text.*;

import com.lowagie.text.pdf.*;

import org.springframework.web.servlet.view.document.*;

 

public class PDFPage extends AbstractPdfView {

    protected void buildPdfDocument(

            Map model,

            Document doc,

            PdfWriter writer,

            HttpServletRequest req,

            HttpServletResponse resp) throws Exception {

        List words = (List) model.get("wordList");

        for (int i = 0; i < words.size(); i++) {

            doc.add(new Paragraph((String) words.get(i)));

        }

    }

}

 

 

 

5.          修改HomePageController.java

        return new ModelAndView("home", map);

改home为xl或pdf。运行就可以看到不同的结果。

本文地址:http://com.8s8s.com/it/it12291.htm