繁体   English   中英

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

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

让我们假设我有一个名为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 以查看您的图像。 当然有更有效的方法来做到这一点,但这是一个快速而肮脏的解决方案,可以完成工作。

当我正在寻找 myslef 使用 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");
 }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM