java 基于反映射导excel的公共类:。

我写了一个基与java反映射机制的公共类,原理是基于对象,所对象的属性和和标题 名称进行关联,然后通过java反映射机制得到属性的值,下面是我的代码,有二个类,一个:Test2,别一个是Person,我用到了jxl.jar。想测试的自己去下。

Test2代码:

package jxl.demo;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.Number;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

public class Test2 {
 public Test2() throws InstantiationException, IllegalAccessException, ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException, InvocationTargetException, IOException, RowsExceededException, WriteException{
  List<Person> list=new ArrayList<Person>();
  Person person=new Person();
  person.setName("马新良");
  person.setAge(20);
  person.setSex("男");
  list.add(person);
  
  person=new Person();
  person.setName("碧南佳");
  person.setAge(23);
  person.setSex("女");
  list.add(person);
  
//  System.out.println(list.get(0).getName());
//  System.out.println(list.get(1).getName());
  
  String name="ddd";
  if(Character.isLowerCase(name.charAt(0))){name=name.substring(0,1)+name.substring(1);}
  
  String refresh[][]={{"name","姓名"},{"age","年龄"},{"sex","性别"}};//定义Excel标题
  
    
  File file= new File(this.getClass().getResource("/").getPath()+"aaa.xls");
  if(!file.exists()) file.createNewFile();
  WritableWorkbook writableWorkbook= Workbook.createWorkbook(file);
  WritableSheet sheet=writableWorkbook.createSheet("模块一", 1);
  
  for(int i=0;i<refresh.length;i++){  //这个循环写Excel标题  
   Label label = new Label(i,0,refresh[i][1]);
   sheet.addCell(label);
   //System.out.println(str[0]+":"+str[1]);
  }
  for(int s=1;s<=list.size();s++){ //循环List时面的对象
   Object obj=list.get(s-1);
   Class cls=Class.forName(obj.getClass().getName());
   for(int i=0;i<refresh.length;i++){  //循环标题时面的对象属性 
    String temp=refresh[i][0];
    if(Character.isLowerCase(temp.charAt(0))) temp=temp.substring(0,1).toUpperCase()+temp.substring(1);
    Method method=cls.getDeclaredMethod("get"+temp);
    Object value=method.invoke(obj);
    if(value instanceof String) {
     Label label=new Label(i,s,value.toString());
     sheet.addCell(label);
    }else if(value instanceof Integer){
     Number number=new Number(i,s,((Integer)value).intValue());
     sheet.addCell(number);
    }
    
   }
  }
  
  
  //WritableCell cells = sheet.getWritableCell(0,0);
  Label label = new Label(0,0,"大家好");
  writableWorkbook.write();
  writableWorkbook.close();
  
  
  
//  for(Iterator<String> iter=set.iterator();iter.hasNext();){
//   String temp=iter.next();
//   temp=temp.substring(0,1).toUpperCase()+temp.substring(1);  
//   
//  }
//  
  
  // name.substring(0,1).toUpperCase()+name.substring(1);
  
//  Class cls = person.getClass();
//  Object obj=cls.newInstance();
//  Method method = cls.getDeclaredMethod("getName");
//  System.out.println(method.invoke(person));
//  //System.out.println(obj);
 }
 public static void main(String args[]) throws RowsExceededException, WriteException, IOException{
  try {
   new Test2();
  } catch (InstantiationException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IllegalAccessException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (ClassNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (SecurityException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IllegalArgumentException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (NoSuchMethodException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (InvocationTargetException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
}

 

Person代码:

package jxl.demo;

public class Person {
 private String name;
 private int age;
 private String sex;
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public int getAge() {
  return age;
 }
 public void setAge(int age) {
  this.age = age;
 }
 public String getSex() {
  return sex;
 }
 public void setSex(String sex) {
  this.sex = sex;
 }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值