POI+反射实现数据库转为EXCEL

来源:互联网 发布:php 面向对象的语言 编辑:程序博客网 时间:2024/06/05 06:59

关于POI 的我就不再赘述了自己可以去官网看看。

下面我们就直接开始把:

首先我们需要导入需要的jar包:文档结构很简单


首先pojo的对象的建立student:

package com.bean;public class Student {private Integer sId;private String userName;private Integer userAge;public Student() {}public Student(Integer sId, String userName, Integer userAge) {this.sId = sId;this.userName = userName;this.userAge = userAge;}public Integer getsId() {return sId;}public void setsId(Integer sId) {this.sId = sId;}public String getUserName() {return userName;}public void setUserName(String userName) {this.userName = userName;}public Integer getUserAge() {return userAge;}public void setUserAge(Integer userAge) {this.userAge = userAge;}}
下面就是主要的代码实现了
copyPoi:

package com.bean;import java.io.FileOutputStream;import java.lang.reflect.Method;import java.util.List;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;import org.apache.poi.hssf.util.HSSFColor;import org.apache.poi.ss.usermodel.CellStyle;import org.apache.poi.ss.usermodel.Font;/**实现对数据库的数据进行excell转化的操作 *  * @author 董政 * */public class CopyPoi {    /**     * 主要操作方法     * @param list  数据库查询得到的集合     * @param clazz  操作对象的泛型     */static <T> void poiToExcell(List<T> list,Class<T> clazz){//首先拿到流FileOutputStream out=null;//声明泛型T t=null;//实例化文档对象HSSFWorkbook book=new HSSFWorkbook();try {//实例化流out=new FileOutputStream("first.xls");//创建工作簿    HSSFSheet sheet=book.createSheet();    //创建标题行    HSSFRow title=sheet.createRow(0);    //通过反射拿到数据库的字段名    String[] array= reflactClass(clazz);    //创建标题样式    CellStyle cellStyle=book.createCellStyle();    //创建颜色    Font headfont=book.createFont();    //设置参数    headfont.setBoldweight(Font.COLOR_RED);    cellStyle.setFont(headfont);    cellStyle.setFillBackgroundColor(new HSSFColor.RED().getIndex());    //循环设置标题行    for (int i = 0; i < array.length; i++) {    //创建单元格    HSSFCell cell=title.createCell(i);    //设置内容cell.setCellValue(array[i]);//设置样式cell.setCellStyle(cellStyle);}    //遍历数据库集合设置行    for (int i = 0; i < list.size(); i++) {    //创建行     HSSFRow row= sheet.createRow(i+1);    //设置行内容    readRow(row, list.get(i),clazz);}   //写入数据    book.write(out);    //关闭流    out.close();} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}/** * 写入每一行内容 * @param hssfRow 传入的行 * @param t  数据 * @param clazz 泛型 */static <T> void readRow(HSSFRow hssfRow,T t,Class<T> clazz){try {//得到数据列String[] method=reflactClass(t.getClass());//循环写数据for (int i = 0; i < method.length; i++) {//创建单元格HSSFCell cell= hssfRow.createCell(i);//通过反射得到方法Method ms=t.getClass().getDeclaredMethod("get"+method[i]);//反射方法得到数据Object object=ms.invoke(t);//填充表格cell.setCellValue(object.toString());}} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}/** * 得到数据列 * @param clazz 泛型 * @return */static <T> String[] reflactClass(Class<T> clazz){//反射得到方法Method[] ms=clazz.getDeclaredMethods();//重建数据保存字段名String[] methods=new String[ms.length/2];//定义字段数标签 int j=0; //循环遍历for (int i = 0; i < ms.length; i++) {//判断是不是get方法if (ms[i].getName().startsWith("get")) {//取字段名methods[j]=ms[i].getName().substring(3);//字段数自加1j++;}}  return methods;}}
下面我们测试一下:

package com.bean;import java.util.ArrayList;import java.util.List;public class Test {public static void main(String[] args) {List<Student> list=new ArrayList<>();list.add(new Student(1,"name1",1));list.add(new Student(1,"name2",1));list.add(new Student(1,"name3",1));list.add(new Student(1,"name4",1));list.add(new Student(1,"name5",1));CopyPoi.poiToExcell(list, Student.class);}}
结束,来看看结果吧:



想要更复杂的单元格样式自己加吧。下一章我会写一个读取到数据库的demo。


原创粉丝点击