使用easyexcel将图片批量写入excel

public class Tupian {
    public static void main(String[] args) throws MalformedURLException {
        String fileName = "E:\\image.xlsx";
        String path = "D:\\2022\\1022\\";
        File file = new File(path);
        System.out.println(file.isDirectory());
        ArrayList list = new ArrayList<>();

        File[] files = file.listFiles();
        for (File image : files) {
//            System.out.println(image.getName());
            if(image.getName().endsWith(".jpg")){
//                list.add(image);
                ImageData imageData = new ImageData();
                imageData.setUrl(new URL("file:/D:\\2022\\1022\\"+image.getName()));
                list.add(imageData);
            }
//
        }
        EasyExcel.write(fileName, ImageData.class).sheet().doWrite(list);
    }

    public static void test01() throws MalformedURLException {
        //生成excel的路径
        String fileName = "E:\\image.xlsx";
        List list = new ArrayList();
        ImageData imageData = new ImageData();
        list.add(imageData);
        //放你的图片url地址
//        imageData.setUrl(new URL(      "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fbpic.wotucdn.com%2F18%2F93%2F18%2F18931874-bbfd4c21f0421f2dc034a85219c232d6-0.jpg&refer=http%3A%2F%2Fbpic.wotucdn.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1621675114&t=c6327627920bcb4020f2b77fee84c91e"));
        imageData.setUrl(new URL("file:/D:\\2022\\1022\\2022000001001.jpg"));
//        EasyExcel.write(fileName, ImageData.class).sheet().doWrite(list);

    }
}

/**
 * @author huiXing
 * @version 1.0.0
 * @description 测试图片
 */
@Data
@ContentRowHeight(100)
@ColumnWidth(100 / 8)
class ImageData {
    // private File file;
    //  private InputStream inputStream;
    /**
     * 如果string类型 必须指定转换器,string默认转换成string
     */
    // @ExcelProperty(converter = StringImageConverter.class)
    //  private String string;
    // private byte[] byteArray;

    /**
     * 根据url导出
     * @since 2.1.1
     */
    private URL url;
}

你可能感兴趣的:(springboot,java,servlet,jvm)