Java数据导出Excel

来源:互联网 发布:死去元知万事空的意思 编辑:程序博客网 时间:2024/06/06 17:18

最近在公司做项目的时候,要求实现一个将数据导出为excel的功能,于是找了找资料,最后终于整出来了,而且我把这个功能写成通用的方法,想了想还是写点东西总结一下吧:

1、首先下载一个第三方jar包:jxl.jar;

2、见源码:

        package com.org.util;
        
        import java.lang.reflect.Constructor;
        import java.lang.reflect.Field;
        import java.lang.reflect.Method;
        import java.util.List;
        
        import org.springframework.context.ApplicationContext;
        import org.springframework.context.support.ClassPathXmlApplicationContext;
        
        import com.org.dao.DeptDao;
        import com.org.entity.Dept;
        
        import jxl.format.Alignment;
        import jxl.format.Border;
        import jxl.format.BorderLineStyle;
        import jxl.format.VerticalAlignment;
        import jxl.write.Label;
        import jxl.write.WritableCellFormat;
        import jxl.write.WritableFont;
        import jxl.write.WritableSheet;
        import jxl.write.WritableWorkbook;
        
        /**
         * @author pike
         * 导出excel工具类
         */
        public class ExportExcel {
            
            
            public static <T> void exportExcel(WritableWorkbook book,List<T> list)throws Exception{
                try {
                    WritableSheet sheet=book.createSheet("第一页", 0);
                    WritableFont wfc=new WritableFont(WritableFont.ARIAL,13,WritableFont.BOLD,false);
                    WritableCellFormat wcfFC=new WritableCellFormat(wfc);
                    
                    String []fieldtitle = getFiledName(list.get(0));//获取类的所有字段
                    for (int i=0;i<fieldtitle.length;i++) {
                        sheet.addCell(new Label(i,0,fieldtitle[i],wcfFC));//向单元格中加入标题
                    }
                    
                    //写入数据
                    int c=1;//默认表示当前为第一行
                    for (int i=0;i<list.size();i++) {
                        T t = list.get(i);//得到当前的对象
                        String []fields = getFiledName(t);//获取类的所有字段
                        for (int j = 0 ;j<fields.length; j++) {
                            String value = getFieldValueByName(fields[j] , t) + "";//获取字段的值  
                            Label content = new Label(j,c,value,wcfFC);//单元格的内容
                            sheet.addCell(content);
                        }
                        c++;//到下一行
                    }
                    book.write();
                    book.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            
            
            /**
            * 获取对象属性,返回一个字符串数组      
            * @param  o 对象
            * @return String[] 字符串数组
            */  
            private static String[] getFiledName(Object o){    
                try {  
                        Field[] fields = o.getClass().getDeclaredFields();  
                        String[] fieldNames = new String[fields.length];    
                        for (int i=0; i < fields.length; i++){    
                            fieldNames[i] = fields[i].getName();    
                        }    
                        return fieldNames;  
                } catch (SecurityException e) {  
                        e.printStackTrace();  
                        System.out.println(e.toString());  
                }  
                    return null;  
            }
            
            /**
            * 使用反射根据属性名称获取属性值  
            *  
            * @param  fieldName 属性名称
            * @param  o 操作对象
            * @return Object 属性值
            */  
              
            private static Object getFieldValueByName(String fieldName, Object o){      
               try   
               {      
                   String firstLetter = fieldName.substring(0, 1).toUpperCase();      
                   String getter = "get" + firstLetter + fieldName.substring(1);      
                   Method method = o.getClass().getMethod(getter, new Class[] {});      
                   Object value = method.invoke(o, new Object[] {});      
                   return value;      
               } catch (Exception e)   
               {      
                   System.out.println("属性不存在");      
                   return null;      
               }      
            }
            
        }

我是通过java反射实现方法的通用。


0 0
原创粉丝点击