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

一、线程池

java 复制代码
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一列单元格导出多张图片

java 复制代码
/**
     *
     * @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);
        }
    }
相关推荐
2501_930707788 小时前
使用C#代码设置 Excel 单元格的背景颜色和图案
excel
ms365copilot8 小时前
Excel Copilot聊天历史|关闭文件后,还能找回之前的AI分析对话
人工智能·excel·copilot
SL-staff9 小时前
规则引擎如何实现风控报告的自动化?JVS-Rules提供解决方案
大数据·自动化·excel·规则引擎·jvs-rules·风控报告·数据溯源
2501_9336707910 小时前
内容电商运营岗位能力模型:SQL、Excel、投放数据与AI工具怎么准备
人工智能·sql·excel
AIGS00110 小时前
工艺参数散在Excel和纸质文件里,能不能统一管、随查随用
服务器·数据库·excel·经验沉淀·知识管理·工艺知识·本体语义
小刘在重生~11 小时前
Django|Excel 批量上传、Form/ModelForm 文件上传、Media 媒体文件配置
python·django·excel
古少侠11 小时前
用 DS随心转 把 AI 给的表格导出成能筛选、能统计的 Excel
人工智能·excel
2601_9622953312 小时前
Python自动化批量文档生成工具:Word与Excel的结合应用
python·自动化·word·excel·文档生成
昵称画2 天前
POC验证怎么设计用例?不走过程的实操要点
大数据·数据库·人工智能·低代码·excel
Wang's Blog2 天前
Vibe Coding一人即团队系列62: Excel数据分析与可视化自动化
数据分析·自动化·excel