java讀取excel圖片導入代碼示例(親測有效)
從excel文件中獲取圖片(兼容新老版本)
/** * 獲取excel表中的圖片 * * @return * @throws IOException * @throws InvalidFormatException * @throws EncryptedDocumentException * @Param fis 文件輸入流 * @Param sheetNum Excel表中的sheet編號 */ public static Map<String, PictureData> getPictureFromExcel(File file, int sheetNum) throws EncryptedDocumentException, IOException { //獲取圖片PictureData集合 String fileName = file.getName(); Workbook workbook = null; if (StringUtil.isEmpty(fileName)) { return null; } FileInputStream fileInputStream = new FileInputStream(file); if (fileName.endsWith("xls")) { //2003 workbook = new HSSFWorkbook(fileInputStream); HSSFSheet sheet = (HSSFSheet) workbook.getSheetAt(sheetNum - 1); Map<String, PictureData> pictures = getPictures(sheet); return pictures; } else if (fileName.endsWith("xlsx")) { //2007 workbook = new XSSFWorkbook(fileInputStream); XSSFSheet sheet = (XSSFSheet) workbook.getSheetAt(sheetNum - 1); Map<String, PictureData> pictures = getPictures(sheet); return pictures; } return new HashMap(); }
從sheet頁中獲取圖片及圖片位置
/** * 獲取圖片和位置 (xls版) * @param sheet * @return * @throws IOException */ public static Map<String, PictureData> getPictures (HSSFSheet sheet) throws IOException { Map<String, PictureData> map = new HashMap<String, PictureData>(); List<HSSFShape> list = sheet.getDrawingPatriarch().getChildren(); for (HSSFShape shape : list) { if (shape instanceof HSSFPicture) { HSSFPicture picture = (HSSFPicture) shape; HSSFClientAnchor cAnchor = picture.getClientAnchor(); PictureData pdata = picture.getPictureData(); String key = cAnchor.getRow1() + "-" + cAnchor.getCol1(); // 行號-列號 map.put(key, pdata); } } return map; } /** * 獲取圖片和位置 (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(); map.put(key, picture.getPictureData()); } } } return map; }
main方法-獲取excel文件中的圖片下載到本地
public static void main(String [] args) throws IOException { File file = new File("C:\\Users\\A\\Desktop\\test.xlsx"); //獲取圖片數(shù)據(jù) Map<String, PictureData> map = getPictureFromExcel(file, 1); //根據(jù)圖片行列位置獲取圖片 PictureData picData = (PictureData)map.get("3"+"-"+14); //后綴名 String extension = picData.suggestFileExtension(); byte[] data = picData.getData(); FileOutputStream out = new FileOutputStream("C:\\Users\\A\\Desktop\\" + "picture." + extension); out.write(data); out.close(); }
遇到問題:圖片導入解析報錯異常java.lang.ClassCastException: org.apache.poi.xssf.usermodel.
XSSFSimpleShape cannot be cast to org.apache.poi.xssf.usermodel.XSSFPicture問題
解決方法:經(jīng)過排查,為xslx版本shap對象強轉(zhuǎn)為圖片對象報錯,添加邏輯首先檢驗if (shape instanceof XSSFPicture)類型然后再進行強轉(zhuǎn),問題解決
總結(jié)
到此這篇關(guān)于java讀取excel圖片導入的文章就介紹到這了,更多相關(guān)java讀取excel圖片導入內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
mybatis foreach批量插入數(shù)據(jù):Oracle與MySQL區(qū)別介紹
這篇文章主要介紹了,需要的朋友可以參考下2018-01-01SpringBoot之@ConditionalOnProperty注解使用方法
在平時業(yè)務中,我們需要在配置文件中配置某個屬性來決定是否需要將某些類進行注入,讓Spring進行管理,而@ConditionalOnProperty能夠?qū)崿F(xiàn)該功能,文中有詳細的代碼示例,需要的朋友可以參考下2023-05-05Spring如何替換掉默認common-logging.jar
這篇文章主要介紹了Spring如何替換掉默認common-logging.jar,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-05-05使用Java和WebSocket實現(xiàn)網(wǎng)頁聊天室實例代碼
WebSocket是HTML5一種新的協(xié)議,它實現(xiàn)了瀏覽器與服務器全雙工通信,這里就將使用WebSocket來開發(fā)網(wǎng)頁聊天室,對Java和WebSocket實現(xiàn)網(wǎng)頁聊天室的實例代碼感興趣的朋友一起學習吧2016-06-06