POI操作EXCEL--源自技术

POI简介

Apache POI是Apache软件基金会(http://baike.baidu.com/view/7044910.htm)的开放源码函式库,POI提供API给Java程序对Microsoft Office格式档案读和写的功能。

网址

Apache POI(http://poi.apache.org/)

简单实现


操作Excle

[java]  view plain  copy
  1. public class ExcelUtil {  
  2.   
  3. public static XSSFWorkbook getWorkbook(String[] titles,String[] propertys,  
  4.         List<?> datas,TransformRule transformRule){  
  5.     ExcelUtil excelUtil = new ExcelUtil();  
  6.     XSSFWorkbook workbook = new XSSFWorkbook();  
  7.     XSSFSheet sheet = workbook.createSheet();  
  8.     excelUtil.setTitle(titles, sheet);  
  9.     if(null!=datas&&datas.size()>0){  
  10.         excelUtil.setBodyRow(propertys, datas, sheet,transformRule);  
  11.     }  
  12.     return workbook;  
  13. }  
  14.       
  15. /*设置表头*/  
  16. private void setTitle(String[] titles,XSSFSheet sheet) {  
  17.     XSSFRow row = sheet.createRow(0);  
  18.     int c = 0;  
  19.     for (String title :titles) {  
  20.         XSSFCell cell = row.createCell(c);  
  21.         cell.setCellValue(title);  
  22.         c++;  
  23.     }  
  24. }  
  25.       
  26. /*填充表格*/  
  27. private void setBodyRow(String[] propertys,  
  28.         List<?> datas, XSSFSheet sheet,TransformRule transformRule){  
  29.     XSSFRow row;  
  30.     int c;  
  31.     int r = 1;  
  32.     for (Object obj : datas) {  
  33.         c = 0;  
  34.         row = sheet.createRow(r);  
  35.         for (String key : propertys) {  
  36.             Object val = getAttributeValue(obj,key,transformRule,c);  
  37.             XSSFCell cell = row.createCell(c);  
  38.             if(null!=val)  
  39.                 cell.setCellValue(val.toString());  
  40.             else  
  41.                 cell.setCellValue("");  
  42.             c++;  
  43.         }  
  44.         r++;  
  45.     }  
  46. }  
  47.       
  48. /*通过反射获取对象属性值*/  
  49. private Object getAttributeValue(Object obj,String attrName,  
  50.         TransformRule transformRule,int index){  
  51.     Class<? extends Object> cl = obj.getClass();  
  52.     String attr = String.format("get%1$s%2$s",   
  53.                       attrName.substring(01).toUpperCase(),  
  54.                       attrName.substring(1));  
  55.     try {  
  56.         Method method = cl.getDeclaredMethod(attr);  
  57.         Object val = method.invoke(obj);  
  58.         if(null==transformRule){  
  59.             return val;  
  60.         }else{  
  61.             return transformRule.form(obj,attrName, val,index);  
  62.         }  
  63.     } catch (NoSuchMethodException | SecurityException e) {  
  64.         e.printStackTrace();  
  65.     }catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {  
  66.         e.printStackTrace();  
  67.     }  
  68.     return null;  
  69. }  
  70. }  


属性值转义抽象接口:用来自定义值的转义规则

[java]  view plain  copy
  1. public abstract class TransformRule {  
  2.     /** 
  3.      * 值转义接口 
  4.      * @param obj 数据对象 
  5.      * @param attribute 属性名称 
  6.      * @param val 值 
  7.      * @param indx 列下标 
  8.      * @return 
  9.      */  
  10. public abstract String form(Object obj,String attribute,Object val,int indx);  
  11.       
  12. public Object getAttributeValue(Object obj,String attrName){  
  13.     Class<? extends Object> cl = obj.getClass();  
  14.     try {  
  15.         Method method = cl.getDeclaredMethod(attrName);  
  16.         Object val = method.invoke(obj);  
  17.         return val;  
  18.     } catch (NoSuchMethodException | SecurityException e) {  
  19.         e.printStackTrace();  
  20.     }catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {  
  21.         e.printStackTrace();  
  22.     }  
  23.     return null;  
  24. }  
  25.       
  26. public String objectToString(Object obj){  
  27.     if(null!=obj){  
  28.         return obj.toString();  
  29.     }else{  
  30.         return "";  
  31.     }  
  32. }  
  33.       
  34. }  

测试类

[java]  view plain  copy
  1. public class User {  
  2.     private Integer id;  
  3.     private String name;  
  4.     private Integer sex;  
  5.       
  6.     public User(Integer id,String name,Integer sex){  
  7.         setId(id);  
  8.         setName(name);  
  9.         setSex(sex);  
  10.     }  
  11.       
  12.     public Integer getId() {  
  13.         return id;  
  14.     }  
  15.     public void setId(Integer id) {  
  16.         this.id = id;  
  17.     }  
  18.     public String getName() {  
  19.         return name;  
  20.     }  
  21.     public void setName(String name) {  
  22.         this.name = name;  
  23.     }  
  24.     public Integer getSex() {  
  25.         return sex;  
  26.     }  
  27.     public void setSex(Integer sex) {  
  28.         this.sex = sex;  
  29.     }  
  30.   
  31.     public static void main(String[] args) {  
  32.           
  33.         List<User> datas = new ArrayList<User>();  
  34.         datas.add(new User(1"李连杰",1));  
  35.         datas.add(new User(1"成龙"1));  
  36.         datas.add(new User(1"舒淇"0));  
  37.           
  38.         /*转换规则*/  
  39.         TransformRule transformRule = new TransformRule() {  
  40.             @Override  
  41.             public String form(Object obj, String attribute, Object val,  
  42.                     int indx) {  
  43.                 if("sex".equals(attribute)){  
  44.                     if (val.equals(1)) {  
  45.                         return "男";  
  46.                     }else if (val.equals(0)) {  
  47.                         return "女";  
  48.                     }  
  49.                 }  
  50.                 return objectToString(val);  
  51.             }  
  52.         };  
  53.           
  54.         String[] propertys = {"id","name","sex"};  
  55.         String[] titles = {"演员编号","名称","性别"};  
  56.         XSSFWorkbook bodyRow = getWorkbook(titles,propertys, datas,transformRule);  
  57.         FileOutputStream out;  
  58.         try {  
  59.             out = new FileOutputStream(new File("C:/Users/Administrator/Desktop/test.xlsx"));  
  60.             bodyRow.write(out);  
  61.         } catch (FileNotFoundException e) {  
  62.             e.printStackTrace();  
  63.         } catch (IOException e) {  
  64.             e.printStackTrace();  
  65.         }  
  66.     }  
  67. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值