package com.malk.zyjn.utils;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.malk.utils.UtilFile;
import lombok.Builder;
import lombok.Data;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Value;

import javax.annotation.Nullable;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.InputStream;
import java.util.List;

@Builder
@Data
public class UtilExcel {
    /**
     * 列表与主表进行填充 [格式: 模板主表 {字段}, 列表 {.字段}]
     */
    @SneakyThrows
    public static void exportAllSheetByTemplate(Object dataMain, List dataList, @Nullable String fileName, String tableFilePath, String templateName) {
        InputStream inputStream = UtilFile.readPackageResource("templates/" + templateName);

        File file = new File(tableFilePath + fileName + ".xlsx");
        if (file.exists()) {
            file.delete();
        }

        ExcelWriter workBook = EasyExcel.write(tableFilePath + fileName + ".xlsx").withTemplate(inputStream).build();

        WriteSheet sheet = EasyExcel.writerSheet(0).build();
        // 先单组数据填充，再多组数据填充
        workBook.fill(dataMain, sheet);
        workBook.fill(dataList, sheet);

        workBook.finish();

    }

}
