使用Java Annotation写Excel 工具类

使用Java Annotation写Excel 工具类

 

 

Java代码   收藏代码
  1. package com.excel.export.util;  
  2.   
  3. import java.lang.annotation.ElementType;  
  4. import java.lang.annotation.Retention;  
  5. import java.lang.annotation.RetentionPolicy;  
  6. import java.lang.annotation.Target;  
  7.   
  8. /** 
  9.  *  
  10.  * <p>导出excel 工具包</p> 
  11.  * @author liuqing 
  12.  * @version 1.0 
  13.  * @see 导出excel 注解 
  14.  * 
  15.  */  
  16. @Retention(value=RetentionPolicy.RUNTIME)  
  17. @Target(value=ElementType.FIELD)  
  18. public @interface AllowExcel {  
  19.   
  20.     boolean value() default true;  
  21.       
  22.     String name();  
  23.       
  24. }  
 

 

写实现类

 

 

Java代码   收藏代码
  1. package com.excel.export.util;  
  2.   
  3. import java.io.Serializable;  
  4. import java.util.Date;  
  5.   
  6. /** 
  7.  *  
  8.  * @author liuqing 
  9.  * @version 1.0 
  10.  * @datetime 2011-11-24 
  11.  * @see 学生测试对象 
  12.  * 
  13.  */  
  14. public class Student implements Serializable {  
  15.       
  16.     private static final long serialVersionUID = -8141916472359874289L;  
  17.   
  18.     @AllowExcel(name="编号")  
  19.     private Integer id;  
  20.       
  21.     @AllowExcel(name="姓名")  
  22.     private String name;  
  23.       
  24.     @AllowExcel(name="生日")  
  25.     private Date date;  
  26.   
  27.     public Integer getId() {  
  28.         return id;  
  29.     }  
  30.   
  31.     public void setId(Integer id) {  
  32.         this.id = id;  
  33.     }  
  34.   
  35.     public String getName() {  
  36.         return name;  
  37.     }  
  38.   
  39.     public void setName(String name) {  
  40.         this.name = name;  
  41.     }  
  42.   
  43.     public Date getDate() {  
  44.         return date;  
  45.     }  
  46.   
  47.     public void setDate(Date date) {  
  48.         this.date = date;  
  49.     }  
  50.   
  51. }  
 

 

POI 实现类

 

 

