I looked at the answers from Vincent Tan and David Yates. Since it took me hours of coding to fulfill the following simple requirements, I will post my solution here so that the next person can save some time.
Requirements:
- Add image to specific cell of existing Excel spreadsheet / sheet
- Create a new table and add an image to a specific cell
- Add an image to a specific cell in the Worksheet (so that other operations can be performed before it is saved)
- Add image from stream
- Add image from file
- Using various image formats
Using
// Add via file to existing spreadsheet try { ExcelTools.AddImage(false, filePath, "Sheet name", imagePath, "Image description", 2 /* column */, 2 /* row */); } catch ... // Add via stream to existing spreadsheet try { ExcelTools.AddImage(false, filePath, "Sheet name", imageStream, "Image description", 2 /* column */, 2 /* row */); } catch ... // Create spreadsheet and add image via path try { ExcelTools.AddImage(true, filePath, "Sheet name", imagePath, "Image description", 2 /* column */, 2 /* row */); } catch ... // Create spreadsheet and add image via stream try { ExcelTools.AddImage(true, filePath, "Sheet name", imageStream, "Image description", 2 /* column */, 2 /* row */); } catch ... // Add multiple images or apply further changes try { // Open spreadsheet spreadsheetDocument = SpreadsheetDocument.Open(excelFile, true); // Get WorksheetPart worksheetPart = ExcelTools.GetWorksheetPartByName(spreadsheetDocument, "Some sheet name"); AddImage(worksheetPart, imagePath1, "My first image", 1, 1); // A1 AddImage(worksheetPart, imagePath2, "My second image", 1, 5); // A5 AddImage(worksheetPart, imagePath3, "My third image", 2, 7); // B7 // Other operations if needed worksheetPart.Worksheet.Save(); spreadsheetDocument.Close(); } catch ...
The code:
using System; using System.Data; using System.Linq; using System.IO; using System.Drawing; using System.Drawing.Imaging; using System.Collections.Generic; using DocumentFormat.OpenXml; using DocumentFormat.OpenXml.Packaging; using DocumentFormat.OpenXml.Spreadsheet; using A = DocumentFormat.OpenXml.Drawing; using Xdr = DocumentFormat.OpenXml.Drawing.Spreadsheet; namespace Utilities { public class ExcelTools { public static ImagePartType GetImagePartTypeByBitmap(Bitmap image) { if (ImageFormat.Bmp.Equals(image.RawFormat)) return ImagePartType.Bmp; else if (ImageFormat.Gif.Equals(image.RawFormat)) return ImagePartType.Gif; else if (ImageFormat.Png.Equals(image.RawFormat)) return ImagePartType.Png; else if (ImageFormat.Tiff.Equals(image.RawFormat)) return ImagePartType.Tiff; else if (ImageFormat.Icon.Equals(image.RawFormat)) return ImagePartType.Icon; else if (ImageFormat.Jpeg.Equals(image.RawFormat)) return ImagePartType.Jpeg; else if (ImageFormat.Emf.Equals(image.RawFormat)) return ImagePartType.Emf; else if (ImageFormat.Wmf.Equals(image.RawFormat)) return ImagePartType.Wmf; else throw new Exception("Image type could not be determined."); } public static WorksheetPart GetWorksheetPartByName(SpreadsheetDocument document, string sheetName) { IEnumerable<Sheet> sheets = document.WorkbookPart.Workbook.GetFirstChild<Sheets>(). Elements<Sheet>().Where(s => s.Name == sheetName); if (sheets.Count() == 0) {