多表逐级关联报表,使用poi方法导出excel


难点要求

多表关联导出,简单介绍:有A1表,A2表,B表,C表,A1和A2是一对多的关系,B表和A1表也是一对多的关系,C表和B表是一对多的关系。这是简化的业务逻辑,表之间的关系就类似这种。完成对关联导出。

实现思路

熟悉poi导出的可能会知道,报表的导出总体思路是将数据按照excel的每一行去写入数据的,假如是有序的数据还好说点,比如单表的导出,直接单表查询出来,循环写入每一个excel行就行,这种做法很简单。但是假如遇到一对多的表关联数据呢?更复杂一点的就是一对多对多,每一行都要根据数据来变化结构。
这种复杂逻辑我真的没有什么特别好的方法来解决,只能将这些逻辑拆分,将所有数据都查询出来,然后按照每一个一个多关系拆分,利用stream表达式进行快速筛选,完成数据的重组,写入excel中。

实现代码

package ect.inv.service.impl;
import com.google.common.collect.Lists;
import com.hand.hap.core.BaseConstants;
import com.hand.hap.core.IRequest;
import ect.co.util.InitDataStyle;
import ect.inv.dto.BusWare;
import ect.inv.mapper.BusWareMapper;
import ect.inv.service.IInvBusAccountService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
 * @ClassName: InvBusAccountServiceImpl
 * @Description:东营公司业务台账
 * @Date: 2018/9/26 11:32 AM
 * @Version 1.0
 */
@Service
public class InvBusAccountServiceImpl implements IInvBusAccountService {
    private final static String SHEET1_NAME = "sheet1";//sheet页1名称
 
    private final static String PUBLIC_NET = "PUBLIC_NET";   //公检净重
    private final static String PUBLIC_PUBLIC = "PUBLIC_PUBLIC";   //   公检公重
    private final static String PUBLIC_ROUGH = "PUBLIC_ROUGH";   // 公检毛重

    /**
     * 定义单元格数量,对各个表在excel之中占据的单元格个数完成定义
     */
    private final static int BUS_WARE = 9;   //入库
    private final static int BUS_PAYMENT = 2;     //付款
    private final static int BUS_SALES = 9;   //销售
    private final static int BUS_OUT_LIBRARY = 2;   //出库
    private final static int BUS_RECEIPT = 2;   //收款
    private final static int BUS_TRANSFER = 3;   //移库
    private final static int BUS_PURCHASE = 4;   //采购
    private final static int BUS_WARE_RECEIPT = 8;   //仓单
    /**
     * 留空部分的list,将每一个表的空list定义好,便于下面使用。
     */
    private static List emptyWare = getEmptyList(BUS_WARE);//入库留空部分
    private static List emptyPayment = getEmptyList(BUS_PAYMENT);//付款留空部分
    private static List emptySales = getEmptyList(BUS_SALES);//销售留空部分
    private static List emptyOutLibrary = getEmptyList(BUS_OUT_LIBRARY);//出库留空部分
    private static List emptyReceipt = getEmptyList(BUS_RECEIPT);//收款留空部分
    private static List emptyTransfer = getEmptyList(BUS_TRANSFER);//移库留空部分
    private static List emptyPurchase = getEmptyList(BUS_PURCHASE);//采购留空部分
    /*===================================================公用方法=====================================================*/
    private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat(BaseConstants.DATE_FORMAT);//日期转换器
    private final static String EMPTY_STRING = "";//空字符串
    //宽度调整
    private final static int CHINESE_WIDTH = 256 * 2;
    //第一位为汉字个数,后面为位置
    private final static int[] UNIT_LOCATION = {6, 0, 19, 20, 36};
    private final static int[] SUBINV_LOCATION = {24, 18};
    private final static int[][] LOCATIONS = {UNIT_LOCATION, SUBINV_LOCATION};

    @Autowired
    private BusWareMapper wareMapper;  //入库查询

