文章目录
- 基于PIO把带有数据和图片的excel表导入到数据库中(图片存储到服务器中,可访问图片路径存入在数据库中)
- 前言
- 创作来之不易可点一颗小心心
- 一、工具类getExcelMap说明?
- 二、使用步骤
- 1.引入库
- 2.读excel中的数据和图片
- 2.ExcelUtil:获取图片和存图片
- 总结
前言 创作来之不易可点一颗小心心 一、工具类getExcelMap说明?
以下工具类都是基于PIO原生态实现的,其中包含了.xls和.xlsx的导入,也涉及到了在excel解析图片。
二、使用步骤 1.引入库2.读excel中的数据和图片org.apache.poi poi-ooxml 4.1.0
代码如下(示例):
public List2.ExcelUtil:获取图片和存图片
public class ExcelUtil{
public static Map getPictures(HSSFSheet sheet) {
Map map = new HashMap<>();
List list = sheet.getDrawingPatriarch().getChildren();
for (HSSFShape shape : list) {
if (shape instanceof HSSFPicture) {
HSSFPicture picture = (HSSFPicture) shape;
HSSFClientAnchor cAnchor = (HSSFClientAnchor) picture.getAnchor();
PictureData pData = picture.getPictureData();
String key = cAnchor.getRow1() + "_" + cAnchor.getCol1();
map.put(key, pData);
}
}
return map;
}
public static Map getPictures (XSSFSheet sheet) throws IOException {
Map map = new HashMap<>();
for (POIXMLDocumentPart dr : sheet.getRelations()) {
if (dr instanceof XSSFDrawing) {
XSSFDrawing drawing = (XSSFDrawing) dr;
List shapes = drawing.getShapes();
for (XSSFShape shape : shapes) {
XSSFPicture pic = (XSSFPicture) shape;
XSSFClientAnchor anchor = (XSSFClientAnchor) shape.getAnchor();
CTMarker ctMarker = anchor.getFrom();
String picIndex = ctMarker.getRow() + "_" + ctMarker.getCol();
map.put(picIndex, pic.getPictureData());
}
}
}
return map;
}
//图片写出
public static Map printImg(Map mapList,String filePath,String webPath) throws IOException {
Map map = new HashMap<>();
mapList.forEach((key, pic) -> {
// 文件扩展名
String ext = pic.suggestFileExtension();
// 图片数据
byte[] data = pic.getData();
//图片保存路径 医院名称+微信公众号/医院照片+图片后缀
String ImgName= UUID.randomUUID().toString()+"." + ext;
//写入到对应的文件夹路径下面去
//filePath+imgName=/aaa/bbb/xxxxx.jpg
try (FileOutputStream out = new FileOutputStream(filePath+imgName)) {
out.write(bytes);
} catch (Exception e) {
e.printStackTrace();
}
//map中的value为:http://localhost:8080/aaa/bbb/xxxxx.jpg
map.put(key, webPath+ImgPath);
});
return map;
}
}
}
总结
由于List



