---
title: "ExcelUtils简单上手"
author: "Li-boy"
date: "2012-07-27T10:20:07Z"
tags: []
categories: ["Web前端"]
slug: "excelutils"
draft: false
lastmod: "2012-07-27T10:20:07Z"
id: 1608727
url: "https://li-boy.iteye.com/blog/1608727"
---

ExcelUtils的User's Guide  [http://excelutils.sourceforge.net/UserGuide.html](http://excelutils.sourceforge.net/UserGuide.html)

这里有个很清楚的测试例子可以看一下：[http://hi.baidu.com/henyue/blog/item/f24eb17edec19e340cd7dacf.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模板  
![](http://dl.iteye.com/upload/attachment/0071/3899/c729679c-4947-32df-9e30-1dc6c6f8fb95.png)  
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.结果如下图：

![](http://dl.iteye.com/upload/attachment/0071/3902/41c4bede-3a20-3d7a-bfbb-50ad8837089f.png)