当前位置: 代码迷 >> Web前端 >> XLSTransformer生成excel文件简略示例
  详细解决方案

XLSTransformer生成excel文件简略示例

热度:379   发布时间:2013-10-14 12:54:46.0
XLSTransformer生成excel文件简单示例
package util;

import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.jxls.exception.ParsePropertyException;
import net.sf.jxls.transformer.XLSTransformer;
/**
 * Excel生成类.
 */
public class ExcelUtil {
	/**
	 * 根据模板生成Excel文件.
	 * @param templateFileName 模板文件.
	 * @param list 模板中存放的数据.
	 * @param resultFileName 生成的文件.
	 */
	public void createExcel(String templateFileName, Map<String,Object> beanParams, String resultFileName){
		//创建XLSTransformer对象
		XLSTransformer transformer = new XLSTransformer();
		//获取java项目编译后根路径
		URL url = this.getClass().getClassLoader().getResource("");
		//得到模板文件路径
		String srcFilePath = url.getPath() + templateFileName;
		String destFilePath = url.getPath() + resultFileName;
		try {
			//生成Excel文件
			transformer.transformXLS(srcFilePath, beanParams, destFilePath);
		} catch (ParsePropertyException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}

?

package test;

import java.util.ArrayList;
import java.util.List;

import po.Fruit;
import util.ExcelUtil;
/**
 * 测试类.
 */
public class Test {

	public static void main(String[] args) {
		List<Fruit> list = new ArrayList<Fruit>();
		list.add(new Fruit("苹果",2.01f));
		list.add(new Fruit("桔子",2.05f));
		String templateFileName = "template/template.xls";
		String resultFileName = "result/fruit.xls";
		Map<String,Object> beanParams = new HashMap<String,Object>();
		beanParams.put("list", list);
		new ExcelUtil().createExcel(templateFileName,beanParams,resultFileName);

	}

}

?template.xls模板文件截图:

?

注意:如果你是用的office 2007生成的excel模板,要另存为97-2003版本的。

package po;
/**
 * 水果.
 */
public class Fruit {
	/**
	 * 水果名称.
	 */
	private String name;
	/**
	 * 水果价格.
	 */
	private float price;
	
	
	public Fruit() {
		super();
	}
	
	public Fruit(String name, float price) {
		super();
		this.name = name;
		this.price = price;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public float getPrice() {
		return price;
	}
	public void setPrice(float price) {
		this.price = price;
	}
	
}

?

生成fruit.xls文件截图:

?

-------注意

?如果是测试。需要:

public void export2(String filename, Map dataMap, String model) throws IOException{
		XLSTransformer transformer = new XLSTransformer();
		HSSFWorkbook workbook = null;
		InputStream in;
		try {
			String templateDir= "D:/logs/areaReport.xls";  
			in =  new FileInputStream(templateDir);
			workbook = transformer.transformXLS(in, dataMap);
			
			String targetDir="D:/logs/testDynaMergeCells.xls";
			OutputStream os = new FileOutputStream(targetDir);  
			 
			workbook.write(os);  
			  
			os.close();  

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

?

?

如果是页面:

/**
	 * 
	 * @param filename	导出的文件名
	 * @param dataMap	List的集合
	 * @param model	
	 * @param response
	 * @throws IOException
	 */
	public void export(String filename, Map dataMap, String model, HttpServletResponse response) throws IOException{
		XLSTransformer transformer = new XLSTransformer();
		HSSFWorkbook workbook = null;
		InputStream in;
		try {

			//此方法需要类与模板放在同一目录下 
			in = getClass().getResource(model).openStream();
			workbook = transformer.transformXLS(in, dataMap);
			outExcel(workbook, response, filename);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	protected void outExcel(HSSFWorkbook workbook, HttpServletResponse response,
		    String filename) {
		   response.setContentType("application ns.ms-excel");
		   response.setHeader("Expires", "0");
		   response.setHeader("Cache-Control",
		     "must-revalidate, post-check=0, pre-check=0");
		   response.setHeader("Pragma", "public");
		   response.setHeader("Content-disposition", "attachment;filename="+filename+".xls");

		   try {
				workbook.write(response.getOutputStream());
		   } catch (ParsePropertyException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

?

?

  相关解决方案