用poi写一个三层嵌套的表格数据封装并下载

  public void exportPurchaseDetails(List pmsCompareRepList, String excelName) throws IOException {
        Workbook workbook = new XSSFWorkbook();
        FileOutputStream outputStream = new FileOutputStream(excelName);
        try {
            Sheet sheet = workbook.createSheet("采购明细");
            // 设置列宽
            sheet.setColumnWidth(0, 3000);
            sheet.setColumnWidth(1, 2500);
            sheet.setColumnWidth(2, 3500);
            sheet.setColumnWidth(3, 2000);
            sheet.setColumnWidth(4, 2000);
            sheet.setColumnWidth(5, 2000);
            sheet.setColumnWidth(6, 2000);
            sheet.setColumnWidth(7, 1500);
            sheet.setColumnWidth(8, 1500);
            sheet.setColumnWidth(9, 1500);
            sheet.setColumnWidth(10, 1500);
            sheet.setColumnWidth(11, 1500);
            sheet.setColumnWidth(12, 1500);
            sheet.setColumnWidth(13, 2000);
            sheet.setColumnWidth(14, 1500);
            sheet.setColumnWidth(15, 1500);
            sheet.setColumnWidth(16, 2000);
            sheet.setColumnWidth(17, 3000);
            sheet.setColumnWidth(18, 1500);
            sheet.setColumnWidth(19, 1500);
            sheet.setColumnWidth(20, 3000);
            sheet.setColumnWidth(21, 1500);
            sheet.setColumnWidth(22, 1500);
            sheet.setColumnWidth(23, 1500);

            // 创建样式
            CellStyle style = workbook.createCellStyle();
            style.setAlignment(HorizontalAlignment.CENTER);
            style.setVerticalAlignment(VerticalAlignment.CENTER);
            style.setWrapText(true);
            //表格首行的高度
            int height = 0;
            for (PmsCompareRepVO pmsCompareRepVO : pmsCompareRepList) {
                PmsPayPrintInfoVO printInfoVO = pmsCompareRepVO.getPrintInfoVO();
                // 创建表头
                Row row1 = sheet.createRow(0 + height);
                row1.setHeight((short) 1500);
                CellStyle headStyle = workbook.createCellStyle();
                headStyle.setAlignment(HorizontalAlignment.CENTER);
                headStyle.setVerticalAlignment(VerticalAlignment.CENTER);
                Font headFont = workbook.createFont();
                headFont.setFontHeightInPoints((short) 20);
                headStyle.setFont(headFont);
                Cell cell1 = row1.createCell(0);
                cell1.setCellValue("采购明细");
                sheet.addMergedRegion(new CellRangeAddress(0 + height, 0 + height, 0, 23));
                cell1.setCellStyle(headStyle);

                Row row2 = sheet.createRow(1 + height);
                row2.setHeight((short) 700);
                Cell cell2 = row2.createCell(0);
                cell2.setCellValue("采购单情况");
                sheet.addMergedRegion(new CellRangeAddress(1 + height, 1 + height, 0, 6));
                cell2.setCellStyle(style);

                Cell cell3 = row2.createCell(7);
                cell3.setCellValue("入库情况");
                sheet.addMergedRegion(new CellRangeAddress(1 + height, 1 + height, 7, 10));
                cell3.setCellStyle(style);

                Cell cell4 = row2.createCell(11);
                cell4.setCellValue("退货情况");
                sheet.addMergedRegion(new CellRangeAddress(1 + height, 1 + height, 11, 14));
                cell4.setCellStyle(style);

                Cell cell5 = row2.createCell(15);
                cell5.setCellValue("付款情况");
                sheet.addMergedRegion(new CellRangeAddress(1 + height, 1 + height, 15, 20));
                cell5.setCellStyle(style);

                Cell cell6 = row2.createCell(21);
                cell6.setCellValue("开票情况");
                sheet.addMergedRegion(new CellRangeAddress(1 + height, 1 + height, 21, 23));
                cell6.setCellStyle(style);

                // 创建表头
                Row row3 = sheet.createRow(2 + height);
                row3.setHeight((short) 1200);
                String[] headers = {"采购单号", "SKU编码", "商品名称", "采购数量", "采购单价", "采购总价", "剩余入库数量", "入库日期", "入库数量", "入库单价", "入库金额", "退货日期", "退货数量", "退货单价", "退货金额", "付款日期", "已付款金额(不含本次)", "本次付款金额", "扣款日期", "扣款金额", "剩余未付金额", "开票日期", "发票号码", "开票金额"};

                for (int i = 0; i < headers.length; i++) {
                    Cell cell = row3.createCell(i);
                    cell.setCellValue(headers[i]);
                    cell.setCellStyle(style);
                }

                int rowIndex = 3 + height;
                //记录第一次的行数
                int rowIndex2 = rowIndex;
                //记录sku详情第一次的行数
                int rowIndex3 =rowIndex;
                //填充数据
//                List pmsCompareSkuList = pmsCompareRepVO.getPmsCompareSkuList();
                List skuInfos = pmsCompareRepVO.getPrintInfoVO().getSkuInfos();
                //遍历sku
                for (PmsPayPrintInfoVO.SkuInfo skuInfo : skuInfos) {
                    List infos = skuInfo.getInfos();
                    int j;
                    if (infos.size()!=0&&null!=infos.get(0).getInStockNum()){
                        j=infos.size();
                    }else {
                        j=1;
                    }

                    for (int i = 0; i < j; i++) {
                        Row row = sheet.createRow(rowIndex);
                        row.setHeight((short) 1500);
                        if (infos.size()!=0&&null!=infos.get(0).getInStockNum()){
                            PmsPayPrintInfoVO.SkuInfo.Info info = infos.get(i);
                            for (int f = 0; f < 24; f++) {
                                Cell cell = row.createCell(f);
                                cell.setCellStyle(style);
                                if (f % 24 == 0) {
                                    cell.setCellValue(pmsCompareRepVO.getOrderId());
                                } else if (f % 24 == 1) {
                                    cell.setCellValue(skuInfo.getSpecode());
                                } else if (f % 24 == 2) {
                                    cell.setCellValue(skuInfo.getTitle());
                                } else if (f % 24 == 3) {
                                    cell.setCellValue(skuInfo.getPurchaseNum().doubleValue());
                                } else if (f % 24 == 4) {
                                    cell.setCellValue(skuInfo.getPurchasePrice().doubleValue());
                                } else if (f % 24 == 5) {
                                    cell.setCellValue(skuInfo.getPurchaseTotal().doubleValue());
                                } else if (f % 24 == 6) {
                                    cell.setCellValue(skuInfo.getRemainingInventoryQuantity().doubleValue());
                                } else if (f % 24 == 7) {
                                    cell.setCellValue(info.getInStockTimeStr());
                                } else if (f % 24 == 8) {
                                    cell.setCellValue(info.getInStockNum().doubleValue());
                                } else if (f % 24 == 9) {
                                    cell.setCellValue(info.getInStockPrice().doubleValue());
                                } else if (f % 24 == 10) {
                                    cell.setCellValue(info.getInStockTotal().doubleValue());
                                } else if (f % 24 == 11&&null!=info.getRtnStockTimeStr()) {
                                    cell.setCellValue(info.getRtnStockTimeStr());
                                } else if (f % 24 == 12&&null!=info.getRtnStockTimeStr()) {
                                    cell.setCellValue(info.getRtnStockNum().doubleValue());
                                } else if (f % 24 == 13&&null!=info.getRtnStockTimeStr()) {
                                    cell.setCellValue(info.getRtnStockPrice().doubleValue());
                                } else if (f % 24 == 14&&null!=info.getRtnStockTimeStr()) {
                                    cell.setCellValue(info.getRtnStockTotal().doubleValue());
                                } else if (f % 24 == 15&&null!=info.getPaymentTimeStr()) {
                                    cell.setCellValue(info.getPaymentTimeStr());
                                } else if (f % 24 == 16&&null!=info.getPaymentTimeStr()) {
                                    cell.setCellValue(info.getAlreadyPaymentTotal().doubleValue());
                                } else if (f % 24 == 17 && rowIndex == rowIndex2) {
                                    cell.setCellValue((pmsCompareRepVO.getPayCount().doubleValue()));
                                } else if (f % 24 == 18 && null!=info.getBuckleTimeStr()) {
                                    cell.setCellValue(info.getBuckleTimeStr());
                                } else if (f % 24 == 19 && null!=info.getBuckleTotal()) {
                                    cell.setCellValue(info.getBuckleTotal().doubleValue());
                                } else if (f % 24 == 20) {
                                    cell.setCellValue(printInfoVO.getIngPaymentTotalSum().doubleValue());
                                }else if (f % 24 == 21&&null!=info.getInvoiceCreatedStr()) {
                                    cell.setCellValue(info.getInvoiceCreatedStr());
                                }else if (f % 24 == 22&&null!=info.getInvoiceNo()) {
                                    cell.setCellValue(info.getInvoiceNo());
                                }else if (f % 24 == 23&&null!=info.getInvoiceAmount()) {
                                    cell.setCellValue(info.getInvoiceAmount().doubleValue());
                                }
                            }
                        }else {
                            for (int f = 0; f < 24; f++) {
                                Cell cell = row.createCell(f);
                                cell.setCellStyle(style);
                                if (f % 24 == 0) {
                                    cell.setCellValue(pmsCompareRepVO.getOrderId());
                                } else if (f % 24 == 1) {
                                    cell.setCellValue(skuInfo.getSpecode());
                                } else if (f % 24 == 2) {
                                    cell.setCellValue(skuInfo.getTitle());
                                } else if (f % 24 == 3) {
                                    cell.setCellValue(skuInfo.getPurchaseNum().doubleValue());
                                } else if (f % 24 == 4) {
                                    cell.setCellValue(skuInfo.getPurchasePrice().doubleValue());
                                } else if (f % 24 == 5) {
                                    cell.setCellValue(skuInfo.getPurchaseTotal().doubleValue());
                                } else if (f % 24 == 6) {
                                    cell.setCellValue(skuInfo.getRemainingInventoryQuantity().doubleValue());
                                } else if (f % 24 == 17 && rowIndex == rowIndex2) {
                                    cell.setCellValue((pmsCompareRepVO.getPayCount().doubleValue()));
                                } else if (f % 24 == 20) {
                                    cell.setCellValue(printInfoVO.getIngPaymentTotalSum().doubleValue());
                                }
                            }
                        }
                        rowIndex++;
                    }
                    //合并单个sku的几行
                    if (infos.size()>1){
                        sheet.addMergedRegion(new CellRangeAddress(rowIndex3, rowIndex - 1, 1, 1));
                        sheet.addMergedRegion(new CellRangeAddress(rowIndex3, rowIndex - 1, 2, 2));
                        sheet.addMergedRegion(new CellRangeAddress(rowIndex3, rowIndex - 1, 3, 3));
                        sheet.addMergedRegion(new CellRangeAddress(rowIndex3, rowIndex - 1, 4, 4));
                        sheet.addMergedRegion(new CellRangeAddress(rowIndex3, rowIndex - 1, 5, 5));
                        sheet.addMergedRegion(new CellRangeAddress(rowIndex3, rowIndex - 1, 6, 6));
                    }
                    //再次赋值
                    rowIndex3=rowIndex;
                }
                if (skuInfos.size() > 1||skuInfos.get(0).getInfos().size()>1) {
                    sheet.addMergedRegion(new CellRangeAddress(rowIndex2, rowIndex - 1, 0, 0));
                    sheet.addMergedRegion(new CellRangeAddress(rowIndex2, rowIndex - 1, 20, 20));
                }
                //填充最后一行
                // 添加统计行
                Row totalRow = sheet.createRow(rowIndex);
                totalRow.setHeight((short) 750);
                Cell totalCell1 = totalRow.createCell(0);
                totalCell1.setCellValue("合计");
                sheet.addMergedRegion(new CellRangeAddress(rowIndex, rowIndex, 0, 2));
                totalCell1.setCellStyle(style);

                Cell totalCell2 = totalRow.createCell(3);
                totalCell2.setCellValue(printInfoVO.getPurchaseNumSum().doubleValue());
                totalCell2.setCellStyle(style);

                Cell totalCell3 = totalRow.createCell(5);
                totalCell3.setCellValue(printInfoVO.getPurchaseTotalSum().doubleValue());
                totalCell3.setCellStyle(style);

                Cell totalCell4 = totalRow.createCell(8);
                totalCell4.setCellValue(printInfoVO.getInStockNumSum().doubleValue());
                totalCell4.setCellStyle(style);

                Cell totalCell5 = totalRow.createCell(10);
                totalCell5.setCellValue(printInfoVO.getInStockTotalSum().doubleValue());
                totalCell5.setCellStyle(style);

                Cell totalCell6 = totalRow.createCell(12);
                totalCell6.setCellValue(printInfoVO.getRtnStockNumSum().doubleValue());
                totalCell6.setCellStyle(style);

                Cell totalCell7 = totalRow.createCell(14);
                totalCell7.setCellValue(printInfoVO.getRtnStockTotalSum().doubleValue());
                totalCell7.setCellStyle(style);

                Cell totalCell8 = totalRow.createCell(16);
                totalCell8.setCellValue(printInfoVO.getAlreadyPaymentTotalSum().doubleValue());
                totalCell8.setCellStyle(style);

                Cell totalCell9 = totalRow.createCell(17);
                totalCell9.setCellValue(printInfoVO.getIngPaymentTotalSum().doubleValue());
                totalCell9.setCellStyle(style);

                Cell totalCell10 = totalRow.createCell(19);
                totalCell10.setCellValue(printInfoVO.getBuckleTotalSum().doubleValue());
                totalCell10.setCellStyle(style);

                Cell totalCell11 = totalRow.createCell(20);
                totalCell11.setCellValue(printInfoVO.getRemainingUnAmountSum().doubleValue());
                totalCell11.setCellStyle(style);

                Cell totalCell12 = totalRow.createCell(23);
                totalCell12.setCellValue(printInfoVO.getInvoiceAmountSum().doubleValue());
                totalCell12.setCellStyle(style);

                height=rowIndex+2;

            }
            // 输出文件
            workbook.write(outputStream);
        } catch (Exception e) {
            log.error("导出采购明细出错{}", e);
            throw new RuntimeException(e);
        } finally {
            workbook.close();
            outputStream.close();
        }

    }

你可能感兴趣的:(excel)