> uploadMonitorItem(MultipartFile upfile, String providerId) throws Exception { InputStream in = null; List搜索查看编辑修改首页UNITYNODEJSPYTHONAIGITPHPGOCEF3JAVAHTMLCSS搜索羊村懒王 这个屌丝很懒,什么也没留下! 关注作者热门标签jqueryHTMLCSSPHPASPPYTHONGOAICC++C#PHOTOSHOPUNITYiOSandroidvuexml爬虫SEOLINUXWINDOWSJAVAMFCCEF3CADNODEJSGITPyppeteerarticle热门文章1opencv图像增强实现方法2tcc分布式事务框架源码解析系列(四)之项目实战3C++/CLI学习笔记12(快速打通c++与c#相互调用的桥梁)404741 计算机网络原理 第4章 网络层_04741 计算机网络原理串讲笔记5中文C语言编程鸿蒙Hi3861智能硬件开发套件_鸿蒙中文编程语言6使用java语言编写一个简易的计算器(完整代码与详细步骤都有哦!)_用java写一个计算器程序7带你了解ChatGPT是啥8vue this.$router.push()传参_this.$router.push,传递参数9一文搞懂Kafka核心基础知识_kafka2.12版本特性10C++上位软件通过LibModbus开源库和西门子S7-1200/S7-1500/S7-200 PLC进行ModbusTcp 和ModbusRTU 通信_c++实现modbusrtu通信当前位置: article > 正文 poi读取excel中的内容(带图片)_poi读取excel中的图片 作者:羊村懒王 | 2024-03-20 11:38:47 赞踩poi读取excel中的图片 项目中有一需求:需要读取excel中的信息,带图片,需要将每一行的图片,做对应,整理方法如下: 主要逻辑: 1,获取excel对象, 2,读取excel中所有的图片流,将图片所处的位置和图片流存在map中getPictures()方法 4,根据图片所处位置,获取图片流,上传图片,并将图片的所处位置和上传路径存在map中printImg()方法 5,读取excel中的每一行文字,当读取到图片列时,根据key获取第四步的map中的value,将路径以及文字信息存入list中;readData()方法 controller层 @PostMapping("/uploadFile") public List<Map<String, String>> uploadMonitorItem(MultipartFile upfile, String providerId) throws Exception { InputStream in = null; List<Map<String, String>> listob = null; in = upfile.getInputStream(); listob = ExcelUtil.readExcelByInputStream(in, providerId); return listob; } 12345678910 2.service层 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<Map<String, String>> readExcelByInputStream(InputStream inputstream, String providerId) throws Exception { // 结果集 List<Map<String, String>> list = new ArrayList<Map<String, String>>(); XSSFWorkbook wb = new XSSFWorkbook(inputstream); String filePath = ERPConfig.getProfile() + "/" + "pic/" + providerId + "/";//图片保存路径 final XSSFSheet sheet = wb.getSheetAt(0);// 得到Excel工作表对象 Map<String, PictureData> map = ExcelImgUtil.getPictures(sheet);//获取图片和位置 Map<String, String> pathMap = ExcelImgUtil.printImg(map, filePath);//写入图片,并返回图片路径,key:图片坐标,value:图片路径 list = ExcelImgUtil.readData(sheet, pathMap,providerId); return list; } } 123456789101112131415161718192021222324252627282930313233343536 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<String, PictureData> getPictures(XSSFSheet sheet) throws IOException { Map<String, PictureData> map = new HashMap<String, PictureData>(); List<POIXMLDocumentPart> list = sheet.getRelations(); for (POIXMLDocumentPart part : list) { if (part instanceof XSSFDrawing) { XSSFDrawing drawing = (XSSFDrawing) part; List<XSSFShape> 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<String, String> printImg(Map<String, PictureData> sheetList, String path) throws IOException { Map<String, String> pathMap = new HashMap<String, String>(); 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<Map<String, String>> readData(XSSFSheet sheet, Map<String, String> map,String providerId) { List<Map<String, String>> newList = new ArrayList<Map<String, String>>();// 单行数据 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<String, String> mapRes = new HashMap<String, String>();// 每格数据 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; } } 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 postman请求测试:/poi/uploadFile?providerId=5最终效果 [ { "unit": "计量单位", "color": "颜色", "material": "材质", "providerId": "5", "specification": "规格型号", "style": "风格", "remark": "备注", "purchasePrice": "采购单价", "secondDictCode": "分类", "productName": "产品名称", "picture": "产品图片" }, { "unit": "平方米", "color": "白色,绿色,红色", "material": "木质1", "providerId": "5", "specification": "600*900", "style": "美式,北欧,中式", "remark": "大萨达十大1", "purchasePrice": "66.8", "secondDictCode": "家具", "productName": "床", "picture": "/pic/5/b3e42b2d741cc16bff4fefaa6bb61d87.jpeg" }, { "providerId": "5", "picture": null } ] 1234567891011121314151617181920212223242526272829303132 github代码demo,及使用说明 csdn资源文件,代码demo及使用说明 声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/273077推荐阅读articleMicrosoft Excel 不能访问文件“ 文件名称或路径不存在。 • 文件正被其他程序使用。 ...Microsoft Office Excel 不能访问文件“D:\WWWRoot\KOBELCOSH\WebUI\Exc... 赞踩article程序员不得不知道的英语词汇_程序员 单词 excel...下面是一些在编程时常见的一些英语词汇。Data Structures 基本数据结构 Dictionaries 字典 Pr... 赞踩articlePython+Requests+Pytest+Excel+Allure 接口自动化测试项目实战【框架...以get和post为例:put,delete请求按一样方法封装,各个请求方式封装完成以后再定义一个主方法,直接调用主方法... 赞踩articleMicrosoft VBA Excel 提取相同名称的整列数据...啦啦啦Microsoft VBA Excel 提取相同名称的整列数据 ... 赞踩articleEXCEL 2016 冻结窗口(首行首列,多行多列)_冻结窗口怎么冻结多行多列...1、冻结首行首列点击视图——冻结窗口,可直接选择冻结首行或者冻结首列,如图:冻结首行后,上下滑动表格时首行保持不动,方便... 赞踩articleMicrosoft VBA Excel 规律的Text文件转工作表Sheet...啦啦啦Microsoft VBA Excel 规律的Text文件转工作表Sheet ... 赞踩articleuniapp导出excel_unicloud download-excel...H5可用、IOS手机无效、安卓无效、小程序无效。无效的原因未知,如有兴趣,可自行研究。此插件是我在搞VUE项目时,PC端... 赞踩articleuniapp 前端导出excel_uni-excel...需求:app前端导出excel,不需要后台_uni-exceluni-excel 需求:app前... 赞踩articleunity打开excel表格_unity创建编辑读取EXCEL文件表格数据游戏插件工具Uni-Exc...Uni-Excel 1.0是创建编辑读取EXCEL文件表格数据的游戏插件工具。With Uni-Excel? You c... 赞踩articleuniApp 使用uni.openDocument(object)预览pdf、excel、word等...uni.openDocument 预览pdf、excel等文件_uni.opendocumentuni.opendocu... 赞踩article【uniapp H5在线浏览Word,excel,Execel 】_uniappp h5端 如何在新...uniapp H5 如何在线浏览Word,excel,Execel_uniappp h5端 如何在新页面打开文件unia... 赞踩articleexcel 多项式拟合数据_excel多项式拟合函数...有如下一组数据和其对应的曲线波形,如何快速得到其数据函数表达式? x y 996 ... 赞踩articledocuments4j-word excel ..转pdf格式_documents4j word转p...草率的看了一眼文档,说明使用本地转换器需要在本机有ms work、ms excel (此处部署在linux时需要安装)。... 赞踩articleFlutter 实现pdf、word、excel、png、ppt等文件预览功能_flutter预览各...在开发过程中,我们很少会遇到预览pdf、word、excel、png、ppt等文件的功能,但是很巧的本人就遇到了,由于第... 赞踩article数学建模——灰色关联分析【工具:Excel】【全文5500字】_灰色关联分析用什么软件做...本文旨在能快速地用Excel实现灰色关联分析,并简单地了解其原理。该系列文章是个人在参加2021年暑假国赛数模的培训,自... 赞踩articleJava 使用POI导出数据到 excel,单 sheet 和多 sheet_poi多sheet...前段时间,写(shui)了一个上传文件的博客(Java 上传附件后端接口大体流程和逻辑),今天做项目时,刚好又遇到导出e... 赞踩articlejava excel 导出多个sheet_java导出怎样一个文件里面 出现两个 secelt...第一步:需要一个通用的装载数据的实体类package com.yfh.common.core.domain;import... 赞踩articlejava导出excel生成多个sheet_java excel clonesheet 不同excel...try {OutputStream out = new FileOutputStream(“E:\test.xls”);... 赞踩articlejava向excel中插入图片_java excel 插入图片...实现方式如下:// 首先确定好图片范围,即excel中的开始行/列和结束行/列int beginCol = CellRe... 赞踩articleJava使用POI读取Excel文件中的所有图片(包含Wps嵌入式图片)_java excel 读取...使用POI读取Excel文件中的图片数据。_java excel 读取单元格内悬浮图片java excel 读取单元格内... 赞踩相关标签uiruntimepythonpytestexcel功能测试软件测试自动化测试程序人生microsoftjavauniapp前端unity打开excel表格uni-appjavascriptpdfflutter数学建模
赞
踩
主要逻辑: 1,获取excel对象, 2,读取excel中所有的图片流,将图片所处的位置和图片流存在map中getPictures()方法 4,根据图片所处位置,获取图片流,上传图片,并将图片的所处位置和上传路径存在map中printImg()方法 5,读取excel中的每一行文字,当读取到图片列时,根据key获取第四步的map中的value,将路径以及文字信息存入list中;readData()方法
@PostMapping("/uploadFile") public List<Map<String, String>> uploadMonitorItem(MultipartFile upfile, String providerId) throws Exception { InputStream in = null; List<Map<String, String>> listob = null; in = upfile.getInputStream(); listob = ExcelUtil.readExcelByInputStream(in, providerId); return listob; }
2.service层
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<Map<String, String>> readExcelByInputStream(InputStream inputstream, String providerId) throws Exception { // 结果集 List<Map<String, String>> list = new ArrayList<Map<String, String>>(); XSSFWorkbook wb = new XSSFWorkbook(inputstream); String filePath = ERPConfig.getProfile() + "/" + "pic/" + providerId + "/";//图片保存路径 final XSSFSheet sheet = wb.getSheetAt(0);// 得到Excel工作表对象 Map<String, PictureData> map = ExcelImgUtil.getPictures(sheet);//获取图片和位置 Map<String, String> 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<String, PictureData> getPictures(XSSFSheet sheet) throws IOException { Map<String, PictureData> map = new HashMap<String, PictureData>(); List<POIXMLDocumentPart> list = sheet.getRelations(); for (POIXMLDocumentPart part : list) { if (part instanceof XSSFDrawing) { XSSFDrawing drawing = (XSSFDrawing) part; List<XSSFShape> 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<String, String> printImg(Map<String, PictureData> sheetList, String path) throws IOException { Map<String, String> pathMap = new HashMap<String, String>(); 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<Map<String, String>> readData(XSSFSheet sheet, Map<String, String> map,String providerId) { List<Map<String, String>> newList = new ArrayList<Map<String, String>>();// 单行数据 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<String, String> mapRes = new HashMap<String, String>();// 每格数据 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; } }
[ { "unit": "计量单位", "color": "颜色", "material": "材质", "providerId": "5", "specification": "规格型号", "style": "风格", "remark": "备注", "purchasePrice": "采购单价", "secondDictCode": "分类", "productName": "产品名称", "picture": "产品图片" }, { "unit": "平方米", "color": "白色,绿色,红色", "material": "木质1", "providerId": "5", "specification": "600*900", "style": "美式,北欧,中式", "remark": "大萨达十大1", "purchasePrice": "66.8", "secondDictCode": "家具", "productName": "床", "picture": "/pic/5/b3e42b2d741cc16bff4fefaa6bb61d87.jpeg" }, { "providerId": "5", "picture": null } ]
github代码demo,及使用说明
csdn资源文件,代码demo及使用说明