java如何加载文件中的图片_如何使用java读取Excel文件里图片和文字

本文介绍如何使用Easypoi读取Excel文件中的图片和文字。通过导入相关依赖,创建工具类ReadExcelUtils,读取Excel内的图片并保存为文件,然后读取文字信息。此外,提供了导出Excel和导入Excel的函数示例。
摘要由CSDN通过智能技术生成

在很多人脸识别和报表应用里面,批量导入是一个非常常见的需求,这里我分享一个读取使用POI和Easypoi读取Excel文件里面的图片以及文字的方法

导入easypoi的pom包

cn.afterturn

easypoi-base

3.2.0

cn.afterturn

easypoi-web

3.2.0

cn.afterturn

easypoi-annotation

3.2.0

2.书写按顺序读取excel内图片的工具类

import org.apache.poi.hssf.usermodel.*;

import org.apache.poi.ss.usermodel.PictureData;

import org.apache.poi.ss.usermodel.WorkbookFactory;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.InputStream;

import java.util.ArrayList;

import java.util.List;

// 用户读取用户上传Excel表格里面的图片

public class ReadExcelUtils {

public static ArrayList uploadPerson(String filePath) throws Exception{

InputStream inp = new FileInputStream(filePath);

HSSFWorkbook workbook = (HSSFWorkbook) WorkbookFactory.create(inp);

List pictures = workbook.getAllPictures();

HSSFSheet sheet = (HSSFSheet) workbook.getSheetAt(0);

ArrayList arrayList = new ArrayList<>();

for (HSSFShape shape: sheet.getDrawingPatriarch().getChildren()) {

HSSFClientAnchor anchor = (HSSFClientAnchor) shape.getAnchor();

if (shape instanceof HSSFPicture) {

HSSFPicture pic = (HSSFPicture) shape;

int row = anchor.getRow2();

int col = anchor.getCol2();

System.out.println("--->" + anchor.getRow2() + ":" + anchor.getCol2());

//map.put(row+":"+col, row+":"+col);

int pictureIndex = pic.getPictureIndex() - 1;

HSSFPictureData picData = pictures.get(pictureIndex);

byte[] data = picData.getData();

arrayList.add(data);

}

}

return arrayList;

}

public static void savePic(String i, PictureData pic) throws Exception {

String ext = pic.suggestFileExtension();

byte[] data = pic.getData();

if (ext.equals("jpeg")) {

FileOutputStream out = new FileOutputStream("D:\\excel\\" + i + ".jpg");

out.write(data);

out.close();

File file = new File("D:\\excel\\" + i + ".jpg");

FileInputStream in = new FileInputStream(file);

System.out.println("in===>" + in );

if (file.isFile()) {

file.delete();

System.out.println("=============delete");

}

}

}

}

3.按顺序读取完图片后,再按顺序读取excel内的文字信息

personDto dto类

@Excel(name = "姓名")

public String personName;

@Excel(name = "人员类型")

public String personType;

// 此字段用于存放图片的byte流,因此不需要加上@Excel

byte[] faceImage;

读取Excel内表格文字数据的函数

import cn.afterturn.easypoi.excel.ExcelExportUtil;

import cn.afterturn.easypoi.excel.ExcelImportUtil;

import cn.afterturn.easypoi.excel.entity.ExportParams;

import cn.afterturn.easypoi.excel.entity.ImportParams;

import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;

import org.apache.commons.lang3.StringUtils;

import org.apache.poi.ss.usermodel.Workbook;

import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

import java.io.File;

import java.io.IOException;

import java.net.URLEncoder;

import java.util.List;

import java.util.Map;

import java.util.NoSuchElementException;

/**

* @Description: 表格工具类

*/

public class PoiUtils {

public static void exportExcel(List < ? > list, String title, String sheetName, Class < ? > pojoClass,

String fileName, boolean isCreateHeader, HttpServletResponse response) {

ExportParams exportParams = new ExportParams(title, sheetName);

exportParams.setCreateHeadRows(isCreateHeader);

defaultExport(list, pojoClass, fileName, response, exportParams);

}

public static void exportExcel(List < ? > list, String title, String sheetName, Class < ? > pojoClass,

String fileName, HttpServletResponse response) {

defaultExport(list, pojoClass, fileName, response, new ExportParams(title, sheetName));

}

public static void exportExcel(List < Map < String, Object >> list, String fileName, HttpServletResponse response) {

defaultExport(list, fileName, response);

}

private static void defaultExport(List < ? > list, Class < ? > pojoClass, String fileName,

HttpServletResponse response, ExportParams exportParams) {

Workbook workbook = ExcelExportUtil.exportExcel(exportParams, pojoClass, list);

if (workbook != null);

downLoadExcel(fileName, response, workbook);

}

private static void downLoadExcel(String fileName, HttpServletResponse response, Workbook workbook) {

try {

response.setCharacterEncoding("UTF-8");

response.setHeader("content-Type", "application/vnd.ms-excel");

response.setHeader("Content-Disposition",

"attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));

workbook.write(response.getOutputStream());

} catch (IOException e) {

throw new RuntimeException(e.getMessage());

}

}

private static void defaultExport(List < Map < String, Object >> list, String fileName, HttpServletResponse response) {

Workbook workbook = ExcelExportUtil.exportExcel(list, ExcelType.HSSF);

if (workbook != null);

downLoadExcel(fileName, response, workbook);

}

/**

* 导入

* @param filePath

* @param titleRows

* @param headerRows

* @param pojoClass

* @param

* @return

*/

public static < T > List < T > importExcel(String filePath, Integer titleRows, Integer headerRows, Class <

T > pojoClass) {

if (StringUtils.isBlank(filePath)) {

return null;

}

ImportParams params = new ImportParams();

params.setTitleRows(titleRows);

params.setHeadRows(headerRows);

List < T > list = null;

try {

list = ExcelImportUtil.importExcel(new File(filePath), pojoClass, params);

} catch (NoSuchElementException e) {

throw new RuntimeException("模板不能为空");

} catch (Exception e) {

e.printStackTrace();

throw new RuntimeException(e.getMessage());

}

return list;

}

/**

* 导入表格

* @param file

* @param pojoClass

* @param

* @return

*/

public static < T > List < T > importExcel(MultipartFile file, Class < T > pojoClass) {

if (file == null) {

return null;

}

ImportParams params = new ImportParams();

try {

List < T > list = ExcelImportUtil.importExcel(file.getInputStream(), pojoClass, params);

return list;

} catch (NoSuchElementException e) {

throw new RuntimeException("excel文件不能为空");

} catch (Exception e) {

throw new RuntimeException(e.getMessage());

}

}

}

最后外层只需要这样调用即可组装成一个读取到excel图片byte流和文字的List了

// 读取Excel表格里面的所有图片的byte[]

FileUtils.byte2File(file.getBytes(),"D:\\excel\\excelFile.xls");

ArrayList bytes=ReadExcelUtils.uploadPerson("D:\\excel\\excelFile.xls");

// 读取Excel表格里面的所有基础数据

List result = PoiUtils.importExcel(file,PersonDto.class);

for (int i=0;i

result.get(i).setFaceImage(bytes.get(i));

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值