9月13日 分布式缓存 周四

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/helloworld_1996/article/details/82710044
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;


public class Test1 {

    public static void main(String[] args) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, IOException {
        HSSFWorkbook wb = new HSSFWorkbook();//创建文档对象
        HSSFSheet sheet = wb.createSheet();//创建表空间
        HSSFRow row = sheet.createRow(0);//创建行

        String[] title = {"学院编号","姓名","薪资"};//首行

        for (int i = 0; i < title.length; i++) {
            HSSFCell createCell = row.createCell(i);//第0行的列
            createCell.setCellValue(title[i]);//给列赋值
        }

        ArrayList<Student> arrayList = new ArrayList<Student>();//创建一个集合
        for (int i = 0; i < 50; i++) {//创建对象放到集合中
            Student student = new Student(i+1,"张三"+i,100+i);
            arrayList.add(student);
        }

        for (int i = 0; i < arrayList.size(); i++) {
            HSSFRow createRow = sheet.createRow(i+1);//创建新的一行
            Student student = arrayList.get(i);//通过循环取到Student对象
            Class<? extends Student> class1 = student.getClass();//反射
            Field[] declaredFields = class1.getDeclaredFields();//获取方法数组
            for (int j = 0; j < declaredFields.length; j++) {//遍历这个数组
                String name = declaredFields[j].getName();//取到成员变量的名称
                String methodName = "get"+name.substring(0,1).toUpperCase()+name.substring(1);//修改名称
                Method method = class1.getMethod(methodName);//获取方法名
                Object invoke = method.invoke(student);//通过对象调用方法
                HSSFCell createCell = createRow.createCell(j);
                createCell.setCellValue(invoke+"");
            }
        }
        FileOutputStream fos = new FileOutputStream(new File("D:\\b.xls"));
        wb.write(fos);
        fos.close();

    }
}

愿你胸中有丘壑,眼里存山河。
愿你炼出钻石心,依然芙蓉面。
愿你心中有花,手里有剑。
有慈悲心肠,有金刚手段。
《你才是自己的过来人》
——李爱玲

猜你喜欢

转载自blog.csdn.net/helloworld_1996/article/details/82710044