POI导出功能

调用工具类

try {
   List<FinanceStats> list = financeStatsService.find(search);
   String filename = "CWBB" + DateTimeTools.get8BitDate(new Date()) + ".xlsx";             
   ExportUtil.exportExcel(filename,list,FinanceStats.class,response);
}catch(Exception ex){
   log.error(ex);
}
 
导出工具类封装了POI导出Excel的功能
package com.hqz.dto.admin;

import java.io.BufferedOutputStream;
import java.io.OutputStream;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

/**类名:ExportUtil <br/>
 * 功能:导出工具类封装了POI导出Excel的功能<br/>
 * 日期:2016年5月19日 <br/>
 */
public class ExportUtil {
    
    /*
     * 方便导出Excel的工具类
     */
    public static void exportExcel(String filename,List<?> list,Class<?> clazz,
            HttpServletResponse response)throws Exception{
        ExcelUtil excelUtil = ExcelUtil.newInstance();
        ExportProperties properties = ExcelUtil.getExcelHeaders(clazz);
        response.setHeader("Content-Disposition", "attachment;filename=\"" + filename + "\"");        
        response.setContentType("application/octet-stream;charset=UTF-8");
        OutputStream out = new BufferedOutputStream(response.getOutputStream());
        out = new BufferedOutputStream(response.getOutputStream());
        excelUtil.exportExcelV07(out, list, properties);    
        out.flush();
        out.close();
    }
}
导出Excel的POI工具类
package com.hqz.dto.admin;

import java.io.OutputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

/**类名:ExcelUtil <br/>
 * 功能:导出Excel的POI工具类<br/>
 * 日期:2016年4月13日 <br/>
 */
public class ExcelUtil{    
    
    private static ExcelUtil instance = new ExcelUtil();    
    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");    
    private ExcelUtil(){}        
    
    public static ExcelUtil newInstance(){    
        return instance;
    }    
    
    /*
     * 导出Excel2003
     */
    public void exportExcelV03(OutputStream out, List<?> rows, ExportProperties properties)
            throws Exception{        
        Workbook wbk = exportExcel(rows, properties, false);
        wbk.write(out);
    }
    
    /*
     * 导出Excel2007
     */
    public void exportExcelV07(OutputStream out, List<?> rows, ExportProperties properties)
            throws Exception{        
        Workbook wbk = exportExcel(rows, properties, true);
        wbk.write(out);
    }    
    
    private Workbook exportExcel(List<?> rows, ExportProperties properties, boolean isXSSF) 
            throws Exception{                
        Workbook wbk = null;
        if(isXSSF){        
            wbk = new XSSFWorkbook();
        }else{    
            wbk = new HSSFWorkbook();
        }
        Sheet sheet = wbk.createSheet();        
        Row row = sheet.createRow(0);            
        List<ExcelHeader> headers = properties.getProperties();
        for(int i = 0; i < headers.size(); i++){        
            ExcelHeader he = headers.get(i);
            row.createCell(i).setCellValue(he.getCname());
        }            
        for(int j = 0; j < rows.size(); j++){    
            Object obj = rows.get(j) ;
            Row rowData = sheet.createRow(j+1);
            for(int g = 0; g < headers.size(); g++){            
                ExcelHeader header = headers.get(g);
                Method method = obj.getClass().getMethod(getMethodName(header.getFname()));
                method.setAccessible(true);
                Object result = method.invoke(obj);
                rowData.createCell(g).setCellValue(convert(result));
            }
        }
        return wbk ;
    }    
    private String convert(Object obj){    
        if (obj == null){
            return "";
        } else {
            if (obj instanceof Date){
                return sdf.format(obj);
            }            
            return obj.toString();
        }        
    }
    
    private String getMethodName(String fname){
        String mname = "get" + fname.substring(0,1).toUpperCase();
        return mname + fname.substring(1);
    }
    
