java 生成 .xls文件和压缩

/**
     * 创建消费折统计报表记录文件
     * 
     * @return
     */
    public boolean createMonthChargelogCountFile(Integer tenantId, Long  partner, List<Map<String, Object>> dataMaps, Long uid) {
        SXSSFWorkbook wb = null;
        try {
            wb = new SXSSFWorkbook();

            Sheet sheet = wb.createSheet("消费统计报表记录");
            sheet.createFreezePane(0, 1, 0, 1); // 冻结行

            Row row = sheet.createRow(0); // 创建行, 位于第0行
            ExcelStyle.setFirstRow(sheet, wb, row, 0, "商户号", 20);
            ExcelStyle.setFirstRow(sheet, wb, row, 1, "消费次数", 20);
            ExcelStyle.setFirstRow(sheet, wb, row, 2, "应缴金额", 20);
            ExcelStyle.setFirstRow(sheet, wb, row, 3, "已付金额", 20);
            ExcelStyle.setFirstRow(sheet, wb, row, 4, "折扣金额", 20);
//            ExcelStyle.setFirstRow(sheet, wb, row, 5, "差额", 20);
            
            CellStyle cellStyle = ExcelStyle.otherRowFont(wb, (byte) 2);
            //shopName,COUNT(id) as discountCount,SUM(saleMoney) as discountMoney
            int i = 1;
            Cell cell = null;
            String partnerID="";
            int consumeCount = 0;
            int consumeCountTotal = 0;
            double money = 0.0;
            double moneyTotal = 0.0;
            double chargeMoney = 0.0;
            double chargeMoneyTotal = 0.0;
            double saleMoney = 0.0;
            double saleMoneyTotal = 0.0;
            double differenceMoney = 0.0;
            double differenceMoneyTotal = 0.0;
            for (Map<String, Object> map : dataMaps) {
                row = sheet.createRow(i++); // 添加到wb中,从第二行开始
                partnerID= Const.getStr(map.get("partnerID"));
                
                consumeCount = Const.getInteger(map.get("consumeCount"));
                consumeCountTotal += consumeCount;
                
                money = Const.getDouble(map.get("money"));
                moneyTotal += money;
                
                chargeMoney = Const.getDouble(map.get("chargeMoney"));
                chargeMoneyTotal +=chargeMoney;
                
                saleMoney = Const.getDouble(map.get("saleMoney"));
                saleMoneyTotal +=saleMoney;
                
                differenceMoney = Const.getDouble(map.get("differenceMoney"));
                differenceMoneyTotal +=differenceMoney;
                


                cell = row.createCell(0);
                cell.setCellValue(partnerID); // 设置内容
                cell.setCellStyle(cellStyle); // 填充样式

                cell = row.createCell(1);
                cell.setCellValue(consumeCount); // 设置内容
                cell.setCellStyle(cellStyle); // 填充样式

                cell = row.createCell(2);
                cell.setCellValue(money); // 设置内容
                cell.setCellStyle(cellStyle); // 填充样式
                
                cell = row.createCell(3);
                cell.setCellValue(chargeMoney); // 设置内容
                cell.setCellStyle(cellStyle); // 填充样式
                
                cell = row.createCell(4);
                cell.setCellValue(saleMoney); // 设置内容
                cell.setCellStyle(cellStyle); // 填充样式
                
                //差额暂时不要
//                cell = row.createCell(5);
//                cell.setCellValue(differenceMoney); // 设置内容
//                cell.setCellStyle(cellStyle); // 填充样式
            }

            // 脚步样式
            CellStyle footCellStyle = ExcelStyle.footRowFont(wb);

            // 填充内容
            row = sheet.createRow(i++);
            Cell footCell = row.createCell(0);
            footCell.setCellValue("总计:");
            footCell.setCellStyle(footCellStyle);

            footCell = row.createCell(1);
            footCell.setCellValue(consumeCountTotal);
            footCell.setCellStyle(footCellStyle);

            footCell = row.createCell(2);
            footCell.setCellValue(moneyTotal);
            footCell.setCellStyle(footCellStyle);
            
            footCell = row.createCell(3);
            footCell.setCellValue(chargeMoneyTotal);
            footCell.setCellStyle(footCellStyle);
            
            footCell = row.createCell(4);
            footCell.setCellValue(saleMoneyTotal);
            footCell.setCellStyle(footCellStyle);
            
            //差额暂时不要
//            footCell = row.createCell(5);
//            footCell.setCellValue(differenceMoneyTotal);
//            footCell.setCellStyle(footCellStyle);

            // 保存文件
            String uuid = UUIDUtil.getUUID19();
            String savePath = terminalupload.getProperty("imgPath") + terminalupload.getProperty("statexport"); // 正式保存csv路径
            String unitName = "";
            Map<String, Object> punitMap = punitDBService.findOne( partner);
            if (punitMap != null && punitMap.size() > 0) {
                unitName = Const.getStr(punitMap.get("unitName"));
            }

            // 保存数据库记录
            String currentTime = DateUtil.getDate("", "yyyy-MM-dd HH-mm-ss");
            String dataPath = DateUtil.getDate(DateUtil.getTimeStampToDate(DateUtil.getTimeStampLong()), "yyMMdd") + "/";
            String downFileName = dataPath + unitName + " 消费记录统计报表[" + currentTime + "]" + ".xls";
            String fileUrl = savePath + downFileName; // 完整的下载路径

            // 保存文件
            File file = new File(savePath+dataPath);
            if (!file.exists()) {
                file.mkdirs();
            }

            FileOutputStream os = new FileOutputStream(fileUrl);//写入文件
            wb.write(os);
            os.close();
            
            String newFileName = fileUrl.replace(".xls", ".rar");
            boolean compress=  fileUploadUtil.compress(fileUrl,newFileName);//压缩文件
            if (compress) {
                File oldfile = new File(fileUrl);
                if (oldfile.exists()) {
                    oldfile.delete();
                }
            }
            String  fileName= downFileName.replace(".xls", ".rar");
            exportLogDBService.saveToId(new ExportLog(tenantId,  partner, uid, (byte) 36, uuid, fileName, fileName, (byte) 1)); // 保存记录
            return true;
        } catch (Exception e) {
            LOG.error("生成excel异常", e);
            return false;
        } finally {
            if (wb != null) {
                try {
                    wb.close();
                } catch (IOException e) {
                    LOG.error("关闭异常", e);
                }
            }
        }
        
        
    }

猜你喜欢

转载自blog.csdn.net/qq501569325/article/details/110518740