项目中有一个要求:您需要在excel中阅读带有图片的信息,并且需要与每一行中的图片相对应。排序方法如下:
主要逻辑:
1,获取excel对象,
2,在excel中读取所有图片流,将图片的位置和图片流存储在地图的getPictures()方法中

4。根据图像的位置,获取图像流,上传图像,并在地图的printImg()方法中存储图像的位置和上传路径
5,在excel中读取每一行文本,当读取图片列时,根据键获取映射第四步中的值,并将路径和文本信息存储在列表中; readData()方法
控制器层

@PostMapping("/uploadFile")
public List2.服务层
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.poi.ss.usermodel.PictureData;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import com.qskj.framework.config.ERPConfig;
public class ExcelUtil {
/**
* 读取 Excel文件内容
*
* @param inputstream 文件输入流
* @return
* @throws Exception
*/
public static List> readExcelByInputStream(InputStream inputstream, String providerId)
throws Exception {
// 结果集
List> list = new ArrayList>();
XSSFWorkbook wb = new XSSFWorkbook(inputstream);
String filePath = ERPConfig.getProfile() + "/" + "pic/" + providerId + "/";//图片保存路径
final XSSFSheet sheet = wb.getSheetAt(0);// 得到Excel工作表对象
Map map = ExcelImgUtil.getPictures(sheet);//获取图片和位置
Map pathMap = ExcelImgUtil.printImg(map, filePath);//写入图片,并返回图片路径,key:图片坐标,value:图片路径
list = ExcelImgUtil.readData(sheet, pathMap,providerId);
return list;
}
}

3,工具
package com.qskj.project.util;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.poi.POIXMLDocumentPart;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.PictureData;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
import org.apache.poi.xssf.usermodel.XSSFDrawing;
import org.apache.poi.xssf.usermodel.XSSFPicture;
import org.apache.poi.xssf.usermodel.XSSFShape;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTMarker;
import com.qskj.common.utils.security.Md5Utils;
import com.qskj.framework.config.ERPConfig;
public class ExcelImgUtil {
private static int counter = 0;
/**
* 获取图片和位置 (xlsx)
*
* @param sheet
* @return
* @throws IOException
*/
public static Map getPictures(XSSFSheet sheet) throws IOException {
Map map = new HashMap();
List list = sheet.getRelations();
for (POIXMLDocumentPart part : list) {
if (part instanceof XSSFDrawing) {
XSSFDrawing drawing = (XSSFDrawing) part;
List shapes = drawing.getShapes();
for (XSSFShape shape : shapes) {
XSSFPicture picture = (XSSFPicture) shape;
XSSFClientAnchor anchor = picture.getPreferredSize();
CTMarker marker = anchor.getFrom();
String key = marker.getRow() + "-" + marker.getCol();
byte[] data = picture.getPictureData().getData();
map.put(key, picture.getPictureData());
}
}
}
return map;
}
public static Map printImg(Map sheetList, String path) throws IOException {
Map pathMap = new HashMap();
Object[] key = sheetList.keySet().toArray();
File f = new File(path);
if (!f.exists()) {
f.mkdirs(); // 创建目录
}
for (int i = 0; i < sheetList.size(); i++) {
// 获取图片流
PictureData pic = sheetList.get(key[i]);
// 获取图片索引
String picName = key[i].toString();
// 获取图片格式
String ext = pic.suggestFileExtension();
String fileName = encodingFilename(picName);
byte[] data = pic.getData();
// 图片保存路径
String imgPath = path + fileName + "." + ext;
FileOutputStream out = new FileOutputStream(imgPath);
imgPath = imgPath.substring(ERPConfig.getProfile().length(), imgPath.length());// 截取图片路径
pathMap.put(picName, imgPath);
out.write(data);
out.close();
}
return pathMap;
}
private static final String encodingFilename(String fileName) {
fileName = fileName.replace("_", " ");
fileName = Md5Utils.hash(fileName + System.nanoTime() + counter++);
return fileName;
}
/**
* 读取excel文字
*
* Excel 07版本以上
*
* @param sheet
*/
public static List> readData(XSSFSheet sheet, Map map,String providerId) {
List> newList = new ArrayList>();// 单行数据
try {
int rowNum = sheet.getLastRowNum() + 1;
for (int i = 1; i < rowNum; i++) {// 从第三行开始读取数据,第一行是备注,第二行是标头
Row row = sheet.getRow(i);// 得到Excel工作表的行
if (row != null) {
int col = row.getPhysicalNumberOfCells();
// 单行数据
Map mapRes = new HashMap();// 每格数据
for (int j = 0; j < col; j++) {
Cell cell = row.getCell(j);
if (cell == null) {
// arrayString.add("");
} else if (cell.getCellType() == 0) {// 当时数字时的处理
mapRes.put(getMapKey(j), new Double(cell.getNumericCellValue()).toString());
} else {// 如果EXCEL表格中的数据类型为字符串型
mapRes.put(getMapKey(j), cell.getStringCellValue().trim());
}
}
if (i != 1) {// 不是标头列时,添加图片路径
String path = map.get(i + "-9");
mapRes.put(getMapKey(9), path);
}
mapRes.put("providerId", providerId);
newList.add(mapRes);
}
}
} catch (Exception e) {
}
return newList;
}
public static String getMapKey(int num) {
String res = "";
switch (num) {
case 0:// 分类
res = "secondDictCode";
break;
case 1:// 产品名称
res = "productName";
break;
case 2:// 规格型号
res = "specification";
break;
case 3:// 计量单位
res = "unit";
break;
case 4:// 风格
res = "style";
break;
case 5:// 颜色
res = "color";
break;
case 6:// 采购单价
res = "purchasePrice";
break;
case 7:// 材质
res = "material";
break;
case 8:// 备注
res = "remark";
break;
case 9:// 产品图片
res = "picture";
break;
default:
break;
}
return res;
}
}
邮递员请求测试:/ poi / uploadFile?providerId = 5最终结果
github代码演示和使用说明
csdn资源文件,代码演示和使用说明
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/shumachanpin/article-372552-1.html
那个愿意搞个合娶