利用POI获取Excel中图片和图片位置(支持excel2003or2007多sheet)
2013-04-22 13:17
741 查看
利用POI获取Excel中图片和图片位置(支持excel2003or2007多sheet)
第三方JAR包(apache下载POI即可):poi-3.9-20121203.jar
dom4j-1.6.1.jar
poi-ooxml-3.9-20121203.jar
poi-ooxml-schemas-3.9-20121203.jar
poi-scratchpad-3.9-20121203.jar
stax-api-1.0.1.jar
xmlbeans-2.3.0.jar
测试代码:
package com; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; 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.hssf.usermodel.HSSFClientAnchor; import org.apache.poi.hssf.usermodel.HSSFPicture; import org.apache.poi.hssf.usermodel.HSSFPictureData; import org.apache.poi.hssf.usermodel.HSSFShape; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.ss.usermodel.PictureData; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.usermodel.WorkbookFactory; 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.apache.poi.xssf.usermodel.XSSFWorkbook; import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTMarker; /** * @since 2013-04-22 * @author Gerrard * 获取excel中 图片,并得到图片位置,支持03 07 多sheet */ public class GetImgFromExcel { /** * @param args * @throws IOException * @throws InvalidFormatException */ public static void main(String[] args) throws InvalidFormatException, IOException { // 创建文件 File file = new File("model/test.xls"); // 创建流 InputStream input = new FileInputStream(file); // 获取文件后缀名 String fileExt = file.getName().substring(file.getName().lastIndexOf(".") + 1); // 创建Workbook Workbook wb = null; // 创建sheet Sheet sheet = null; //根据后缀判断excel 2003 or 2007+ if (fileExt.equals("xls")) { wb = (HSSFWorkbook) WorkbookFactory.create(input); } else { wb = new XSSFWorkbook(input); } //获取excel sheet总数 int sheetNumbers = wb.getNumberOfSheets(); // sheet list List<Map<String, PictureData>> sheetList = new ArrayList<Map<String, PictureData>>(); // 循环sheet for (int i = 0; i < sheetNumbers; i++) { sheet = wb.getSheetAt(i); // map等待存储excel图片 Map<String, PictureData> sheetIndexPicMap; // 判断用07还是03的方法获取图片 if (fileExt.equals("xls")) { sheetIndexPicMap = getSheetPictrues03(i, (HSSFSheet) sheet, (HSSFWorkbook) wb); } else { sheetIndexPicMap = getSheetPictrues07(i, (XSSFSheet) sheet, (XSSFWorkbook) wb); } // 将当前sheet图片map存入list sheetList.add(sheetIndexPicMap); } printImg(sheetList); } /** * 获取Excel2003图片 * @param sheetNum 当前sheet编号 * @param sheet 当前sheet对象 * @param workbook 工作簿对象 * @return Map key:图片单元格索引(0_1_1)String,value:图片流PictureData * @throws IOException */ public static Map<String, PictureData> getSheetPictrues03(int sheetNum, HSSFSheet sheet, HSSFWorkbook workbook) { Map<String, PictureData> sheetIndexPicMap = new HashMap<String, PictureData>(); List<HSSFPictureData> pictures = workbook.getAllPictures(); if (pictures.size() != 0) { for (HSSFShape shape : sheet.getDrawingPatriarch().getChildren()) { HSSFClientAnchor anchor = (HSSFClientAnchor) shape.getAnchor(); if (shape instanceof HSSFPicture) { HSSFPicture pic = (HSSFPicture) shape; int pictureIndex = pic.getPictureIndex() - 1; HSSFPictureData picData = pictures.get(pictureIndex); String picIndex = String.valueOf(sheetNum) + "_" + String.valueOf(anchor.getRow1()) + "_" + String.valueOf(anchor.getCol1()); sheetIndexPicMap.put(picIndex, picData); } } return sheetIndexPicMap; } else { return null; } } /** * 获取Excel2007图片 * @param sheetNum 当前sheet编号 * @param sheet 当前sheet对象 * @param workbook 工作簿对象 * @return Map key:图片单元格索引(0_1_1)String,value:图片流PictureData */ public static Map<String, PictureData> getSheetPictrues07(int sheetNum, XSSFSheet sheet, XSSFWorkbook workbook) { Map<String, PictureData> sheetIndexPicMap = new HashMap<String, PictureData>(); for (POIXMLDocumentPart dr : sheet.getRelations()) { if (dr instanceof XSSFDrawing) { XSSFDrawing drawing = (XSSFDrawing) dr; List<XSSFShape> shapes = drawing.getShapes(); for (XSSFShape shape : shapes) { XSSFPicture pic = (XSSFPicture) shape; XSSFClientAnchor anchor = pic.getPreferredSize(); CTMarker ctMarker = anchor.getFrom(); String picIndex = String.valueOf(sheetNum) + "_" + ctMarker.getRow() + "_" + ctMarker.getCol(); sheetIndexPicMap.put(picIndex, pic.getPictureData()); } } } return sheetIndexPicMap; } public static void printImg(List<Map<String, PictureData>> sheetList) throws IOException { for (Map<String, PictureData> map : sheetList) { Object key[] = map.keySet().toArray(); for (int i = 0; i < map.size(); i++) { // 获取图片流 PictureData pic = map.get(key[i]); // 获取图片索引 String picName = key[i].toString(); // 获取图片格式 String ext = pic.suggestFileExtension(); byte[] data = pic.getData(); FileOutputStream out = new FileOutputStream("D:\\pic" + picName + "." + ext); out.write(data); out.close(); } } } }
相关文章推荐
- 利用POI获取Excel中图片和图片位置
- 转:通过 POI 获取图片在 Excel 表格中的位置
- poi获取Excel表中图片位置
- 关于POI对Excel2007中图片的处理,获取图片在excel中对应的位置
- zk框架获取POI Excel图片 以及 设置图片位置和大小的方法
- poi,java向 excel文件写数据(缺点,HSSFWorkbook不支持图片) 例子
- 将eChart图片利用POI导出到Excel
- poi 获取excel中图片
- java利用POI实现Excel导入导出详解-支持97-2013版本以及2017版本
- 利用Java+POI 读写Excel文档&向Excel中插入图片
- JAVA利用POI解析Excel图片,并按照标签号分类导入文件夹
- Java中利用POI操作Excel(支持2003、2007)
- 封装poi读取excel的超强工具类,支持一行代码获取excel内容
- java利用POI向Excel(xls)写入图片,并对图片引用超链接
- POI+JFreeChart生成报表图片在Excel中的位置
- poi读取Excel中的图片位置
- 用POI获取excel表中的图片位置及数据
- poi获取图片位置
- java poi Excel内图片获取存储并关联数据入库
- poi 获取excel中图片