工具类,包含线程池,excel图片处理

一、线程池

public class ThreadPool {
 
    /**
     * 核心线程
     */
    public static final int CORE_POOL_SIZE = Runtime.getRuntime().availableProcessors() + 1;
 
    /**
     * 线程池最大线程数
     */
    public static final int MAX_POOL_SIZE = CORE_POOL_SIZE * 2;
 
    /**
     * 空闲线程回收时间
     */
    public static final int KEEP_ALIVE_TIME = 30;
 
    /**
     * 队列最大长度
     */
    public static final int QUEUE_CAPACITY = 128;
 
    private static final ExecutorService executorService =
            new ThreadPoolExecutor(CORE_POOL_SIZE,
                MAX_POOL_SIZE,
                KEEP_ALIVE_TIME,
                TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(QUEUE_CAPACITY),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.CallerRunsPolicy());
 
    private ThreadPool() {
 
    }
 
    public static void execute(Runnable task) {
        executorService.execute(task);
    }
 
    public static void shutdown() {
        if(!executorService.isShutdown()){
            executorService.shutdown();
        }
    }
 
    public static Future submit(Runnable task){
        return executorService.submit(task);
    }
 
    public static boolean cancel(Future future){
        if(future != null && !future.isCancelled()){
            return future.cancel(true);
        }
        return false;
    }
}

二、EasyExcel一列单元格导出多张图片

/**
     *
     * @param context
     * @param imageDataList 图片列表
     * @param imgColIndex 图片列
     */
    private void setImage(CellWriteHandlerContext context, List<ImageData> imageDataList, int imgColIndex) {
        Sheet sheet = context.getWriteSheetHolder().getSheet();
        Cell cell = context.getCell();
        // 图片数量
        int maxSize = imageDataList.size() > 1 ? imageDataList.size() : 1;
        // 根据图片数量设置图片列的宽度
        sheet.setColumnWidth(imgColIndex, (int)(18 * maxSize + 0.72) * 256);
        // 图片宽度,单位px,自己设置咯
        int picWidth = Units.pixelToEMU(125);
        Drawing drawing = sheet.getDrawingPatriarch();
        if (drawing == null) {
            drawing = sheet.createDrawingPatriarch();
        }
        for (int i = 0; i < imageDataList.size(); i++) {
            int index = sheet.getWorkbook().addPicture(imageDataList.get(i).getImage(), XSSFWorkbook.PICTURE_TYPE_PNG);
            // 设置图片坐标和位置 dx1, dy1, dx2, dy2, col1, row1, col2, row2
            // (dx1, dy1)是起始单元格图片左上角的坐标
            // (dx2, dy2)是结束单元格图片右下角的坐标
            // (col1, row1)是起始单元格位置
            // (col2, row2)是结束单元格位置
            ClientAnchor anchor = drawing.createAnchor(picWidth, 0, picWidth + picWidth * i,
                0,cell.getColumnIndex(), cell.getRowIndex(), cell.getColumnIndex(), cell.getRowIndex() + 1);
            // 设置图片可以随着单元格移动
            anchor.setAnchorType(ClientAnchor.AnchorType.MOVE_AND_RESIZE);
            drawing.createPicture(anchor, index);
        }
    }

相关推荐

  1. 工具包含线excel图片处理

    2024-05-01 15:14:01       12 阅读
  2. android 线的管理工具

    2024-05-01 15:14:01       33 阅读
  3. 线相关的学习

    2024-05-01 15:14:01       25 阅读
  4. python 多线处理图片

    2024-05-01 15:14:01       13 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-05-01 15:14:01       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-05-01 15:14:01       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-05-01 15:14:01       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-05-01 15:14:01       18 阅读

热门阅读

  1. json.parse(json.stringify)的弊端

    2024-05-01 15:14:01       10 阅读
  2. Element-UI 快速入门

    2024-05-01 15:14:01       11 阅读
  3. 前端html中iframe的基本使用

    2024-05-01 15:14:01       10 阅读
  4. 【笔试题汇总】华为春招笔试题题解 2024-3-20

    2024-05-01 15:14:01       12 阅读
  5. RocketMQ与Kafka深度对比:消息中间件的选择之战

    2024-05-01 15:14:01       11 阅读
  6. C#访问关键字this和base有什么作用

    2024-05-01 15:14:01       10 阅读
  7. UDP/TCP

    UDP/TCP

    2024-05-01 15:14:01      9 阅读