package com.gci.boot.modules.basedata.controller;

import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;

@Api(tags="导出模板")
@RestController
@RequestMapping("/basedata/exportExport")
@Slf4j
public class ExportController {

    //模板下载
    @RequestMapping("/getExcelModel")
    public void getExcelModel(String modelName, HttpServletResponse response) throws Exception {
    	try {
    		ClassPathResource xlsx = new ClassPathResource("excelTemplate/"+modelName);
            InputStream inputStream = xlsx.getInputStream();
            Workbook wb = new XSSFWorkbook(inputStream);
            String fileName = new String(modelName.getBytes(),"ISO8859-1");
            response.setContentType("application/vnd.ms-excel;charset=utf-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
            response.addHeader("Pargam", "no-cache");
            response.addHeader("Cache-Control", "no-cache");
            OutputStream os = response.getOutputStream();
            wb.write(os);
            os.flush();
            os.close();
            wb.close();
            inputStream.close();
        } catch (Exception e) {
            log.error("下载模版失败", e);
        }
        
    }
    
}
