admin管理员组文章数量:1334923
I am currently trying to extract all images and their positions from an Excel file in C#. It works fine if the image is placed over the cells. In that case, I can use ClosedXML.Excel
and retrieve them with the following code:
using (var workbook = new XLWorkbook(stream))
{
var worksheet = workbook.Worksheet(1);
foreach(var pictures in worksheet.Pictures)
{
..
}
}
The problem is that if you insert the image into a cell, then Pictures
becomes empty, and the image cannot be found.
So, then I tried using DocumentFormat.OpenXml
, which at least allows me to locate the images like this:
using (SpreadsheetDocument document = SpreadsheetDocument.Open("C:\\test.xlsx", false))
{
var package = document.GetPackage();
foreach (var part in package.GetParts())
{
if (part.ContentType.StartsWith("image/"))
{
using (var stream = part.GetStream(FileMode.Open, FileAccess.Read))
using (var fileStream = new FileStream("testimage.png", FileMode.Create, FileAccess.Write))
{
stream.CopyTo(fileStream);
}
}
}
}
This gives me all the images, but it doesn't provide any information about their positions.
Anyone have an idea on how to get the position or what cell the image belongs too? And just to be clear, this is when the image is placed in the cell. If it is placed over the cell it works fine.
本文标签: How can I extract image placed inside a cell in excel with CStack Overflow
版权声明:本文标题:How can I extract image placed inside a cell in excel with C#? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742375923a2463160.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论