欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

poi读取excel2003文件内的图片

程序员文章站 2022-03-06 22:26:17
...

     poi读取excel内的图片,网上例子很多,本文是网上找的,所用poi版本为poi3.9,代码本人亲测通过,注意,本文所说的图片是jpeg,png等格式的图片,使用excel绘制的图表不再本文所说的图片范围内。

     参考原文如下:

    

http://blog.csdn.net/delongcpp/article/details/8833995
http://www.cnblogs.com/dingmy/archive/2013/03/11/2954217.html

    poi 3.9读取excel 2003内的图片:

    

/**
	 * 获取Excel2003图片
	 * 
	 * @param sheetNum
	 *            当前sheet编号
	 * @param sheet
	 *            当前sheet对象
	 * @param workbook
	 *            工作簿对象
	 * @return Map key:图片单元格索引(sheet(id)_[row1,col1]_[row2,col2])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) {
			HSSFPatriarch hssPatriarch = sheet.getDrawingPatriarch();
			if (hssPatriarch != null) {
				for (HSSFShape shape : hssPatriarch.getChildren()) {
					HSSFClientAnchor anchor = (HSSFClientAnchor) shape
							.getAnchor();
					if (shape instanceof HSSFPicture) {
						HSSFPicture pic = (HSSFPicture) shape;
						int pictureIndex = pic.getPictureIndex() - 1;
						HSSFPictureData picData = pictures.get(pictureIndex);
						//行列下标都是从0开始的,
						//这里行数加+1,第一张图片信息行数不准确,测试下就知道了
						String picIndex = "sheet" + String.valueOf(sheetNum)
								+ "_[" + String.valueOf(anchor.getRow1()+1)+"," + String.valueOf(anchor.getCol1())
								+ "]_["+ String.valueOf(anchor.getRow2()+1)+"," + String.valueOf(anchor.getCol2())+"]";
						sheetIndexPicMap.put(picIndex, picData);
					}
				}
			}
			return sheetIndexPicMap;
		} else {
			return null;
		}
	}

    保存读取的图片代码为:

  

public static void printImg(List<Map<String, PictureData>> sheetList,
			String filePath) 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(filePath + picName
						+ "." + ext);
				out.write(data);
				out.close();
			}
		}
	}

    对于poi 3.9在读取excel 2007的图片,网上搜了下,只能一次全部读取出来,拿不到图片的具体信息,比如图片所在的sheet,图片位置(行,列)信息。poi可能有相关API,没找到,有知道的,请指教,谢谢。

   poi读取excel 2007图片代码如下:

  

public static void getSheetPic07(String filePath, XSSFSheet sheet,
			XSSFWorkbook workbook) throws Exception {
		List<XSSFPictureData> pictures = workbook.getAllPictures();
		Map<String, XSSFPictureData> map = new HashMap<String, XSSFPictureData>();
		for (int i = 0; i < pictures.size(); i++) {
			XSSFPictureData pictureData = pictures.get(i);
			byte[] data = pictureData.getData();
			String ext = pictureData.suggestFileExtension();
			FileOutputStream out = new FileOutputStream(filePath + "img_" + i
					+ "." + ext);
			out.write(data);
			out.close();
		}
	}

    测试方法为:

   

public static void main(String[] args) throws Exception {
		// 创建文件
		File file = new File("f:/saveFile/testpic2003.xls");
		// 创建流
		InputStream input = new FileInputStream(file);
		// 获取文件后缀名
		String fileExt = file.getName().substring(
				file.getName().lastIndexOf(".") + 1);
		// 创建Workbook
		Workbook wb = null;
		// 创建sheet
		Sheet sheet = null;
		// 根据后缀判断03,07
		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>>();
		if (fileExt.equals("xls")) {
			// 循环sheet
			for (int i = 0; i < sheetNumbers; i++) {
				sheet = wb.getSheetAt(i);
				// map等待存储excel图片
				Map<String, PictureData> sheetIndexPicMap = null;
				sheetIndexPicMap = getSheetPictrues03(i, (HSSFSheet) sheet,
						(HSSFWorkbook) wb);
				// 将当前sheet图片map存入list
				sheetList.add(sheetIndexPicMap);
			}
			printImg(sheetList, "f:/saveFile/pic/");
		} else {
			getSheetPic07("f:/saveFile/pic/", (XSSFSheet) sheet,
					(XSSFWorkbook) wb);
		}
	}

    全文完。

 

 

相关标签: poi