OpenXml:确定在Excel单元格中的图像

可能重复:
如何检查一个单元格是否有图片?
OpenXML:Excel,提取单元格文本和图像/图片数据

我有一个Excel文档有4个图像,每个在单元格A1,A2,A3,A4

我可以迭代工作表中的所有图像,但是我需要能够编辑这个函数来给我例如“A1”和“A2”中的图像

[Test] public void IterateThruImages() { WorkbookPart wbPart = document.WorkbookPart; var workSheet = wbPart.WorksheetParts.FirstOrDefault(); foreach(ImagePart i in workSheet.DrawingsPart.GetPartsOfType<ImagePart>()) { Stream stream = i.GetStream(); long length = stream.Length; byte[] byteStream = new byte[length]; stream.Read(byteStream, 0, (int)length); var imageAsString = Convert.ToBase64String(byteStream); } } 

诀窍是获得图像的关系ID“rId”。 你可以通过获取行/列的TwoCellAnchor。 然后获取embedded。 用它来获取图像。

  [Test] public void GetImageRelationshipIdAndImageOfThatId() { string row = "1"; string col = "0"; WorkbookPart wbPart = document.WorkbookPart; var workSheet = wbPart.WorksheetParts.FirstOrDefault(); TwoCellAnchor cellHoldingPicture = workSheet.DrawingsPart.WorksheetDrawing.OfType<TwoCellAnchor>() .Where(c => c.FromMarker.RowId.Text == row && c.FromMarker.ColumnId.Text == col).FirstOrDefault(); var picture = cellHoldingPicture.OfType<DocumentFormat.OpenXml.Drawing.Spreadsheet.Picture>().FirstOrDefault(); string rIdofPicture = picture.BlipFill.Blip.Embed; Console.WriteLine("The rID of this Anchor's [{0},{1}] Picture is '{2}'" ,row,col, rIdofPicture); ImagePart imageInThisCell = (ImagePart)workSheet.DrawingsPart.GetPartById(rIdofPicture); }