利用JXLS根据模板导出Excel实例教程

看了几天了,感觉迷迷糊糊的,今天终于搞出来了。

先做模板,做成想要的格式样子保存,然后通过程序根据模板生成对应样式的Excel文件,代码简单。什么连接数据库查询然后将结果生成Excel文件就不讲了,放入List里面,然后套一下就行了,照老虎花猫。

准备:

1,相关jar包:

2,模板文件 :


开始,

1、 先实体类:Staff.java

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package myjxls;  
  2. /** 
  3.  * 2014-3-17 
  4.  * 8dou 
  5.  * 实体 
  6.  */  
  7. public class Staff {  
  8.        
  9.         /** 
  10.          * 名称 
  11.          */  
  12.         private String name;  
  13.        
  14.         /** 
  15.          * 薪资 
  16.          */  
  17.         private Double payment;  
  18.        
  19.         /** 
  20.          * 年终奖 
  21.          */  
  22.         private Double bonus;  
  23.        
  24.         public String getName() {  
  25.             return name;  
  26.         }  
  27.        
  28.         public void setName(String name) {  
  29.             this.name = name;  
  30.         }  
  31.        
  32.         public Double getPayment() {  
  33.             return payment;  
  34.         }  
  35.        
  36.         public void setPayment(Double payment) {  
  37.             this.payment = payment;  
  38.         }  
  39.        
  40.         public Double getBonus() {  
  41.             return bonus;  
  42.         }  
  43.        
  44.         public void setBonus(Double bonus) {  
  45.             this.bonus = bonus;  
  46.         }  
  47.         public Staff(String name, Double payment, Double bonus) {  
  48.             super();  
  49.             this.name = name;  
  50.             this.payment = payment;  
  51.             this.bonus = bonus;  
  52.         }  
  53. }  

2、测试类 ChartTest.java 

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package myjxls;  
  2. /** 
  3.  * 2014-3-17 
  4.  * 8dou 
  5.  * 测试JXLS根据模板样式导出Excel 
  6.  */  
  7. import java.util.ArrayList;  
  8. import java.util.HashMap;  
  9. import java.util.List;  
  10. import java.util.Map;  
  11.    
  12. import net.sf.jxls.transformer.XLSTransformer;  
  13. public class ChartTest {  
  14.    
  15.     /** 
  16.      * @param args 
  17.      */  
  18.     public static void main(String[] args) throws Exception {  
  19.         List<Staff> staffs = new ArrayList<Staff>();  
  20.            
  21.         Staff s1 = new Staff("张三", 6000D, 3000D);  
  22.         staffs.add(s1);  
  23.            
  24.         Staff s2 = new Staff("李四", 5000D, 2000D);  
  25.         staffs.add(s2);  
  26.            
  27.         Staff s3 = new Staff("王五", 4000D, 1000D);  
  28.         staffs.add(s3);  
  29.            
  30.         String srcFilePath = "e:/simple.xlsx";  
  31.         String destFilePath = "e:/template-simple.xlsx";  
  32.         Map<String, List<Staff>> beanParams = new HashMap<String, List<Staff>>();  
  33.         beanParams.put("staffs", staffs);  
  34.            
  35.         XLSTransformer former = new XLSTransformer();  
  36.         former.transformXLS(srcFilePath, beanParams, destFilePath);  
  37.           
  38.         System.out.println("the end !!!");  
  39.     }  
  40.    
  41. }  

运行结束后看生成的Excel文件,template-simple.xlsx


如果是Web,需要下载可以看

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. // 下载  
  2. public static void doDownLoad(String path, String name,  
  3.         HttpServletResponse response) {  
  4.     try {  
  5.         response.reset();  
  6.         response.setHeader("Content-disposition",  
  7.                 "attachment;success=true;filename ="  
  8.                         + URLEncoder.encode(name, "utf-8"));  
  9.         BufferedInputStream bis = null;  
  10.         BufferedOutputStream bos = null;  
  11.         OutputStream fos = null;  
  12.         InputStream fis = null;  
  13.         File uploadFile = new File(path);  
  14.         fis = new FileInputStream(uploadFile);  
  15.         bis = new BufferedInputStream(fis);  
  16.         fos = response.getOutputStream();  
  17.         bos = new BufferedOutputStream(fos);  
  18.         // 弹出下载对话框  
  19.         int bytesRead = 0;  
  20.         byte[] buffer = new byte[8192];  
  21.         while ((bytesRead = bis.read(buffer, 08192)) != -1) {  
  22.             bos.write(buffer, 0, bytesRead);  
  23.         }  
  24.         bos.flush();  
  25.         fis.close();  
  26.         bis.close();  
  27.         fos.close();  
  28.         bos.close();  
  29.     } catch (Exception e) {  
  30.         e.printStackTrace();  
  31.     }  
  32. }  

最后补充下Excel知识:在单元格里面将日期和时间显示在同一个单元格里面,自定义单元格式→yyyy-m-d hh:mm:ss


  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值