Java导出Excel的工具类及其Demo

所需jar:

poi.3.16.jar

==ExcelUtils==

import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
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.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor.HSSFColorPredefined;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;


public class ExcelUtils {

    public static void createExcel(List dataLists,String[] titles,OutputStream out){
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet();
        HSSFCellStyle titleCellStyle = workbook.createCellStyle();

        //设置单元标题样式
        titleCellStyle.setAlignment(HorizontalAlignment.CENTER);
        titleCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        titleCellStyle.setFillForegroundColor(HSSFColorPredefined.SKY_BLUE.getIndex());
        titleCellStyle.setWrapText(true);
        //设置单元标题字体
        HSSFFont titleFont = workbook.createFont();
        titleFont.setFontHeightInPoints((short)13);
        titleCellStyle.setFont(titleFont);

        //填写标题
        HSSFRow headRow = sheet.createRow(0);
        HSSFCell headerCell = null;
        for(int i=0;i
            headerCell = headRow.createCell(i);
            headerCell.setCellStyle(titleCellStyle);
            headerCell.setCellValue(titles[i]);
            sheet.setColumnWidth(i, (30*160));
        }

        //设置表格内容单元样式
        HSSFCellStyle valueCellStyle = workbook.createCellStyle();
        valueCellStyle.setAlignment(HorizontalAlignment.CENTER);
        valueCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);

        HSSFFont cellFont = workbook.createFont();
        cellFont.setFontHeightInPoints((short)12);
        valueCellStyle.setFont(cellFont);

        for(int i=0;i
            HSSFRow row = sheet.createRow(i+1);
            Class clazz = dataLists.get(i).getClass();
            Field fields[] = clazz.getDeclaredFields();
            for(int j=0;j
                String fieldName = fields[j].getName();
                fieldName = fieldName.substring(0,1).toUpperCase()+fieldName.substring(1);
                //执行get方法,获取属性
                Method gMethod;
                try {
                    gMethod = clazz.getMethod("get"+fieldName);
                    String value = gMethod.invoke(dataLists.get(i)).toString();
                    if(value==null) {
                        value = "";
                    }
                    HSSFCell valueCell = row.createCell(j);
                    valueCell.setCellStyle(valueCellStyle);
                    valueCell.setCellValue(value);
                } catch (NoSuchMethodException e) {
                    e.printStackTrace();

                } catch (SecurityException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }

            }
        }


        try {
            workbook.write(out);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //导出成功!

    }
}


==User类==

public class User {
    private int id;
    private String name;
    private String password;

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }


}

==Demo==

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;

import model.User;
import tt.util.ExcelUtils;

public class ExcelTest {
    public static void main(String[] args) {
        User user = new User();
        user.setId(1);
        user.setName("chen");
        user.setPassword("123456");
        List users = new ArrayList();
        users.add(user);
        try {
            String titles[] = {"学号","姓名","密码"};
            //输出到的文件
            FileOutputStream fout = new FileOutputStream("F:\\test.xls");
            //如若在servlet中返回excel则填写 ExcelUtils.createExcel(users,titles,response.getOutputStream());
            //并设置contentType为 application/vnd.ms-excel
            ExcelUtils.createExcel(users, titles, fout);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

    }
}

注:程序中所出现的ExcelUtil,ExcelTest,User类皆在不同包内。请自行建包并写入相应的类。

运行效果:

Java导出Excel的工具类及其Demo_第1张图片

你可能感兴趣的:(java,反射)