excel导出工具类实例

1.定义要导出的实体类 在属性上可以定义导出excel的列名,枚举值

// 入库单编号
@Excel(columnName = "入库单号")
private String stockInNo;

// 入库类型
@Excel(columnName = "入库类型")
@ExcelEnum(code = {"ESRK-BJ","ESRK-CG","ESRK-JS","ESRK-PY"},name = {"备件入","采购入","拒收入","盘盈入"})
private String stockInType;

2.分页查询导出

public void exportStock(HttpServletResponse response,String options){

    Account account = AccountUtils.getCurrentAccount();
    String uuId = UUID.randomUUID().toString();
    uuId = uuId.replace("-", "");
    // 执行过程编号
    String processNo = "[过程" + uuId + "]";
    logUtils.info(processNo + "[入库单导出 exportStock][操作人:" + account.getErp() + ",查询条件:" + JSON.toJSONString(options) + "]");

    StockInDocInfoQO stockInDocInfoQuery = getStockInDocInfoQO(options);
    //处理多个配送中心
    String warehouseCode = stockInDocInfoQuery.getWarehouseCode();
    List<String> distribCenterCodeList1 = getDistribCenterCodeList(warehouseCode);

    stockInDocInfoQuery.setDistribCenterCodeList(distribCenterCodeList1);

    stockInDocInfoQuery.setWarehouseCode("94");
    //处理多个单据状态
    String stockInStatus = stockInDocInfoQuery.getStockInStatus();
    if(StringUtils.isNotEmpty(stockInStatus)){
        List<String> stockInStatusList = Arrays.asList(stockInStatus.split(","));
        stockInDocInfoQuery.setStockInStatusList(stockInStatusList);
    }

    int pageNum = 1;
    int pageSize = 200;
    Page<StockInDocInfoQO> page = this.buildPage(pageNum, pageSize, "id", "desc");

    Page<StockInDocInfoQueryVO> queryPage = null;
    queryPage = stockInDocInfoInternalJSFService.findPage(stockInDocInfoQuery,page);

    List<StockInDocInfoQueryVO> next = queryPage.getContent();

    ExcelUtilPro excelUtilPro = ExcelUtilPro.newInstance(StockInDocInfoQueryVO.class, "入库单导出");
    excelUtilPro.addList(next);
    //记录导出的条数
    long totalElements = next.size();

    while (queryPage.hasNext() && totalElements<MAX_NUMBER){ //限制导出条数上限

        pageNum++;

        page = this.buildPage(pageNum, pageSize, "id", "desc");

        queryPage = this.stockInDocInfoInternalJSFService.findPage(stockInDocInfoQuery, page);

        next = queryPage.getContent();

        excelUtilPro.addList(next);

        totalElements += next.size();

    }

    logUtils.info(processNo + "[入库单导出 exportStock][操作人:" + account.getErp() + ",导出条数:" + totalElements + "]");

    String finename = "入库单导出" + SIMPLE_DATE_FORMAT.format(new Date()) + ".xlsx";

    String downFinename = null;

    try {
        downFinename = new String(finename.getBytes("UTF-8"), "ISO-8859-1");
    } catch (UnsupportedEncodingException e) {
        downFinename = finename;
        e.printStackTrace();
    }

    try {

        response.reset();
        response.setHeader("Content-Disposition", "attachment; filename=\"" + downFinename + "\"");
        response.setContentType("application/octet-stream;charset=UTF-8");
        BufferedOutputStream outputStream = new BufferedOutputStream(response.getOutputStream()) ;
        excelUtilPro.write(outputStream);

    } catch (Exception e) {

        e.printStackTrace();

    }

}
导出工具类请看上篇文章

猜你喜欢

转载自blog.csdn.net/rock93/article/details/80060014