easyexcel 我在导出千万级别的数据,选择inMemory = false 模式,但是水印图片加不上了

mec1mxoz  于 5个月前  发布在  其他
关注(0)|答案(1)|浏览(52)

我在导出千万级别的数据选择inMemory = false 模式,,使用分页导出,但是水印图片加不上了。有没有解决的办法呢。

y3bcpkx1

y3bcpkx11#

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.util.IOUtils;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class ExcelExporter {

    public static void main(String[] args) {
        String filePath = "output.xlsx";
        List<List<Object>> data = generateData(); // 生成数据

        ExcelWriter excelWriter = EasyExcel.write(filePath).build();
        WriteSheet writeSheet = EasyExcel.writerSheet("Sheet1").build();

        // 设置水印图片
        setWatermark(writeSheet);

        // 写入数据
        excelWriter.write(data, writeSheet);

        // 关闭 ExcelWriter
        excelWriter.finish();
    }

    private static List<List<Object>> generateData() {
        // 生成数据的逻辑
        List<List<Object>> data = new ArrayList<>();
        // 添加数据到列表
        return data;
    }

    private static void setWatermark(WriteSheet writeSheet) {
        // 水印图片路径
        String watermarkImagePath = "watermark.png";

        // 读取水印图片
        InputStream inputStream = null;
        byte[] watermarkBytes;
        try {
            inputStream = new FileInputStream(watermarkImagePath);
            watermarkBytes = IOUtils.toByteArray(inputStream);
        } catch (IOException e) {
            System.out.println("Failed to read watermark image: " + e.getMessage());
            return;
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    // Ignore
                }
            }
        }

        // 添加水印图片
        Drawing<?> drawing = writeSheet.getSheet().createDrawingPatriarch();
        CreationHelper helper = writeSheet.getWorkbook().getCreationHelper();
        ClientAnchor anchor = helper.createClientAnchor();
        anchor.setCol1(0);
        anchor.setCol2(7);
        anchor.setRow1(0);
        anchor.setRow2(5);
        drawing.createPicture(anchor, writeSheet.getWorkbook().addPicture(watermarkBytes, Workbook.PICTURE_TYPE_PNG));
    }
}

相关问题