多表逐级关联报表,使用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<Object> emptyWare = getEmptyList(BUS_WARE);//入库留空部分
    private static List<Object> emptyPayment = getEmptyList(BUS_PAYMENT);//付款留空部分
    private static List<Object> emptySales = getEmptyList(BUS_SALES);//销售留空部分
    private static List<Object> emptyOutLibrary = getEmptyList(BUS_OUT_LIBRARY);//出库留空部分
    private static List<Object> emptyReceipt = getEmptyList(BUS_RECEIPT);//收款留空部分
    private static List<Object> emptyTransfer = getEmptyList(BUS_TRANSFER);//移库留空部分
    private static List<Object> 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<BusWare> storageList = wareMapper.selectWare(record);   //入库数据查询
        List<BusWare> paymentList = wareMapper.selectPayment(record);  //付款数据查询
        List<BusWare> saleList = wareMapper.selectSale(record);  //销售数据查询
        List<BusWare> outLibraryList = wareMapper.selecOutLibrary(record);  //出库数据查询
        List<BusWare> receiptList = wareMapper.selectReceipt(record);  //收款数据查询
        List<BusWare> transferList = wareMapper.selectTransfer(record);  //移库数据查询
        List<BusWare> purchaseList = wareMapper.selectPurchase(record);  //采购数据查询

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

        //TODO:将入库数据按合同编号分组分组处理
        Map<String, List<BusWare>> groupByConNumMap =
                storageList.stream().collect(Collectors.groupingBy(BusWare::getConNum));
        //TODO:将收款数据按合同编号分组分组处理
        Map<String, List<BusWare>> receiptGroupByConNumMap =
                receiptList.stream().collect(Collectors.groupingBy(BusWare::getConNum));
        // TODO:组装数据start,循环入库数据,因为入库数据是业务逻辑的主数据,其他表的数据都是根据主数据的变化而变化
        for (String conNum : groupByConNumMap.keySet()) {
          //TODO:使用合同编号做条件,利用stream表达式筛选付款数据
            List<BusWare> payments = paymentList.stream().filter(busWare -> conNum.equals(busWare.getConNum()))
                    .collect(Collectors.toList());
          //TODO:根据合同号获取分组的入库数据
            List<BusWare> busWareList = groupByConNumMap.get(conNum);
            int b = 0;
           //TODO:处理第一个一对多表关系,循环入库数据
            for (BusWare storage : busWareList) {
                 //TODO:以下是根据批次号来筛选各个表的数据
                List<BusWare> sales = saleList.stream().filter(busWare -> storage.getBatchNum().equals(busWare.getBatchNum()))
                        .collect(Collectors.toList());
                List<BusWare> outLibrarys = outLibraryList.stream().filter(busWare -> (storage.getBatchNum().equals(busWare.getBatchNum())
                        && storage.getBatchId().equals(busWare.getBatchId()) && storage.getWareId().equals(busWare.getWareId())))
                        .collect(Collectors.toList());
                List<BusWare> transfers = transferList.stream().filter(busWare -> storage.getBatchNum().equals(busWare.getBatchNum()))
                        .collect(Collectors.toList());
                List<BusWare> 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<BusWare> receipt=i<sales.size()?receiptGroupByConNumMap.get(sales.get(i).getConNum()):null;
                            if (CollectionUtils.isNotEmpty(receipt)){
                                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,
                                        initReceipt(receipt.get(0)),
                                        i < transfers.size() ? initTransfer(transfers.get(i)) : emptyTransfer,
                                        i < purchases.size() ? initPurchase(purchases.get(i)) : emptyPurchase));
                                b++;
                                receipt.remove(0);
                                receiptGroupByConNumMap.put(sales.get(0).getConNum(),receipt);
                                if(sales.size()>(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<String> 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<Object> getEmptyList(int size) {
        List<Object> 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<Object> addRow(List<Object> wareList, List<Object> paymentList, List<Object> salesList, List<Object> outLibrary,
                                       List<Object> receiptList, List<Object> transfer, List<Object> purchase) {
        List<Object> 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<Object> addRow(List<Object> wareList, List<Object> paymentList, List<Object> salesList, List<Object> outLibrary,
                                       List<Object> receiptList) {
        List<Object> result = new ArrayList<>();
        result.addAll(wareList);
        result.addAll(paymentList);
        result.addAll(salesList);
        result.addAll(outLibrary);
        result.addAll(receiptList);
        return result;
    }

    /**
     * 解析入库数据
     *
     * @return
     */
    private static List<Object> initStorage(BusWare busWare) {
        List<Object> 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<Object> initSelfStorage(BusWare busWare) {
        List<Object> 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<Object> initPayment(BusWare busWare) {
        List<Object> 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<Object> initSales(BusWare busWare) {
        List<Object> 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<Object> initSelfSales(BusWare busWare) {
        List<Object> 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<Object> initOutLibrary(BusWare busWare) {
        List<Object> result = new ArrayList<>();
        result.add(busWare.getOrderDate());  //出库日期
        result.add(busWare.getWareWeight()); //出库重量
        return result;
    }

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

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

    /**
     * 解析采购数据
     *
     * @param busWare
     * @return
     */
    private static List<Object> initPurchase(BusWare busWare) {
        List<Object> 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<List<Object>> values, XSSFCellStyle cellStyle) {
        if (values != null) {
            for (List<Object> 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
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 214,951评论 6 497
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,606评论 3 389
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 160,601评论 0 350
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,478评论 1 288
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,565评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,587评论 1 293
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,590评论 3 414
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,337评论 0 270
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,785评论 1 307
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,096评论 2 330
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,273评论 1 344
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,935评论 5 339
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,578评论 3 322
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,199评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,440评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,163评论 2 366
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,133评论 2 352

推荐阅读更多精彩内容