package com.manage.controller;

import com.manage.service.cache.CacheManager;
import com.manage.service.ImportExcel.ImportExcelUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;

/**
 * @ProjectName:
 * @Description:
 * @Param 传输参数
 * @Return
 * @Author: 曾文和
 * @CreateDate: 2019/10/12 12:20
 * @UpdateUser: 曾文和
 * @UpdateDate: 2019/10/12 12:20
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
@Controller
public class ExportExcelController {
    @RequestMapping(value="exportWrongExcel")
    public void exportWrongExcel(String workBookKey,String fileName, HttpServletResponse response){
        OutputStream os = null;
        if(StringUtils.isNoneBlank(fileName)){
            //文件名
            fileName = "导入"+fileName+"时出错数据列表.xls";
            try {
                //导出excel的操作
                Workbook workbook = ImportExcelUtil.getWorkBookMapByKey(workBookKey);
                os = response.getOutputStream();
                response.reset();
                response.setContentType("application/OCTET-STREAM;charset=gbk");
                response.setHeader("pragma", "no-cache");
                fileName = new String(fileName.getBytes("utf-8"), "iso-8859-1");
                response.setHeader("Content-disposition", "attachment;filename=\"" + fileName + "\"");
                workbook.write(os);
                CacheManager.addExcCount("noExc");
            }catch (Exception e){
                CacheManager.addExcCount("exc");
                e.printStackTrace();
            }finally {
                if(os != null){
                    try {
                        os.close();
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}
