ExcelUtils简单上手

 ExcelUtilsUser'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.结果如下图:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值