package <%=packageName%>.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ZeroCopyHttpOutputMessage;
import org.springframework.http.server.reactive.ServerHttpResponse;

/**
 * 导出工具
 *
 */
public class ExportUtil {

    public static void excel(Workbook workbook, String filename, ServerHttpResponse response) throws IOException {
        ZeroCopyHttpOutputMessage zeroCopyResponse = (ZeroCopyHttpOutputMessage) response;
        response.getHeaders().set(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + new String(filename.getBytes(), StandardCharsets.ISO_8859_1));
        response.getHeaders().setContentType(MediaType.APPLICATION_OCTET_STREAM);
        File file = new File(new String(filename.getBytes(), StandardCharsets.ISO_8859_1));
        workbook.write(new FileOutputStream(file));
        zeroCopyResponse.writeWith(file, 0, file.length());
    }
}
