如何使用 EPPlus 从 Excel 文件 (xlsx) get/read 图片

How to get/read picture from Excel file (xlsx) using EPPlus

假设我有一个名为 sheet1 的工作表,其中包含一张名为 pic_001 的图片 我怎样才能将此图片作为 System.Drawing.Image 对象。

所有 xlsx 文件实际上都是 zip 文件。您可以 copy/rename .xlsx 扩展名到 .zip 并自行浏览文件夹层次结构。导航至 xl/media 以查看您的图片。当然有更有效的方法来做到这一点,但这是一个快速而肮脏的解决方案,可以完成工作。

好的,我知道如何:

public static Image GetImage(string sheetname, ExcelPackage excelFile)
    {
      var sheet = excelFile.Workbook.Worksheets[sheetname];
      var pic = sheet.Drawings["pic_001"] as ExcelPicture;
      return pic.Image;
    }

我在寻找 myslef 使用 EPplus 从 excel sheet 中提取图像时偶然发现了这个问题。我的要求是获得相应的 row/col 以及我想将图像与我从中读取的行相关联。


var workbook = xlPackage.Workbook;
//You can resolve it by worksheet name if you using multiple sheet
ExcelWorksheet ws = workbook.Worksheets[0];
//Create a lookup of drawings per sheet
var lkDrawings = ws.Drawings.ToLookup(x => $"{ x.From.Row}_{x.From.Column}");


//now you can use this lookup while iterating your cells and save it as image. 
//You can look at example of iterating epplus cells 
//or ping me if you need more details on that.

 var lookUpKey = $"{rowCounter}_{col}";
 if(lkDrawings.Contains(lookUpKey))
 {
    ExcelPicture image = lkDrawings[lookUpKey].ToList()[0] as ExcelPicture;

    image.Image.Save($"{rowCounter}_{col}.png");
 }