    //获取行标题的数据    
    public static ExportProperties getExcelHeaders(Class<?> clazz){    
        ExportProperties headers = new ExportProperties();        
        Field[] fileds = clazz.getDeclaredFields();
        for(Field f : fileds){        
            String fname = f.getName();
            ExcelCol excelCol = f.getAnnotation(ExcelCol.class) ;
            if(f.isAnnotationPresent(ExcelCol.class)){            
                headers.addProperty(new ExcelHeader(fname,excelCol.title(),excelCol.order()));
            }
        }        
        //根据注解order排序
        Collections.sort(headers.getProperties()) ;
        return headers;
    }    
}

自定义注解

package com.hqz.dto.admin;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**类名:ExcelCol <br/>
 * 功能:将Excel文档中的列标题和
 *         实体类的属性对应并支持对列标题位置的排序<br/>
 * 日期:2016年5月20日 <br/>
 */
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ExcelCol {
    /*
     * Excel文档中的列标题名称
     */
    String title();
    /*
     * Excel列标题的位置标识
     */
    int order();
}

实体类

package com.hqz.bo.admin;

import java.math.BigDecimal;

import com.hqz.dto.admin.ExcelCol;

/**类名:FinanceStats <br/>
 * 功能:财务表报每日统计模型<br/>
 * 日期:2016年5月19日 <br/>
 */
public class FinanceStats {    
    
    @ExcelCol(title="统计日期",order=1)
    private String statsDate;
    
    @ExcelCol(title="回款额",order=2)
    private BigDecimal returnMoney;    
    
    @ExcelCol(title="本金",order=3)
    private BigDecimal actualMoney;        
    
    @ExcelCol(title="利息",order=4)
    private BigDecimal interestMoney;
    
    @ExcelCol(title="管理费",order=5)
    private BigDecimal investManageMoney;
    
    @ExcelCol(title="投资金额",order=6)
    private BigDecimal investMoney;
    
    @ExcelCol(title="提现金额",order=7)
    private BigDecimal withDrawCash;    

    @ExcelCol(title="提现手续费",order=8)
    private BigDecimal withDrawFee;
    
    @ExcelCol(title="充值金额",order=9)
    private BigDecimal rechargeCash;    

//    @ExcelCol(title="手机认证礼金",order=10)
    private BigDecimal mobileCheckedCash;

//    @ExcelCol(title="身份认证礼金",order=11)
    private BigDecimal idCardCheckedCash;

//    @ExcelCol(title="银行卡认证礼金",order=12)
    private BigDecimal bankCheckedCash;

//    @ExcelCol(title="VIP收入",order=13)
    private BigDecimal vipCash;

//    @ExcelCol(title="注册送礼金",order=16)
    private BigDecimal registerCash;

//    @ExcelCol(title="积分兑换礼金",order=14)
    private BigDecimal scoreCash;

//    @ExcelCol(title="其它礼金",order=18)
    private BigDecimal othersCash;

    @ExcelCol(title = "已使用红包礼金",order = 10)
    private BigDecimal redTotalCash;

    @ExcelCol(title="红包过期礼金",order=11)
    private BigDecimal expiredCash;

    @ExcelCol(title = "已使用红包券礼金",order = 12)
    private BigDecimal redCash;

    @ExcelCol(title = "红包券过期礼金",order = 13)
    private BigDecimal redExpiredCash;

    @ExcelCol(title="邀请好友礼金",order=14)
    private BigDecimal inviteCash;

    @ExcelCol(title="体验金已回款收益",order=15)
    private BigDecimal investRetain;

    @ExcelCol(title="加息券成本",order=16)
    private BigDecimal rateHikeCost;
    
    public BigDecimal getWithDrawFee() {
        return withDrawFee;
    }

    public void setWithDrawFee(BigDecimal withDrawFee) {
        this.withDrawFee = withDrawFee;
    }
    
    public String getStatsDate() {
        return statsDate;
    }

    public void setStatsDate(String statsDate) {
        this.statsDate = statsDate;
    }    

    public BigDecimal getActualMoney() {
        return actualMoney;
    }

    public void setActualMoney(BigDecimal actualMoney) {
        this.actualMoney = actualMoney;
    }

