导出数据库任意表数据为EXCEL

public class ExportAsExcel {
    final Logger logger = LoggerFactory.getLogger(getClass());
    static SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    /**
     * 公共组件,导出Excel表格数据方法<p>
     * 使用方法: 数据为List<Object>类型,注意当中object的属性理论上必须为java基本数据类型
     * 当然也支持像日期这种格式,即完整支持数据库中字段类型
     * @author pery
     * @param className     数据模型Object的类完整名称
     *                      ,eg:com.swust.kelab.Department
     * @param propertyName  数据模型类的属性对应的列名,
     *                       如“depaId”对应列表“机构id“,
     *                       未指定列名的属性不会被导出
     * @param tableName     导出的excel中的表名称
     * @param record        导出的文件绝对路径(必须为存在的文件,在本方法中没有做文件路径检查)
     * @param objectList    要导出的数据集
     * @return              成功返回true,失败返回false;
     */ http://www.huiyi8.com/moban/   网站模板
    public static boolean exportExcel(String className,Map propertyName,String tableName,File record,List<? extends Object> objectList){
        HSSFWorkbook wbook = new HSSFWorkbook();
        HSSFSheet wsheet = wbook.createSheet(tableName); //表名
        HSSFCellStyle cellStyle=getStyle(wbook);
        // 设置Excel表头
        HSSFRow excelTitle = wsheet.createRow(0);
        excelTitle.setHeightInPoints(22);
        if(objectList.size()<=0){
            wsheet.autoSizeColumn(0);
            HSSFCell titleCell = excelTitle.createCell(0);
            titleCell.setCellValue("对不起,没有可用数据导出");
            titleCell.setCellStyle(cellStyle);
        }
        Class c = null;
        try {
            c = Class.forName(className);
        } 
        catch (ClassNotFoundException e1) {
            System.out.println("穿入反射用到的类名字有误");
            e1.printStackTrace();
            return false;
        }
        java.lang.reflect.Field[] flds = c.getDeclaredFields();
        int j=0;
        for (int i = 0; i < flds.length; i++) {
            String columName=flds[i].getName();
            if(propertyName.containsKey(columName)){
                wsheet.setColumnWidth(j+1, (int)(100*35.7));
                HSSFCell titleCell = excelTitle.createCell(j++);
                titleCell.setCellValue((String)propertyName.get(columName));
                titleCell.setCellStyle(cellStyle);
            }
        }
        for (int i = 0; i < objectList.size(); i++) {
            Object obj = objectList.get(i);
            HSSFRow row = wsheet.createRow(i + 1);
            int k=0;
            for (j = 0; j < flds.length; j++) {
                String columName=flds[j].getName();
                if(!propertyName.containsKey(columName)){
                    continue;
                }    
                HSSFCell hssfCell = row.createCell(k++);
                try {
                    flds[j].setAccessible(true);
                    Object t = flds[j].get(obj);
                    if(t instanceof Date){
                        t = dateFormat.format(t);
                    }
                    if(t!=null){
                        hssfCell.setCellValue(String.valueOf(t));
                    }
                } catch (IllegalArgumentException e) {
                    System.out.println("该字段不是基本数据格式字段");
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    System.out.println("非法访问");
                    e.printStackTrace();
        try {
            FileOutputStream fOut = new FileOutputStream(record);
            wbook.write(fOut);
            fOut.flush();
            fOut.close();
        } catch (FileNotFoundException e) {
            System.out.println("文件路径错误");
            e.printStackTrace();
        } catch (IOException e) {
            System.out.println("I/O错误");
            e.printStackTrace();
        }
        return true;
 * Excel表格格式属性调整
 * @param wbook
 * @return
 */
    private static HSSFCellStyle getStyle(HSSFWorkbook wbook){
        HSSFCellStyle cellStyle = wbook.createCellStyle();
        cellStyle.setBorderLeft(HSSFCellStyle.BORDER_DOUBLE);
        cellStyle.setBorderRight(HSSFCellStyle.BORDER_DOUBLE);
        cellStyle.setBorderTop(HSSFCellStyle.BORDER_DOUBLE);
        cellStyle.setBorderBottom(HSSFCellStyle.BORDER_DOUBLE);
        HSSFFont font = wbook.createFont();
        font.setFontHeightInPoints((short) 12);
        font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        cellStyle.setFont(font);
        return cellStyle;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值