    /**
     * 联营业务台账导出
     *
     * @param iRequest
     * @param record
     * @return
     */
    @Override
    public XSSFWorkbook exportExcelProVen(IRequest iRequest, BusWare record) {
        //数据整理
        List storageList = wareMapper.selectWare(record);   //入库数据查询
        List paymentList = wareMapper.selectPayment(record);  //付款数据查询
        List saleList = wareMapper.selectSale(record);  //销售数据查询
        List outLibraryList = wareMapper.selecOutLibrary(record);  //出库数据查询
        List receiptList = wareMapper.selectReceipt(record);  //收款数据查询
        List transferList = wareMapper.selectTransfer(record);  //移库数据查询
        List purchaseList = wareMapper.selectPurchase(record);  //采购数据查询

       //存放组装好的数据
        List> allRows = new ArrayList<>();

        //TODO:将入库数据按合同编号分组分组处理
        Map> groupByConNumMap =
                storageList.stream().collect(Collectors.groupingBy(BusWare::getConNum));
        //TODO:将收款数据按合同编号分组分组处理
        Map> receiptGroupByConNumMap =
                receiptList.stream().collect(Collectors.groupingBy(BusWare::getConNum));
        // TODO:组装数据start,循环入库数据,因为入库数据是业务逻辑的主数据,其他表的数据都是根据主数据的变化而变化
        for (String conNum : groupByConNumMap.keySet()) {
          //TODO:使用合同编号做条件,利用stream表达式筛选付款数据
            List payments = paymentList.stream().filter(busWare -> conNum.equals(busWare.getConNum()))
                    .collect(Collectors.toList());
          //TODO:根据合同号获取分组的入库数据
            List busWareList = groupByConNumMap.get(conNum);
            int b = 0;
           //TODO:处理第一个一对多表关系,循环入库数据
            for (BusWare storage : busWareList) {
                 //TODO:以下是根据批次号来筛选各个表的数据
                List sales = saleList.stream().filter(busWare -> storage.getBatchNum().equals(busWare.getBatchNum()))
                        .collect(Collectors.toList());
                List outLibrarys = outLibraryList.stream().filter(busWare -> (storage.getBatchNum().equals(busWare.getBatchNum())
                        && storage.getBatchId().equals(busWare.getBatchId()) && storage.getWareId().equals(busWare.getWareId())))
                        .collect(Collectors.toList());
                List transfers = transferList.stream().filter(busWare -> storage.getBatchNum().equals(busWare.getBatchNum()))
                        .collect(Collectors.toList());
                List purchases = purchaseList.stream().filter(busWare -> storage.getBatchNum().equals(busWare.getBatchNum()))
                        .collect(Collectors.toList());
                //todo:假如各个表筛选的数据为大于0,说明有一对多的关系,反之,不用处理,直接放入准备好的allRows中。
                int[] nArray = {sales.size(), outLibrarys.size(), transfers.size(), purchases.size()};
                int maxSize = getMax(nArray);
                if (maxSize > 0) {
                //TODO:处理第二层一对多关系。这里是第一层已经筛选好的一对多数据。
                    for (int i = 0; i < maxSize; i++) {
                        if (sales.size() > 0) {
                        //收款数据与销售数据是一对多关系,根据合同号进行筛选,并重新组装数据
                            List receipt=i(i+1)&&(!sales.get(i).getConNum().equals(sales.get(i+1).getConNum()))){
                                    for (BusWare busWare:receipt){
                                        allRows.add(addRow(emptyWare,
                                                emptyPayment,
                                                emptySales,
                                                emptyOutLibrary,
                                                initReceipt(busWare),
                                                emptyTransfer,
                                                emptyPurchase));
                                        b++;
                                    }
                                    receiptGroupByConNumMap.put(sales.get(0).getConNum(),null);
                                }
                            }else{
                              //TODO:组装第一层一对多数据
                                allRows.add(addRow(i == 0 ? initStorage(storage) : emptyWare,
                                        b < payments.size() ? initPayment(payments.get(b)) : emptyPayment,
                                        i < sales.size() ? initSales(sales.get(i)) : emptySales,
                                        i < outLibrarys.size() ? initOutLibrary(outLibrarys.get(i)) : emptyOutLibrary,
                                        emptySalfReceipt,
                                        i < transfers.size() ? initTransfer(transfers.get(i)) : emptyTransfer,
                                        i < purchases.size() ? initPurchase(purchases.get(i)) : emptyPurchase));
                                b++;
                            }
                        } else {
                             //TODO:组装第一层一对多数据
                            allRows.add(addRow(i == 0 ? initStorage(storage) : emptyWare,
                                    b < payments.size() ? initPayment(payments.get(b)) : emptyPayment,
                                    emptySales,
                                    i < outLibrarys.size() ? initOutLibrary(outLibrarys.get(i)) : emptyOutLibrary,
                                    emptySalfReceipt,
                                    i < transfers.size() ? initTransfer(transfers.get(i)) : emptyTransfer,
                                    i < purchases.size() ? initPurchase(purchases.get(i)) : emptyPurchase));
                            b++;
                        }
                    }
                } else {
                    //TODO:组装第一层一对多数据
                    allRows.add(addRow(initStorage(storage),
                            b < payments.size() ? initPayment(payments.get(b)) : emptyPayment,
                            emptySales,
                            emptyOutLibrary,
                            emptyReceipt,
                            emptyTransfer,
                            emptyPurchase));
                    b++;
                }
            }
            int max = payments.size();
            if (b < max) {
                allRows.add(addRow(emptyWare,
                        b < payments.size() ? initPayment(payments.get(b)) : emptyPayment,
                        emptySales,
                        emptyOutLibrary,
                        emptyReceipt,
                        emptyTransfer,
                        emptyPurchase));
            }
        }
        XSSFWorkbook xwork = new XSSFWorkbook();
        XSSFSheet sheet = xwork.createSheet(SHEET1_NAME);
        //表标题
        //居中style
        XSSFCellStyle mediumStyle = InitDataStyle.getHorizontalCenterBoldHead2(xwork);
        //拼接title
        XSSFRow title = sheet.createRow(0);
        sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, BUS_WARE - 1));
        title.createCell(0).setCellValue("入库");
        title.getCell(0).setCellStyle(mediumStyle);
        sheet.addMergedRegion(new CellRangeAddress(0, 0, BUS_WARE, BUS_WARE + BUS_PAYMENT - 1));
        title.createCell(BUS_WARE).setCellValue("付款");
        title.getCell(BUS_WARE).setCellStyle(mediumStyle);
        sheet.addMergedRegion(new CellRangeAddress(0, 0, BUS_WARE + BUS_PAYMENT,
                BUS_WARE + BUS_PAYMENT + BUS_SALES - 1));
        title.createCell(BUS_WARE + BUS_PAYMENT).setCellValue("销售");
        title.getCell(BUS_WARE + BUS_PAYMENT).setCellStyle(mediumStyle);
        sheet.addMergedRegion(new CellRangeAddress(0, 0, BUS_WARE + BUS_PAYMENT + BUS_SALES,
                BUS_WARE + BUS_PAYMENT + BUS_SALES + BUS_OUT_LIBRARY - 1));
        title.createCell(BUS_WARE + BUS_PAYMENT + BUS_SALES).setCellValue("出库");
        title.getCell(BUS_WARE + BUS_PAYMENT + BUS_SALES).setCellStyle(mediumStyle);
        sheet.addMergedRegion(new CellRangeAddress(0, 0, BUS_WARE + BUS_PAYMENT + BUS_SALES + BUS_OUT_LIBRARY,
                BUS_WARE + BUS_PAYMENT + BUS_SALES + BUS_OUT_LIBRARY + BUS_RECEIPT - 1));
        title.createCell(BUS_WARE + BUS_PAYMENT + BUS_SALES + BUS_OUT_LIBRARY).setCellValue("收款");
        title.getCell(BUS_WARE + BUS_PAYMENT + BUS_SALES + BUS_OUT_LIBRARY).setCellStyle(mediumStyle);
        sheet.addMergedRegion(new CellRangeAddress(0, 0, BUS_WARE + BUS_PAYMENT + BUS_SALES + BUS_OUT_LIBRARY + BUS_RECEIPT,
                BUS_WARE + BUS_PAYMENT + BUS_SALES + BUS_OUT_LIBRARY + BUS_RECEIPT + BUS_TRANSFER - 1));
        title.createCell(BUS_WARE + BUS_PAYMENT + BUS_SALES + BUS_OUT_LIBRARY + BUS_RECEIPT).setCellValue("移库");
        title.getCell(BUS_WARE + BUS_PAYMENT + BUS_SALES + BUS_OUT_LIBRARY + BUS_RECEIPT).setCellStyle(mediumStyle);
        sheet.addMergedRegion(new CellRangeAddress(0, 0, BUS_WARE + BUS_PAYMENT + BUS_SALES + BUS_OUT_LIBRARY + BUS_RECEIPT + BUS_TRANSFER,
                BUS_WARE + BUS_PAYMENT + BUS_SALES + BUS_OUT_LIBRARY + BUS_RECEIPT + BUS_TRANSFER + BUS_PURCHASE - 1));
        title.createCell(BUS_WARE + BUS_PAYMENT + BUS_SALES + BUS_OUT_LIBRARY + BUS_RECEIPT + BUS_TRANSFER).setCellValue("采购");
        title.getCell(BUS_WARE + BUS_PAYMENT + BUS_SALES + BUS_OUT_LIBRARY + BUS_RECEIPT + BUS_TRANSFER).setCellStyle(mediumStyle);
        sheet.addMergedRegion(new CellRangeAddress(0, 0, BUS_WARE + BUS_PAYMENT + BUS_SALES + BUS_OUT_LIBRARY + BUS_RECEIPT + BUS_TRANSFER + BUS_PURCHASE,
                BUS_WARE + BUS_PAYMENT + BUS_SALES + BUS_OUT_LIBRARY + BUS_RECEIPT + BUS_TRANSFER + BUS_PURCHASE + BUS_WARE_RECEIPT - 1));

        //表头
        XSSFRow head = sheet.createRow(1);
        List colNameList = Lists.newArrayList(
                " 业务类型 ", " 协议号 ", " 批号 ", " 品名 ", " 采摘方式 ", " 仓库 ", " 件数 ", " 重量 ", " 在库状态 ",
                " 付款日期 ", " 付款金额 ", " 销售合同号 ", " 买方 ", " 签约日期 ", " 单价 ", " 提货方式 ", " 出疆补贴 ", " 库费承担 ",
                " 重量结算 ", " 签约重量 ", " 出库日期 ", " 出库重量 ", " 收款日期 ", " 收款金额 ", " 移库日期 ", " 入库日期 ", " 内地仓库 ",
                " 采购合同号 ", " 采购价格 ", " 采购价差 ", " 采购金额 "
        );
      //TODO:循环将表头显示插入excel
        for (int i = 0; i < colNameList.size(); i++) {
            head.createCell(i).setCellValue(colNameList.get(i).toString());
            head.getCell(i).setCellStyle(mediumStyle);
        }

        int rowNum = 2;
        XSSFCellStyle centerStyle = xwork.createCellStyle();
        //插入上部分
        rowNum = addRow(rowNum, sheet, allRows, centerStyle);
        //自动适应列宽
        for (int i = 0; i < colNameList.size(); i++) {
            sheet.autoSizeColumn(i, true);
        }
        return xwork;
    }
    //TODO:根据字段个数组装空表数据
    private static List getEmptyList(int size) {
        List result = Lists.newArrayListWithCapacity(size);
        for (int i = 0; i < size; i++) {
            result.add(EMPTY_STRING);
        }
        return result;
    }

    /**
     * 组装行数据
     *
     * @param wareList
     * @param paymentList
     * @return
     */
    private static List addRow(List wareList, List paymentList, List salesList, List outLibrary,
                                       List receiptList, List transfer, List purchase) {
        List result = new ArrayList<>();
        result.addAll(wareList);
        result.addAll(paymentList);
        result.addAll(salesList);
        result.addAll(outLibrary);
        result.addAll(receiptList);
        result.addAll(transfer);
        result.addAll(purchase);
        return result;
    }

    /**
     * 组装行数据
     *
     * @param wareList
     * @param paymentList
     * @return
     */
    private static List addRow(List wareList, List paymentList, List salesList, List outLibrary,
                                       List receiptList) {
        List result = new ArrayList<>();
        result.addAll(wareList);
        result.addAll(paymentList);
        result.addAll(salesList);
        result.addAll(outLibrary);
        result.addAll(receiptList);
        return result;
    }

    /**
     * 解析入库数据
     *
     * @return
     */
    private static List initStorage(BusWare busWare) {
        List result = new ArrayList<>();
        result.add(busWare.getMeaning() == null ? "" : busWare.getMeaning());      //业务类型
        result.add(busWare.getConNum() == null ? "" : busWare.getConNum());       //协议号
        result.add(busWare.getBatchNum() == null ? "" : busWare.getBatchNum());     //批号
        result.add(busWare.getTag() == null ? "" : busWare.getTag());          //品名
        result.add(busWare.getDescription() == null ? "" : busWare.getDescription());  //采摘方式
        result.add(busWare.getSubinvName() == null ? "" : busWare.getSubinvName());   //仓库
        result.add(busWare.getEntryQty() == null ? "" : busWare.getEntryQty());     //件数
        result.add(busWare.getEntryWeight() == null ? "" : busWare.getEntryWeight());  //重量
        String depotStatus = "";
        if (busWare.getBatchQty() != null && busWare.getAlHoldQty() != null) {
            if (busWare.getMoveListBatchId() == null && (busWare.getBatchQty().compareTo(busWare.getAlHoldQty()) > 0)) {
                depotStatus = "在库";
            } else if (busWare.getMoveListBatchId() == null && (busWare.getBatchQty().compareTo(busWare.getAlHoldQty()) <= 0)) {
                depotStatus = "已售";
            } else if (busWare.getMoveListBatchId() != null && (busWare.getBatchQty().compareTo(busWare.getAlHoldQty()) > 0)) {
                depotStatus = "移库";
            } else if (busWare.getMoveListBatchId() != null && (busWare.getBatchQty().compareTo(busWare.getAlHoldQty()) <= 0)) {
                depotStatus = "移库已售";
            }
        }
        result.add(depotStatus);                        //在库状态
        return result;
    }


    /**
     * 解析入库数据(自营)
     *
     * @return
     */
    private static List initSelfStorage(BusWare busWare) {
        List result = new ArrayList<>();
        result.add(busWare.getMeaning() == null ? "" : busWare.getMeaning());      //业务类型
        result.add(busWare.getBatchNum() == null ? "" : busWare.getBatchNum());     //批号
        result.add(busWare.getcFactory() == null ? "" : busWare.getcFactory());     //加工单位
        result.add(busWare.getConNum() == null ? "" : busWare.getConNum());       //采购合同号
        result.add(busWare.getTag() == null ? "" : busWare.getTag());          //品名
        result.add(busWare.getDescription() == null ? "" : busWare.getDescription());  //采摘方式
        result.add(busWare.getSubinvName() == null ? "" : busWare.getSubinvName());   //仓库
        result.add(busWare.getEntryQty() == null ? "" : busWare.getEntryQty());     //件数
        result.add(busWare.getEntryWeight() == null ? "" : busWare.getEntryWeight());  //重量
        String depotStatus = "";
        if (busWare.getBatchQty() != null && busWare.getAlHoldQty() != null) {
            if (busWare.getMoveListBatchId() == null && (busWare.getBatchQty().compareTo(busWare.getAlHoldQty()) > 0)) {
                depotStatus = "在库";
            } else if (busWare.getMoveListBatchId() == null && (busWare.getBatchQty().compareTo(busWare.getAlHoldQty()) <= 0)) {
                depotStatus = "已售";
            } else if (busWare.getMoveListBatchId() != null && (busWare.getBatchQty().compareTo(busWare.getAlHoldQty()) > 0)) {
                depotStatus = "移库";
            } else if (busWare.getMoveListBatchId() != null && (busWare.getBatchQty().compareTo(busWare.getAlHoldQty()) <= 0)) {
                depotStatus = "移库已售";
            }
        }
        result.add(depotStatus);                        //在库状态
        result.add(busWare.getSignedDate() == null ? "" : busWare.getSignedDate());  //采购日期
        result.add(busWare.getOrderDate() == null ? "" : busWare.getOrderDate());  //货转日期
        result.add(busWare.getPrice() == null ? "" : busWare.getPrice());  //单价
        return result;
    }


    /**
     * 解析付款数据
     *
     * @param busWare
     * @return
     */
    private static List initPayment(BusWare busWare) {
        List result = new ArrayList<>();
        result.add(busWare.getApplyDate() == null ? "" : busWare.getApplyDate());  //付款日期
        result.add(busWare.getApplyAmt() == null ? "" : busWare.getApplyAmt());   //付款金额
        return result;
    }

    /**
     * 解析销售数据
     *
     * @param busWare
     * @return
     */
    private static List initSales(BusWare busWare) {
        List result = new ArrayList<>();
        result.add(busWare.getConNum() == null ? "" : busWare.getConNum());   //销售合同号
        result.add(busWare.getPartyName() == null ? "" : busWare.getPartyName());   //买方
        result.add(busWare.getSignedDate() == null ? "" : busWare.getSignedDate());   //签约日期
        result.add(busWare.getPrice() == null ? "" : busWare.getPrice());    //单价
        result.add(busWare.getDeltType() == null ? "" : busWare.getDeltType());  //提货方式
        //出疆补贴
        if (busWare.getSubsidyClaim() == null) {
            result.add("");
        } else if (busWare.getSubsidyClaim().equals("甲方")) {
            result.add(busWare.getOnePartyName());
        } else if (busWare.getSubsidyClaim().equals("乙方")) {
            result.add(busWare.getPartyName());
        } else {
            result.add(busWare.getSubsidyClaim());
        }
        //库费承担
        if (busWare.getOtherPartyNum() == null) {
            result.add("");
        } else if (busWare.getOtherPartyNum().equals("甲方")) {
            result.add(busWare.getOnePartyName());
        } else if (busWare.getOtherPartyNum().equals("乙方")) {
            result.add(busWare.getPartyName());
        } else {
            result.add(busWare.getOtherPartyNum());
        }
        //重量结算
        if (busWare.getPaymentAccording() == null) {
            result.add("");
        } else if (busWare.getPaymentAccording().equals(PUBLIC_PUBLIC)) {
            result.add("公检公重");
        } else if (busWare.getPaymentAccording().equals(PUBLIC_ROUGH)) {
            result.add("公检毛重");
        } else if (busWare.getPaymentAccording().equals(PUBLIC_NET)) {
            result.add("公检净重");
        } else {
            result.add("");
        }
        //签约重量
        if (busWare.getWeightType() == null) {
            result.add("");
        } else if (busWare.getWeightType().equals("公检公重")) {
            result.add(busWare.getPubPw());
        } else if (busWare.getWeightType().equals("公检毛重")) {
            result.add(busWare.getPubGw());
        } else if (busWare.getWeightType().equals("pubNw")) {
            result.add(busWare.getPubNw());
        }
        return result;
    }

    /**
     * 解析销售数据
     *
     * @param busWare
     * @return
     */
    private static List initSelfSales(BusWare busWare) {
        List result = new ArrayList<>();
        result.add(busWare.getConNum() == null ? "" : busWare.getConNum());   //销售合同号
        result.add(busWare.getPartyName() == null ? "" : busWare.getPartyName());   //买方
        result.add(busWare.getSignedDate() == null ? "" : busWare.getSignedDate());   //签约日期
        //签约重量
        if (busWare.getWeightType() == null) {
            result.add("");
        } else if (busWare.getWeightType().equals("公检公重")) {
            result.add(busWare.getPubPw());
        } else if (busWare.getWeightType().equals("公检毛重")) {
            result.add(busWare.getPubGw());
        } else if (busWare.getWeightType().equals("pubNw")) {
            result.add(busWare.getPubNw());
        }
        result.add(busWare.getPrice() == null ? "" : busWare.getPrice());    //单价
        result.add(busWare.getDeltType() == null ? "" : busWare.getDeltType());  //提货方式
        //出疆补贴
        if (busWare.getSubsidyClaim() == null) {
            result.add("");
        } else if (busWare.getSubsidyClaim().equals("甲方")) {
            result.add(busWare.getOnePartyName());
        } else if (busWare.getSubsidyClaim().equals("乙方")) {
            result.add(busWare.getPartyName());
        } else {
            result.add(busWare.getSubsidyClaim());
        }
        //库费承担
        if (busWare.getOtherPartyNum() == null) {
            result.add("");
        } else if (busWare.getOtherPartyNum().equals("甲方")) {
            result.add(busWare.getOnePartyName());
        } else if (busWare.getOtherPartyNum().equals("乙方")) {
            result.add(busWare.getPartyName());
        } else {
            result.add(busWare.getOtherPartyNum());
        }
        return result;
    }

    /**
     * 解析出库数据
     *
     * @param busWare
     * @return
     */
    private static List initOutLibrary(BusWare busWare) {
        List result = new ArrayList<>();
        result.add(busWare.getOrderDate());  //出库日期
        result.add(busWare.getWareWeight()); //出库重量
        return result;
    }

    /**
     * 解析收款数据
     *
     * @param busWare
     * @return
     */
    private static List initReceipt(BusWare busWare) {
        List result = new ArrayList<>();
        result.add(busWare.getReceiptDate());   //收款日期
        result.add(busWare.getEnteredAmt());    //收款金额
        return result;
    }

    /**
     * 解析移库数据
     *
     * @param busWare
     * @return
     */
    private static List initTransfer(BusWare busWare) {
        List result = new ArrayList<>();
        result.add(busWare.getOrderDate());   //移库日期
        result.add(busWare.getEntryDate());    //入库日期
        result.add(busWare.getToSubinvName());    //内地仓库
        return result;
    }

    /**
     * 解析采购数据
     *
     * @param busWare
     * @return
     */
    private static List initPurchase(BusWare busWare) {
        List result = new ArrayList<>();
        result.add(busWare.getConNum());   //采购合同号
        result.add(busWare.getPrice());    //采购价格
        result.add(busWare.getSalePrice().subtract(busWare.getPrice()));    //采购价差
        result.add(busWare.getPrice().multiply(busWare.getQuantity()));    //采购金额
        return result;
    }


    private int addRow(int rowNum, XSSFSheet sheet, List> values, XSSFCellStyle cellStyle) {
        if (values != null) {
            for (List value : values) {
                XSSFRow row = sheet.createRow(rowNum);
                for (int i = 0; i < value.size(); i++) {
                    setCellValue(row.createCell(i), value.get(i), cellStyle);
                }
                rowNum++;
            }
        }
        return rowNum;
    }

    private void setCellValue(XSSFCell cell, Object value, XSSFCellStyle centerStyle) {
        if (value instanceof BigDecimal) {
            //XSSFCellStyle centerStyle = cell.getSheet().getWorkbook().createCellStyle();
            centerStyle.setDataFormat(getDecimalPlace((BigDecimal) value));
            cell.setCellValue(((BigDecimal) value).doubleValue());
            cell.setCellStyle(centerStyle);
        } else if (value instanceof Date) {
            cell.setCellValue(simpleDateFormat.format((Date) value));
        } else {
            if (value == null) {
                cell.setCellValue(EMPTY_STRING);
            } else {
                cell.setCellValue(value.toString());
            }
        }
    }

    private int getDecimalPlace(BigDecimal value) {
        String s = value.toPlainString();
        String[] split = s.split(".");
        if (split != null && split.length > 1) {
            return split[1].length();
        }
        return 0;
    }


    private int getMax(int[] array) {
        int A[] = array;
        int max = A[0];
        for (int i = 0; i < A.length; i++) {
            if (A[i] > max)
                max = A[i];
        }
        return max;
    }
}

导出结果

屏幕快照 2018-12-02 下午7.19.04.png

你可能感兴趣的:(多表逐级关联报表,使用poi方法导出excel)