博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
XLSTransformer生成excel文件简单演示样例
阅读量:4630 次
发布时间:2019-06-09

本文共 2176 字,大约阅读时间需要 7 分钟。

项目结构图:

项目中所用到的jar,能够到下载

ExcelUtil类源代码:

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, List
list, String resultFileName){ //创建XLSTransformer对象 XLSTransformer transformer = new XLSTransformer(); //获取java项目编译后根路径 URL url = this.getClass().getClassLoader().getResource(""); //得到模板文件路径 String srcFilePath = url.getPath() + templateFileName; Map
beanParams = new HashMap
(); beanParams.put("list", list); String destFilePath = url.getPath() + resultFileName; try { //生成Excel文件 transformer.transformXLS(srcFilePath, beanParams, destFilePath); } catch (ParsePropertyException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }}
Test类源代码:

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
list = new ArrayList
(); list.add(new Fruit("苹果",2.01f)); list.add(new Fruit("桔子",2.05f)); String templateFileName = "template/template.xls"; String resultFileName = "result/fruit.xls"; new ExcelUtil().createExcel(templateFileName,list,resultFileName); }}
template.xls模板文件截图:

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

Fruit类源代码:

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文件截图:

转载于:https://www.cnblogs.com/zfyouxi/p/4191059.html

你可能感兴趣的文章
ASP.NET将原始图片按照指定尺寸等比例缩放显示图片
查看>>
测试用例设计方法基础理论知识
查看>>
基于visual Studio2013解决面试题之0804复杂链表
查看>>
find_in_set
查看>>
【转帖】SQLServer登录连接失败(error:40-无法打开到SQLServer的连接)的解决方案...
查看>>
ibatis的there is no statement named xxx in this SqlMap
查看>>
系统启动时,spring配置文件解析失败,报”cvc-elt.1: 找不到元素 'beans' 的声明“异常...
查看>>
《Python学习手册》读书笔记
查看>>
简单数据结构(队列 栈 树 堆 )
查看>>
洛谷P2380 狗哥采矿
查看>>
zTree实现节点修改的实时刷新
查看>>
MVC 总结
查看>>
Ubuntu 14.04系统托盘图标问题,skype托盘图标显示
查看>>
learning to openstack concept
查看>>
BasicModal - 简单易用的现代 Web App 弹窗
查看>>
免费下载高质量素材资源的28个网站推荐
查看>>
junit、hamcrest、eclemma的安装与使用
查看>>
自动轮播精简版
查看>>
[置顶] 单例模式lua实现
查看>>
如何在VMware虚拟机上安装Linux操作系统(Ubuntu)
查看>>