Java代码   收藏代码
  1. package com.excel.export.util;  
  2.   
  3. import java.io.FileOutputStream;  
  4. import java.io.IOException;  
  5. import java.io.Serializable;  
  6. import java.lang.reflect.Field;  
  7. import java.util.ArrayList;  
  8. import java.util.Collection;  
  9. import java.util.Date;  
  10. import java.util.List;  
  11.   
  12. import org.apache.poi.hssf.usermodel.HSSFCell;  
  13. import org.apache.poi.hssf.usermodel.HSSFRow;  
  14. import org.apache.poi.hssf.usermodel.HSSFSheet;  
  15. import org.apache.poi.hssf.usermodel.HSSFWorkbook;  
  16.   
  17. /** 
  18.  *  
  19.  * @author liuqing 
  20.  * @see excel 导出工具类 
  21.  * @version 1.0 
  22.  * @datetime 2011-11-24 
  23.  * 
  24.  */  
  25. public class ExportExcel<T extends Serializable> {  
  26.       
  27.     private HSSFWorkbook hw = new HSSFWorkbook();  
  28.       
  29.     /** 
  30.      * 生成sheet 名称 
  31.      */  
  32.     private String sheetName = "shtteName";  
  33.       
  34.     private boolean showHeader = true;  
  35.       
  36.     /** 
  37.      * 缓存成员变量 
  38.      */  
  39.     private List<String> fieldNameCaches = new ArrayList<String>();  
  40.       
  41.       
  42.     public void createHeader(Collection<T> data) throws IllegalAccessException {  
  43.         //Sheet 创建工作表  
  44.         HSSFSheet sheet = hw.createSheet(sheetName);  
  45.           
  46.         int j = 0;  
  47.         for (T t:data) {  
  48.             Field[] fields = t.getClass().getDeclaredFields();  
  49.             /** 
  50.              * 加入允许字段缓存数据 
  51.              * if == 0时表示要添加缓存数据 
  52.              */  
  53.             if (j == 0) {  
  54.                 HSSFRow headRow = null;  
  55.                 if (this.showHeader) {  
  56.                     headRow = sheet.createRow(0);  
  57.                 }  
  58.                 int i = 0;  
  59.                 for (Field field:fields) {  
  60.                     //判断Excel 安全允许注解  
  61.                     AllowExcel allowExcel = field.getAnnotation(AllowExcel.class);  
  62.                     if (allowExcel != null && allowExcel.value()) {  
  63.                         //显示关部信息  
  64.                         if (this.showHeader) {  
  65.                             HSSFCell cell = headRow.createCell(i);  
  66.                             cell.setCellValue(allowExcel.name());  
  67.                         }  
  68.                         this.fieldNameCaches.add(field.getName());  
  69.                     }  
  70.                     System.out.println(allowExcel.name());  
  71.                     i++;  
  72.                 }  
  73.             }  
  74.             j++;  
  75.               
  76.             //创建产生行数据  
  77.             HSSFRow hssfRow = sheet.createRow(j);  
  78.             this.setCellValueToRow(t, hssfRow);  
  79.         }  
  80.     }  
  81.       
  82.     /** 
  83.      * 输出Excel Row 信息 
  84.      * @param t T extends Serializable 
  85.      * @param hssfRow HSSFRow 
  86.      * @return HSSFRow  
  87.      * @throws IllegalAccessException 
  88.      */  
  89.     public HSSFRow setCellValueToRow(T t,HSSFRow hssfRow) throws IllegalAccessException {  
  90.         Field fields[] = t.getClass().getDeclaredFields();  
  91.         //定义Excel 输出行数  
  92.         int i = 0;  
  93.         for (Field field:fields) {  
  94.             //缓存中是否存在允许字段  
  95.             if (this.isCacheFiledName(field.getName())) {  
  96.                 HSSFCell cell = hssfRow.createCell(i);  
  97.                 i++;  
  98.                 field.setAccessible(true);  
  99.                 Object obj = field.get(t);  
  100.                 //类型转换  
  101.                 if (obj instanceof Integer ) {  
  102.                     cell.setCellValue((Integer)obj);  
  103.                 }  
  104.                 else if (obj instanceof String) {  
  105.                     cell.setCellValue((String)obj);  
  106.                 }  
  107.                 else if (obj instanceof Date) {  
  108.                     cell.setCellValue((Date)obj);  
  109.                 }  
  110.                 else if (obj instanceof Double) {  
  111.                     cell.setCellValue((Double)obj);  
  112.                 }  
  113.                 else if (obj instanceof Boolean) {  
  114.                     cell.setCellValue((Boolean)obj);  
  115.                 }  
  116.                 else if (obj instanceof Float) {  
  117.                     cell.setCellValue((Float)obj);  
  118.                 }  
  119.                 else if (obj instanceof Long) {  
  120.                     cell.setCellValue((Long)obj);  
  121.                 }  
  122.                 else {  
  123.                     throw new TypeNotPresentException("类型不支持"null);  
  124.                 }  
  125.                   
  126.             }  
  127.         }  
  128.         return hssfRow;  
  129.     }  
  130.       
  131.     /** 
  132.      * 判断Cache 是否有对应的FiledName 
  133.      * @param fieldName String 
  134.      * @return boolean 
  135.      */  
  136.     public boolean isCacheFiledName(String fieldName) {  
  137.         if (fieldName == null) {  
  138.             return false;  
  139.         }  
  140.         for (String fieldNameCache:this.fieldNameCaches) {  
  141.             if (fieldName.equals(fieldNameCache)) {  
  142.                 return true;  
  143.             }  
  144.         }  
  145.         return false;  
  146.     }  
  147.       
  148.     public static void main(String args[]) throws IllegalAccessException {  
  149.         ExportExcel<Student> export = new ExportExcel<Student>();  
  150.         List<Student> list = new ArrayList<Student>();  
  151.         Student stu = new Student();  
  152.         stu.setId(123);  
  153.         stu.setDate(new Date());  
  154.         stu.setName("liquing");  
  155.         Student stu1 = new Student();  
  156.         stu1.setId(1231);  
  157.         stu1.setDate(new Date());  
  158.         stu1.setName("liquing1");  
  159.         Student stu2 = new Student();  
  160.         stu2.setId(1231);  
  161.         stu2.setDate(new Date());  
  162.         stu2.setName("liquing1");  
  163.         list.add(stu);  
  164.         list.add(stu1);  
  165.         list.add(stu2);  
  166.         export.createHeader(list);  
  167.         FileOutputStream out = null;  
  168.         try {  
  169.             out = new FileOutputStream("d:\\um.xls");  
  170.             export.hw.write(out);  
  171.         } catch (Exception e) {  
  172.             // TODO: handle exception  
  173.         }  
  174.         finally {  
  175.             if (out != null) {  
  176.                 try {  
  177.                     out.close();  
  178.                 }   
  179.                 catch (IOException e) {  
  180.                     // TODO Auto-generated catch block  
  181.                     e.printStackTrace();  
  182.                 }  
  183.             }  
  184.         }  
  185.           
  186.     }  
  187.       
  188.     /** 
  189.      * 查找对应的类自定义方法 
  190.      * @param methodName 
  191.      * @return boolean 
  192.      */  
  193.     public boolean isClassMethod(String methodName) {  
  194.         if (methodName != null) {  
  195.             if ("getClass".equals(methodName)) {  
  196.                 return false;  
  197.             }  
  198.             if (methodName.startsWith("get")   
  199.                     || methodName.startsWith("is")  
  200.                     || methodName.startsWith("set")) {  
  201.                 return true;  
  202.             }  
  203.             return false;  
  204.         }  
  205.         return false;  
  206.     }  
  207.   

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值