使用OpenXml 3.0.1在Excel单元格插入图片后打开文件报错
OpenXml 3.0.1插入Excel图片报错问题
我正在使用OpenXml 3.0.1版本向Excel单元格插入图片。代码运行无报错,但导出文件后打开时会收到错误提示,且图片未出现在预期的A2单元格中。

我参考了MSDN代码库中的插入图片示例,但该代码似乎无法正常工作,可能已不适用于最新版OpenXml。
以下是我的代码:
using PIC = DocumentFormat.OpenXml.Drawing.Pictures; using A = DocumentFormat.OpenXml.Drawing; public byte[] AddImageToExcel(SheetExport sheetExport) { using var memoryStream = new MemoryStream(); var spreadSheetDocument = SpreadsheetDocument.Create(memoryStream, SpreadsheetDocumentType.Workbook); var workbookPart = spreadSheetDocument.AddWorkbookPart(); workbookPart.Workbook = new Workbook(); // Add a WorksheetPart to the WorkbookPart. WorksheetPart worksheetPart = workbookPart.AddNewPart<WorksheetPart>(); worksheetPart.Worksheet = new Worksheet(new SheetData()); Sheets sheets = workbookPart.Workbook.AppendChild(new Sheets()); var sheetToAppend = new Sheet() { Name = sheetExport.SheetName, SheetId = 1, Id = workbookPart.GetIdOfPart(worksheetPart) }; sheets.Append(sheetToAppend); // Adding image to the worksheet InsertImageInCell(worksheetPart); workbookPart.Workbook.Save(); spreadSheetDocument.Dispose(); return memoryStream.ToArray(); } private (long Width, long Height) GetImageExtents(string imagePath) { using System.Drawing.Image image = System.Drawing.Image.FromFile(imagePath); long width = (long)((image.Width / image.HorizontalResolution) * 914400L); long height = (long)((image.Height / image.VerticalResolution) * 914400L); return (width, height); } private void InsertImageInCell(WorksheetPart worksheetPart) { DrawingsPart drawingsPart; if (worksheetPart.DrawingsPart == null) { drawingsPart = worksheetPart.AddNewPart<DrawingsPart>(); worksheetPart.Worksheet.Append(new Drawing { Id = worksheetPart.GetIdOfPart(drawingsPart) }); worksheetPart.Worksheet.Save(); } else { drawingsPart = worksheetPart.DrawingsPart; } var imagePath = Path.Combine(Directory.GetCurrentDirectory(), "Resources", "Banner.png"); var imageExtents = GetImageExtents(imagePath); // Add the image ImagePart imagePart = drawingsPart.AddImagePart(ImagePartType.Png); using (var imageFileStream = new FileStream(imagePath, FileMode.Open, FileAccess.Read)) { imagePart.FeedData(imageFileStream); } // Create the drawing elements var nvdp = new PIC.NonVisualDrawingProperties() { Id = (UInt32Value)1025U, Name = "Picture 1" }; var nvpd = new PIC.NonVisualPictureDrawingProperties(new A.PictureLocks() { NoChangeAspect = true }); var nvpp = new PIC.NonVisualPictureProperties() { NonVisualDrawingProperties = nvdp, NonVisualPictureDrawingProperties = nvpd }; var blip = new A.Blip() { Embed = drawingsPart.GetIdOfPart(imagePart), CompressionState = A.BlipCompressionValues.Print }; var blipFill = new PIC.BlipFill() { Blip = blip, SourceRectangle = new A.SourceRectangle() }; blipFill.Append(new A.Stretch() { FillRectangle = new A.FillRectangle() }); var sp = new PIC.ShapeProperties() { BlackWhiteMode = A.BlackWhiteModeValues.Auto }; var transform2D = new A.Transform2D(); var offset = new A.Offset() { X = 0L, Y = 0L }; var extents = new A.Extents() { Cx = imageExtents.Width, Cy = imageExtents.Height }; transform2D.Append(offset); transform2D.Append(extents); sp.Append(transform2D); sp.Append(new A.PresetGeometry(new A.AdjustValueList()) { Preset = A.ShapeTypeValues.Rectangle }); var picture = new PIC.Picture() { NonVisualPictureProperties = nvpp, BlipFill = blipFill, ShapeProperties = sp }; var position = new Position() { X = 0L, Y = 0L }; var extent = new Extent() { Cx = imageExtents.Width, Cy = imageExtents.Height }; var absoluteAnchor = new AbsoluteAnchor() { Position = position, Extent = extent }; absoluteAnchor.Append(picture); absoluteAnchor.Append(new ClientData()); WorksheetDrawing worksheetDrawing = drawingsPart.WorksheetDrawing; if (worksheetDrawing == null) { worksheetDrawing = new WorksheetDrawing(); drawingsPart.WorksheetDrawing = worksheetDrawing; } worksheetDrawing.Append(absoluteAnchor); drawingsPart.WorksheetDrawing.Save(); }
drawingPart.WorkSheetDrawing的内部XML:
<xdr:absoluteAnchor xmlns:xdr="http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing"> <xdr:pos x="0" y="0" /> <xdr:ext cx="1781423" cy="628737" /> <pic:pic xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture"> <pic:nvPicPr> <pic:cNvPr id="1025" name="Picture 1" /> <pic:cNvPicPr> <a:picLocks noChangeAspect="1" xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" /> </pic:cNvPicPr> </pic:nvPicPr> <pic:blipFill> <a:blip r:embed="R3a1e7131d49045f5" cstate="print" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" /> <a:srcRect xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" /> <a:stretch xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"> <a:fillRect /> </a:stretch> </pic:blipFill> <pic:spPr bwMode="auto"> <a:xfrm xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"> <a:off x="0" y="0" /> <a:ext cx="1781423" cy="628737" /> </a:xfrm> <a:prstGeom prst="rect" xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"> <a:avLst /> </a:prstGeom> </pic:spPr> </pic:pic> <xdr:clientData /> </xdr:absoluteAnchor>
我不能使用EPPlus或ClosedXml等第三方库,必须通过OpenXml解决此问题。
内容的提问来源于stack exchange,提问作者Bug Hunter Zoro
相关产品推荐
相关产品推荐

