导出数据到excel表格

导出数据

	@PostMapping("/commodityInventoryExportExcel")
    @ApiOperation("库存盘点导出(一次只能导出一条记录)")
    public void commodityInventoryExportExcel(@RequestParam Integer id, HttpServletResponse response){
        ExcelData data = new ExcelData();

        data.setName("库存盘点明细");

        List<String> titles = new ArrayList<>();
        titles.add("序号");
        titles.add("商品品类");
        titles.add("商品名称");
        titles.add("仓库");
        titles.add("库区");
        titles.add("库位");
        titles.add("单位");
        titles.add("原始数量");
        titles.add("盘点数量");
        titles.add("盘损");
        titles.add("盘溢");
        titles.add("成本价");
        titles.add("金额");
        titles.add("备注");
        titles.add("操作人");
        titles.add("盘点日期");
        titles.add("状态");
        data.setTitles(titles);

        List<CommodityInventoryDetail> commodityInventoryDetailList = commodityInventoryDetailService.getListByCommodityInventoryId(id);

        List<List<Object>> rows = new ArrayList<>();

        for (int i = 0; i < commodityInventoryDetailList.size(); i++) {
            List<Object> row = new ArrayList<>();
            row.add(i+1);
            row.add(commodityInventoryDetailList.get(i).getCategoriesName());//分类名
            row.add(commodityInventoryDetailList.get(i).getCommodityName());//商品名
            WarehouseManagement warehouseManagement = warehouseManagementService.findWarehouseByStorageDetailId(commodityInventoryDetailList.get(i).getWarehouseId());
            if (warehouseManagement!=null) {
                row.add(warehouseManagement.getWarehouseManagement().getWarehouseManagement().getName());//仓库
                row.add(warehouseManagement.getWarehouseManagement().getName());//库区
                row.add(warehouseManagement.getName());//库位
            }
            row.add(commodityInventoryDetailList.get(i).getUnit());//单位
            row.add(commodityInventoryDetailList.get(i).getOriginalNum());//原始数量
            row.add(commodityInventoryDetailList.get(i).getInventoryNum());//盘点数量
            row.add(commodityInventoryDetailList.get(i).getLost()==0?"":commodityInventoryDetailList.get(i).getLost());//盘损
            row.add(commodityInventoryDetailList.get(i).getOverFlow()==0?"":commodityInventoryDetailList.get(i).getOverFlow());//盘溢
            row.add(commodityInventoryDetailList.get(i).getCostPrice());//成本
            //金额
            if (commodityInventoryDetailList.get(i).getLost()!=0) {
                commodityInventoryDetailList.get(i).setSum((commodityInventoryDetailList.get(i).getLost())*commodityInventoryDetailList.get(i).getCostPrice());
            } else if (commodityInventoryDetailList.get(i).getOverFlow()!=0){
                commodityInventoryDetailList.get(i).setSum((commodityInventoryDetailList.get(i).getOverFlow())*commodityInventoryDetailList.get(i).getCostPrice());
            }
            row.add(commodityInventoryDetailList.get(i).getSum());//总金额

            row.add(commodityInventoryDetailList.get(i).getRemark());//备注
            row.add(commodityInventoryDetailList.get(i).getUpdateBy());//操作人
            row.add(commodityInventoryDetailList.get(i).getUpdateTime());//操作时间
            if(commodityInventoryDetailList.get(i).getStatus()==0){//状态
                row.add("");
            }else {
                row.add("已审核");
            }
            rows.add(i,row);
        }

        data.setRows(rows);
        try {
            ExcelUtils.exportExcel(data, response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

你可能感兴趣的:(java,java)