spring里结合POI生成EXCEL

spring里支持从数据动态生成PDF/XSL,PDF的很多例子了,而Spring可以结合Apache的POI包,来生成EXCEL的。

首先到http://jakarta.apache.org/poi/index.html去下载poi包,将其放到工程目录的WEB-INF\LIB下。

POI的具体操作例子可以到http://jakarta.apache.org/poi/hssf/quick-guide.html这里去看。

其实讲到底是利用了spring的ResourceBundleViewResolver的。

下面列一个一本书上的例子,以供备考

song.java

package springxls;

public class Song {

privateStringsinger;

 private String title;

 public Song(String singer, String title) {

this.singer=singer;

this.title=title;

}

publicStringgetSinger(){

returnsinger;

}

publicStringgetTitle(){

returntitle;

}

}

控制器SongPageController.java

publicclassSongPageControllerextendsAbstractController{

privateStringsuccessView;

publicvoidsetSuccessView(StringsuccessView){

this.successView=successView;

 }

 protected ModelAndView handleRequestInternal(HttpServletRequest request,

HttpServletResponseresponse)

throwsException{

Mapmap=newHashMap();

    List songList = new ArrayList();

    songList.add(new Song("James Blunt", "Goodbye My Lover"));    songList.add(new Song("MC HotDog", "我行我素"));

    map.put("songlist", songList);

    return new ModelAndView(successView, map);

 }

}

生成Xsl文件

mportjava.util.Iterator;

importjava.util.List;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;

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

importorg.apache.poi.hssf.usermodel.HSSFRow;

importorg.apache.poi.hssf.usermodel.HSSFSheet;

importorg.apache.poi.hssf.usermodel.HSSFWorkbook;

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

public class SongXlsPage extends AbstractExcelView {

publicvoidbuildExcelDocument(

Mapmodel,HSSFWorkbookworkbook,

HttpServletRequestrequest,HttpServletResponseresponse)

   throws Exception {

  int sheetRowNum = 0;

  //取得模型数据

Listsongs=(List)model.get("songlist");

Iterator<Song>iter=songs.iterator();

//创建工作表和标题

HSSFSheetsheet=workbook.createSheet("MySong");

HSSFRowtitleRow=sheet.createRow((short)sheetRowNum++);

HSSFCelltitleCell=titleRow.createCell((short)0);

titleCell.setCellValue("MySongs");

//创建一个空行

sheet.createRow(sheetRowNum++);

//创建数据表头

String[]titles={"Title","Singer"};

HSSFRowdataTitleRow=sheet.createRow((short)sheetRowNum++);

for(inti=0;i<titles.length;i++){

HSSFCellcell=dataTitleRow.createCell((short)i);

cell.setCellValue(titles[i]);

}

//数据模型转换:创建表格数据

for(inti=sheetRowNum;i<songs.size()+sheetRowNum;i++){

if(iter.hasNext()){

Songsong=iter.next();

HSSFRowdataRow=sheet.createRow((short)(i));

HSSFCellcell1=dataRow.createCell((short)0);

//中文显示指定编码

cell1.setEncoding(HSSFCell.ENCODING_UTF_16);

HSSFCellcell2=dataRow.createCell((short)1);

cell1.setCellValue(song.getTitle());

cell2.setCellValue(song.getSinger());

}

}

}

}

web.xml

<servlet>

<servlet-name>frontcontroller</servlet-name>

<servlet-class>

org.springframework.web.servlet.DispatcherServlet

</servlet-class>

<load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>frontcontroller</servlet-name>

<url-pattern>*.do</url-pattern>

</servlet-mapping>

froncontroller-servlet.xml<beans>

<beanid="viewResolver"

class="org.springframework.web.servlet.view.ResourceBundleViewResolver">

<propertyname="basename">

<value>views</value>

</property>

  </bean>

  <bean id="defaultHandlerMapping"  class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>

    <bean name="/viewsong-xls.do"

class="springxls.SongPageController">

<propertyname="successView"value="xlsSong"/>

</bean>

</beans>

相关推荐