ExcelUtils的User's Guide http://excelutils.sourceforge.net/UserGuide.html
这里有个很清楚的测试例子可以看一下:http://hi.baidu.com/henyue/blog/item/f24eb17edec19e340cd7dacf.html
我在这里就写在Web中的应用了:
1. 首先在项目加入以下五个jar包:
- excelutils.jar
- poi-2.5.1.jar
- commons-logging.jar
- commons-digester.jar
- commons-beanutils.jar
2. 建立一个Excel模板
3. 在Action中
/**
* 导出报表
* @param response
* @return
* @throws ExcelException
* @throws IOException
*/
@Action("/finishbillInfoExport")
public String finishbillInfoExport(){
String CMaketabletime = Struts2Utils.getParameter("CMaketabletime");
String CTrainsetid = Struts2Utils.getParameter("CTrainsetid");
String CTrainsetno = Struts2Utils.getParameter("CTrainsetno");
String CTrackname = Struts2Utils.getParameter("CTrackname");
mapList = finishbillInfoService.queryFinishbillDetail();
if(!("undefined".equals(CMaketabletime))){
ExcelUtils.addValue("CMaketabletime", CMaketabletime);
}
if(!("undefined".equals(CTrainsetid))){
ExcelUtils.addValue("CTrainsetid", CTrainsetid);
}
if(!("undefined".equals(CTrainsetno))){
ExcelUtils.addValue("CTrainsetno", CTrainsetno);
}
if(!("undefined".equals(CTrackname))){
ExcelUtils.addValue("CTrackname", CTrackname);
}
ExcelUtils.addValue("details", mapList);
String path = "/pages/assignment/templates/finishbillDetails.xls";
try {
HttpServletResponse response = ServletActionContext.getResponse();
response.reset();
response.setCharacterEncoding( "UTF-8" );
response.setHeader( "Content-disposition", "attachment; filename=" + System.currentTimeMillis()+".xls");
response.setContentType("application/msexcel");
ExcelUtils.export(ServletActionContext.getServletContext(),path,response.getOutputStream());
} catch (ExcelException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
4.结果如下图: