java.lang.NullPointerException; XSSFClientAnchor anchor = picture.getPreferredSize()空指针;poi读取excel图片

poi jar包下载地址:https://search.maven.org/artifact/org.apache.poi/poi/4.1.0/jar

 

XSSFClientAnchor anchor = picture.getPreferredSize();空指针问题

不啰嗦,先看报错信息

java.lang.NullPointerException; XSSFClientAnchor anchor = picture.getPreferredSize()空指针;poi读取excel图片_第1张图片

 

然后就是卡在这里三天 ,在网上也找不到太多有用的信息。

最后呢,看了这篇文章给了我启发https://www.cnblogs.com/kevinfuture/p/4775973.html

我发现我的jar包版本不一。

 

java.lang.NullPointerException; XSSFClientAnchor anchor = picture.getPreferredSize()空指针;poi读取excel图片_第2张图片

 

针对这个错误我另外去下载了一个3.9版本的jar包,重新运行,这个问题就不存在了

 

下面是我的代码:

public static void main(String[] args) { //
		try {
			new ReadExcel().getValues("c:\\aa1.xlsx");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

//读取excel表的内容
public void getValues(String filePath) throws IOException {
		String values = null;
		// try {
		InputStream is = new FileInputStream(filePath);
		// 构造 XSSFWorkbook 对象,strPath 传入文件路径
		XSSFWorkbook xwb = new XSSFWorkbook(is);

		// 读取第一章表格内容
		XSSFSheet sheet = xwb.getSheetAt(0);

		System.out.println("获取图片");
		// 获取图片
		Map map = getPicture1(sheet);
		System.out.println("输出map:" + map);
		System.out.println("写入图片");
		printImg(map);

		// 定义 row、cell
		XSSFRow row;
		String cell;
		System.out.println(sheet.getPhysicalNumberOfRows());
		// 循环输出表格中的内容
		for (int i = sheet.getFirstRowNum(); i < sheet.getPhysicalNumberOfRows(); i++) {
			// 获取表的行
			row = sheet.getRow(i);
			List list = new ArrayList();
			// System.out.println(row.getPhysicalNumberOfCells());
			for (int j = row.getFirstCellNum(); j < row.getPhysicalNumberOfCells(); j++) {
				// 通过 row.getCell(j).toString() 获取单元格内容,
				if (row.getCell(j) != null) {
					cell = row.getCell(j).toString();
					list.add(cell);
					// System.out.println(cell);
					System.out.print(cell + "\t");
				}

			}
			
			System.out.println("");
		}
		
	}


public Map getPicture1(XSSFSheet sheet) {
		Map map = new HashMap();
		List list = sheet.getRelations();

		System.out.println("输出POIXMLDocumentPart对象集合:" + list);
		for (POIXMLDocumentPart part : list) {
			if (part instanceof XSSFDrawing) {
				XSSFDrawing drawing = (XSSFDrawing) part;
				System.out.println("XSSFDrawing对象:" + drawing);
				List shapes = drawing.getShapes();
				System.out.println("XSSFShape对象集合:" + shapes);
				for (XSSFShape shape : shapes) {
					if (shape instanceof XSSFPicture) {
						XSSFPicture picture = (XSSFPicture) shape;
						System.out.println("XSSFPicture对象:" + picture);
						XSSFClientAnchor anchor = picture.getPreferredSize();
						System.out.println("XSSFClientAnchor对象:" + anchor);
						CTMarker marker = anchor.getFrom();
						String key = marker.getRow() + "-" + marker.getCol();
						map.put(key, picture.getPictureData());
					}
				}
			}
		}

		return map;
	}



// 图片写出
	public static void printImg(Map sheetList) throws IOException {

		System.out.println("map是否有值:" + sheetList.size());
		// for (Map map : sheetList) {
		Object key[] = sheetList.keySet().toArray();
		for (int i = 0; i < sheetList.size(); i++) {
			// 获取图片流
			PictureData pic = sheetList.get(key[i]);
			// 获取图片索引
			String picName = key[i].toString();
			// 获取图片格式
			String ext = pic.suggestFileExtension();

			byte[] data = pic.getData();

			// 图片保存路径
			FileOutputStream out = new FileOutputStream("G:\\img\\pic" + picName + "." + ext);
			out.write(data);
			out.close();
		}
		// }

	}

 

你可能感兴趣的:(poi)