redis缓存导出excel

package com.maersk.newdragon.controller;

import com.maersk.newdragon.service.DownLoadFileService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Slf4j
@RestController
@RequestMapping("/downLoad")
public class DownLoadExcelController {

	@Autowired
	private DownLoadFileService downLoadFileService;

	@RequestMapping(value = "/reportExcel")
	public void downLoadReportExcel(HttpServletRequest request, HttpServletResponse response,String type,String ids) {
		downLoadFileService.downLoadReportExcel(request, response,type,ids);
	}
}

package com.maersk.newdragon.service;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.maersk.common.core.util.IOUtil;
import com.maersk.common.dao.entity.NdLinertrackingctnrinfo;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
@AllArgsConstructor
public class DownLoadFileService {

	private static final String ddlist = "ddlist";
	private static final String pierlist = "pierlist";
	private static final String outerWarehouselist = "outerWarehouselist";
	private static final String dndFeeList = "dndFeeList";
	private final StringRedisTemplate stringRedisTemplate;

	public void downLoadReportExcel(HttpServletRequest request, HttpServletResponse response, String type, String ids) {
		ServletOutputStream outputStream = null;
		List<NdLinertrackingctnrinfo> businessFlows = null;
		List<NdLinertrackingctnrinfo> businessFlows1 = new ArrayList<>();
		try {
			outputStream = response.getOutputStream();
			//IOUtil.setFileDownloadHeader(response, "qqq.xlsx");

			if (type.equals("1")) {//DnD订阅箱信息表
				IOUtil.setFileDownloadHeader(response, "DnDCostMonitoring.xlsx");
				businessFlows = JSON.parseArray(stringRedisTemplate.opsForValue().get(ddlist), NdLinertrackingctnrinfo.class);
			} else if (type.equals("2")) {//码头订阅箱信息表
				IOUtil.setFileDownloadHeader(response, "TerminalManagement.xlsx");
				businessFlows = JSON.parseArray(stringRedisTemplate.opsForValue().get(pierlist), NdLinertrackingctnrinfo.class);
			} else if (type.equals("3")) {//外仓订阅箱信息表
				IOUtil.setFileDownloadHeader(response, "ExternalWarehouseManagement.xlsx");
				businessFlows = JSON.parseArray(stringRedisTemplate.opsForValue().get(outerWarehouselist), NdLinertrackingctnrinfo.class);
			} else if (type.equals("4")) {
				IOUtil.setFileDownloadHeader(response, "DnDExpenseReport.xlsx");
				businessFlows = JSON.parseArray(stringRedisTemplate.opsForValue().get(dndFeeList), NdLinertrackingctnrinfo.class);
			}
			//通过id获取值
			String[] id = ids.split(",");
			for (int j = 0; j < id.length; j++) {
				for (int i = 0; i < businessFlows.size(); i++) {
					//将传过的id跟redis对比
					if (businessFlows.get(i).getId().longValue() == Long.parseLong(id[j])) {
						businessFlows1.add(businessFlows.get(i));
					}
				}
			}
			Workbook workbook = createWb(businessFlows1);
			workbook.write(outputStream);
			outputStream.flush();
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		} finally {
			IOUtil.close(outputStream);
		}
	}

	private Workbook createWb(List<NdLinertrackingctnrinfo> businessFlows) {
		Workbook wb = new XSSFWorkbook();
		Sheet sheet = wb.createSheet("sheet1");
		String[] header = new String[]{"免堆期", "免箱期", "异常提醒", "备注", "费用结算方", "箱型", "码头", "离开码头堆场时间", "码头总存放天数", "用箱总天数", "还空时间", "外堆场", "进入外堆场时间", "离开外堆场时间", "外堆场总堆存天数", "码头堆存费", "外堆场堆存费"};
		// step1=============创建设置表头==========================
		Row row = sheet.createRow(0);
		for (int i = 0; i < header.length; i++) {
			Cell cell = row.createCell(i);
			cell.setCellValue(header[i]);
		}

		if (CollectionUtils.isEmpty(businessFlows)) return wb;

		// step2=============创建数据列表==========================
		for (int i = 0; i < businessFlows.size(); i++) {
			row = sheet.createRow(i + 1);
			NdLinertrackingctnrinfo info = businessFlows.get(i);
			for (int j = 0; j < header.length; j++) {
				Cell cell = row.createCell(j);
				switch (j) {
					case 0:
						cell.setCellValue(info.getStorage() == null ? "null" : info.getStorage());//免堆期
						break;
					case 1:
						cell.setCellValue(info.getFreeBoxs() == null ? "null" : info.getFreeBoxs().toString());//免箱期
						break;
					case 2:
						cell.setCellValue(info.getErrorMsg() == null ? "" : info.getErrorMsg());//异常提醒
						break;
					case 3:
						cell.setCellValue(info.getRemarks() == null ? "" : info.getRemarks());//备注
						break;
					case 4:
						cell.setCellValue(info.getSettlementParty() == null ? "" : info.getSettlementParty());//费用结算方
						break;
					case 5:
						cell.setCellValue(info.getCtnrCode() == null ? "" : info.getCtnrCode());//箱型
						break;
					case 6:
						cell.setCellValue(info.getDtp() == null ? "" : info.getDtp());//港口
						break;
					case 7:
						cell.setCellValue(info.getSTCSstatustime() == null ? "" : info.getSTCSstatustime());//离开码头堆场时间
						break;
					case 8:
						cell.setCellValue(info.getStorageDays() == null ? "" : info.getStorageDays().toString());//码头总存放天数
						break;
					case 9:
						cell.setCellValue(info.getBoxDays() == null ? "" : info.getBoxDays().toString());//用箱总天数
						break;
					case 10:
						cell.setCellValue(info.getStatustime() == null ? "" : info.getStatustime());//还空时间
						break;
					case 11:
						cell.setCellValue(info.getDtpCode() == null ? "" : info.getDtpCode());//外堆场
						break;
					case 12:
						cell.setCellValue(info.getDSCHstatustime() == null ? "" : info.getDSCHstatustime());//进入外堆场时间
						break;
					case 13:
						cell.setCellValue(info.getSTCSstatustime() == null ? "" : info.getSTCSstatustime());//离开外堆场时间
						break;
					case 14:
						cell.setCellValue(info.getOuterYardDays() == null ? "" : info.getOuterYardDays().toString());//外堆场总堆存天数
						break;
					case 15:
						cell.setCellValue(info.getPortFee() == null ? "" : info.getPortFee().toString());//码头堆存费
						break;
					case 16:
						cell.setCellValue(info.getOutsideYardFee() == null ? "" : info.getOutsideYardFee().toString());//外堆场堆存费
						break;
					default:
						break;
				}
			}
		}
		return wb;
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值