package com.inspect.metadata.prodevmnt.util;

import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ExportExcelUtil {
    private static final String FILE_PATH = "file/";

    public static void exportTemplate(HttpServletResponse response, String fileName) {
        try {
            InputStream resourceAsStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(FILE_PATH + fileName);
            XSSFWorkbook workbook = new XSSFWorkbook(resourceAsStream);
            response.reset();
            response.setCharacterEncoding("UTF-8");
            response.setContentType("multipart/form-data");
            response.setContentType("application/x-download");
            response.setHeader("Accept-Ranges", "bytes");
            response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(URLEncoder.encode(fileName, "UTF-8"), "ISO-8859-1"));
            OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());
            response.setContentType("application/octet-stream");
            workbook.write(outputStream);
            outputStream.flush();
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