    public BigDecimal getReturnMoney() {
        return returnMoney;
    }

    public void setReturnMoney(BigDecimal returnMoney) {
        this.returnMoney = returnMoney;
    }

    public BigDecimal getInterestMoney() {
        return interestMoney;
    }

    public void setInterestMoney(BigDecimal interestMoney) {
        this.interestMoney = interestMoney;
    }

    public BigDecimal getInvestManageMoney() {
        return investManageMoney;
    }

    public void setInvestManageMoney(BigDecimal investManageMoney) {
        this.investManageMoney = investManageMoney;
    }

    public BigDecimal getInvestMoney() {
        return investMoney;
    }

    public void setInvestMoney(BigDecimal investMoney) {
        this.investMoney = investMoney;
    }

    public BigDecimal getRegisterCash() {
        return registerCash;
    }

    public void setRegisterCash(BigDecimal registerCash) {
        this.registerCash = registerCash;
    }

    public BigDecimal getMobileCheckedCash() {
        return mobileCheckedCash;
    }

    public void setMobileCheckedCash(BigDecimal mobileCheckedCash) {
        this.mobileCheckedCash = mobileCheckedCash;
    }

    public BigDecimal getIdCardCheckedCash() {
        return idCardCheckedCash;
    }

    public void setIdCardCheckedCash(BigDecimal idCardCheckedCash) {
        this.idCardCheckedCash = idCardCheckedCash;
    }

    public BigDecimal getBankCheckedCash() {
        return bankCheckedCash;
    }

    public void setBankCheckedCash(BigDecimal bankCheckedCash) {
        this.bankCheckedCash = bankCheckedCash;
    }

    public BigDecimal getInviteCash() {
        return inviteCash;
    }

    public void setInviteCash(BigDecimal inviteCash) {
        this.inviteCash = inviteCash;
    }

    public BigDecimal getScoreCash() {
        return scoreCash;
    }

    public void setScoreCash(BigDecimal scoreCash) {
        this.scoreCash = scoreCash;
    }    

    public BigDecimal getOthersCash() {
        return othersCash;
    }

    public void setOthersCash(BigDecimal othersCash) {
        this.othersCash = othersCash;
    }

    public BigDecimal getVipCash() {
        return vipCash;
    }

    public void setVipCash(BigDecimal vipCash) {
        this.vipCash = vipCash;
    }

    public BigDecimal getExpiredCash() {
        return expiredCash;
    }

    public void setExpiredCash(BigDecimal expiredCash) {
        this.expiredCash = expiredCash;
    }

    public BigDecimal getWithDrawCash() {
        return withDrawCash;
    }

    public void setWithDrawCash(BigDecimal withDrawCash) {
        this.withDrawCash = withDrawCash;
    }

    public BigDecimal getRechargeCash() {
        return rechargeCash;
    }

    public void setRechargeCash(BigDecimal rechargeCash) {
        this.rechargeCash = rechargeCash;
    }

    public BigDecimal getRedTotalCash() {
        return redTotalCash;
    }

    public void setRedTotalCash(BigDecimal redTotalCash) {
        this.redTotalCash = redTotalCash;
    }

    public BigDecimal getRedCash() {
        return redCash;
    }

    public void setRedCash(BigDecimal redCash) {
        this.redCash = redCash;
    }

    public BigDecimal getRedExpiredCash() {
        return redExpiredCash;
    }

    public void setRedExpiredCash(BigDecimal redExpiredCash) {
        this.redExpiredCash = redExpiredCash;
    }

    public BigDecimal getInvestRetain() {
        return investRetain;
    }

    public void setInvestRetain(BigDecimal investRetain) {
        this.investRetain = investRetain;
    }

    public BigDecimal getRateHikeCost() {
        return rateHikeCost;
    }

    public void setRateHikeCost(BigDecimal rateHikeCost) {
        this.rateHikeCost = rateHikeCost;
    }
}

猜你喜欢

转载自www.cnblogs.com/sam-w21/p/9721103.html