XLSTransformer生成excel文件简单示例

项目结构图:

项目中所用到的jar,可以到http://www.findjar.com/index.x下载

 

ExcelUtil类源码:

 

[java]  view plain copy
  1. package util;  
  2.   
  3. import java.io.IOException;  
  4. import java.net.URL;  
  5. import java.util.HashMap;  
  6. import java.util.List;  
  7. import java.util.Map;  
  8.   
  9. import net.sf.jxls.exception.ParsePropertyException;  
  10. import net.sf.jxls.transformer.XLSTransformer;  
  11. /** 
  12.  * Excel生成类. 
  13.  */  
  14. public class ExcelUtil {  
  15.     /** 
  16.      * 根据模板生成Excel文件. 
  17.      * @param templateFileName 模板文件. 
  18.      * @param list 模板中存放的数据. 
  19.      * @param resultFileName 生成的文件. 
  20.      */  
  21.     public void createExcel(String templateFileName, List<?> list, String resultFileName){  
  22.         //创建XLSTransformer对象  
  23.         XLSTransformer transformer = new XLSTransformer();  
  24.         //获取java项目编译后根路径  
  25.         URL url = this.getClass().getClassLoader().getResource("");  
  26.         //得到模板文件路径  
  27.         String srcFilePath = url.getPath() + templateFileName;  
  28.         Map<String,Object> beanParams = new HashMap<String,Object>();  
  29.         beanParams.put("list", list);  
  30.         String destFilePath = url.getPath() + resultFileName;  
  31.         try {  
  32.             //生成Excel文件  
  33.             transformer.transformXLS(srcFilePath, beanParams, destFilePath);  
  34.         } catch (ParsePropertyException e) {  
  35.             e.printStackTrace();  
  36.         } catch (IOException e) {  
  37.             e.printStackTrace();  
  38.         }  
  39.     }  
  40.   
  41. }  

Test类源码:

 

 

[java]  view plain copy
  1. package test;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import po.Fruit;  
  7. import util.ExcelUtil;  
  8. /** 
  9.  * 测试类. 
  10.  */  
  11. public class Test {  
  12.   
  13.     public static void main(String[] args) {  
  14.         List<Fruit> list = new ArrayList<Fruit>();  
  15.         list.add(new Fruit("苹果",2.01f));  
  16.         list.add(new Fruit("桔子",2.05f));  
  17.         String templateFileName = "template/template.xls";  
  18.         String resultFileName = "result/fruit.xls";  
  19.         new ExcelUtil().createExcel(templateFileName,list,resultFileName);  
  20.   
  21.     }  
  22.   
  23. }  

template.xls模板文件截图:

 

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


Fruit类源码:

 

[java]  view plain copy
  1. package po;  
  2. /** 
  3.  * 水果. 
  4.  */  
  5. public class Fruit {  
  6.     /** 
  7.      * 水果名称. 
  8.      */  
  9.     private String name;  
  10.     /** 
  11.      * 水果价格. 
  12.      */  
  13.     private float price;  
  14.       
  15.       
  16.     public Fruit() {  
  17.         super();  
  18.     }  
  19.       
  20.     public Fruit(String name, float price) {  
  21.         super();  
  22.         this.name = name;  
  23.         this.price = price;  
  24.     }  
  25.     public String getName() {  
  26.         return name;  
  27.     }  
  28.     public void setName(String name) {  
  29.         this.name = name;  
  30.     }  
  31.     public float getPrice() {  
  32.         return price;  
  33.     }  
  34.     public void setPrice(float price) {  
  35.         this.price = price;  
  36.     }  
  37.       
  38. }  

生成fruit.xls文件截图:

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值