package com.beilei.controller.applyprocess;


import com.beilei.entity.DriverApplyProcess;
import com.beilei.util.WebUtils;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import org.apache.log4j.Logger;
import com.beilei.utils.DriversellTimeUtils;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.Date;
import java.util.List;

/**
 * Created bl lx on 2016/7/25.
 */
public class ApplyProcessExcelUtil {

    private static final Logger logger = Logger.getLogger(ApplyProcessExcelUtil.class);

    public static void exportExcel(HttpServletRequest request, HttpServletResponse response, List<DriverApplyProcess> driverApplyProcess) throws Exception {
        String filePath = request.getServletContext().getRealPath("/");
        File file = new File(filePath + "[" + new Date().getTime() + "]" + ".xls");
        WritableWorkbook book = null;
        book = Workbook.createWorkbook(file);
        WritableSheet sheet = book.createSheet("第一页", 0);

        // 设置样式,第一行
        WritableFont wfc1 = new WritableFont(WritableFont.ARIAL, 13, WritableFont.NO_BOLD, false);
        jxl.write.WritableCellFormat wcfFC1 = new jxl.write.WritableCellFormat(wfc1);
        wcfFC1.setAlignment(jxl.format.Alignment.CENTRE);
        wcfFC1.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);

        // 设置样式,第二行
        WritableFont wfc2 = new WritableFont(WritableFont.ARIAL, 10, WritableFont.NO_BOLD, false);
        jxl.write.WritableCellFormat wcfFC2 = new jxl.write.WritableCellFormat(wfc2);
        wcfFC2.setAlignment(jxl.format.Alignment.CENTRE);
        wcfFC2.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);

        String title = "城市,所属,id,身份证姓名,身份证号码,手机号码,提交时间,行为,状态";

        String[] splitsTitle = title.split(",");
        int length = splitsTitle.length;
        for (int i = 0; i < length; i++) {
            sheet.addCell(new Label(i, 0, splitsTitle[i], wcfFC2));
            sheet.setColumnView(i, 15);
        }

        for (int c = 0; c < driverApplyProcess.size(); c++) {
            DriverApplyProcess o = driverApplyProcess.get(c);
            int rowNum = c + 1;
            int colNum = 0;
            sheet.addCell(new Label(colNum++, rowNum, o.getDelivererDto().getCityName() == null ? "" : o.getDelivererDto().getCityName().toString(), wcfFC2));
            sheet.addCell(new Label(colNum++, rowNum, o.getDelivererDto().getStationRegionName() == null ? "" : o.getDelivererDto().getStationRegionName(), wcfFC2));
            sheet.addCell(new Label(colNum++, rowNum, o.getDelivererDto().getBdId() == null ? "" : o.getDelivererDto().getBdId().toString(), wcfFC2));
            sheet.addCell(new Label(colNum++, rowNum, o.getDelivererDto().getName() == null ? "" : o.getDelivererDto().getName(), wcfFC2));
            sheet.addCell(new Label(colNum++, rowNum, o.getDelivererDto().getIdentifyNumber() == null ? "" : o.getDelivererDto().getIdentifyNumber(), wcfFC2));
            sheet.addCell(new Label(colNum++, rowNum, o.getDelivererDto().getMobile()== null ? "" : o.getDelivererDto().getMobile(), wcfFC2));

            sheet.addCell(new Label(colNum++, rowNum, o.getcT() == null ? "" : DriversellTimeUtils.unixTimestampToDateString(o.getcT(),"yyyy/MM/dd"), wcfFC2));
            sheet.addCell(new Label(colNum++, rowNum, o.getType() == null ? "" : o.getType().toString(), wcfFC2));
            sheet.addCell(new Label(colNum++, rowNum, o.getStatusName() == null ? "" : o.getStatusName().toString(), wcfFC2));
        }
        book.write();
        book.close();
        WebUtils.fileToResponse(response, file);
    }

}

调用
 
Page<DriverApplyProcess> page = driverApplyProcessService.query(applyProcess, 1, Integer
        .MAX_VALUE).getBody();

// 输出excel
ApplyProcessExcelUtil.exportExcel(request, response, page.getItems());