如何使用EPPlus从Excel文件(xlsx)中获取/读取图片



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

好的,我找到了如何:

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;
    }

所有xlsx文件实际上都是zip文件。您可以将.xlsx扩展名复制/重命名为.zip,并自己浏览文件夹层次结构。导航到xl/media查看您的图像。当然有更有效的方法可以做到这一点,但这是一个快速而肮脏的解决方案,可以完成任务。

当我使用EPplus从excel表中提取图像时,我遇到了这个问题。我的要求是获得相应的行/列,因为我想将图像与我读取的行相关联。


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");
 }

最新更新