package cn.stylefeng.team.project.modular.filing.util;

import cn.hutool.core.io.IoUtil;
import com.aspose.cells.*;
import lombok.extern.slf4j.Slf4j;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;

@Slf4j
public class ExcelToImageUtil {

    public static byte[] parseExcelFileToPng(InputStream inputStream) {

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

        try {

            Workbook workbook = new Workbook(inputStream);

            Worksheet worksheet = workbook.getWorksheets().get(0);

            ImageOrPrintOptions options = new ImageOrPrintOptions();
            options.setOnePagePerSheet(true);
            options.setImageType(ImageType.PNG);

            SheetRender sheetRender = new SheetRender(worksheet, options);

            sheetRender.toImage(0, outputStream);
            return outputStream.toByteArray();

        } catch (Exception e) {
            log.error("解析Excel文件失败", e);
            return null;
        } finally {
            IoUtil.close(outputStream);
            IoUtil.close(inputStream);
        }

    }
}