日期:2014-05-20 浏览次数:21023 次
public class Test {
public static void main(String[] args) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException {
List<Model> list = new ArrayList<Model>();
for (int i = 0; i < 10; i++) {
list.add(new Model("model"+i));
}
Method[] methods = Model.class.getMethods();
for (Model model : list) {
for (Method method : methods) {
if(method.getName().equalsIgnoreCase("getName")){
System.out.println(method.invoke(model));
}
}
}
}
}
class Model{
private String name;
public Model(){
}
public Model(String name){
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
public class A {
private String id;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public A(String s){
this.id = s;
}
public static void main(String[] args) {
//反射获取方法
Method[] ms = A.class.getDeclaredMethods();//
for(Method m:ms){
System.out.println(m.getName());
}
//反射获取属性
Field[] fs = A.class.getDeclaredFields();
for(Field f : fs){
System.out.println(f.getName());
}
//反射获取构造函数
Constructor[] cs = A.class.getConstructors();
for(Constructor c : cs){
System.out.println(c.getName());
}
//先初始化
List<A> l = init();
//反射获取属性的值
for(A a : l){
for(Field f : a.getClass().getDeclaredFields()){
try {
System.out.println(f.getName()+":"+f.get(a));//可以看做在此写入Excel
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public static List<A> init(){
List<A> l = new ArrayList<A>();
for(int i = 0;i<100;i++){
l.add(new A(i+""));
}
return l;
}
}
package com.poi.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface ExcelHeaderTitle {
String name();
int index();
int colSpan() default 1;
boolean export();
}
package com.poi.export;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.RegionUtil;
import com.poi.annotation.ExcelHeaderTitle;
import com.poi.bean.TestBean;
public class ExcelExporter {
public static void main(String[] args) throws Exception {
FileOutputStream out = new FileOutputStream("G:\\test.xls");
ExcelExporter ex = new ExcelExporter();
List<TestBean> testBeans = new ArrayList<TestBean>();