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<titles.length;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<dataLists.size();i++) {
            HSSFRow row = sheet.createRow(i+1);
            Class clazz = dataLists.get(i).getClass();
            Field fields[] = clazz.getDeclaredFields();
            for(int j=0;j<fields.length;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<User> users = new ArrayList<User>();
        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类皆在不同包内。请自行建包并写入相应的类。

运行效果:

这里写图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值