gpt4 book ai didi

c# - OpenXML:Excel,提取单元格文本和图像/图片数据

转载 作者:太空狗 更新时间:2023-10-30 01:23:09 26 4
gpt4 key购买 nike

我正在编写代码以将单元格和单元格中的图片/图像从 Excel 文档提取到数据库中。

使用这段代码,我可以获得不是图片的单元格值。

但是,Cell 对象中没有图片。请注意,每一行都以图片开头。

A1- Picture
B1- Text
C1- Text and so on.

如何从“A”列获取图像?

        WorkbookPart wbPart = document.WorkbookPart;
var workSheet = wbPart.WorksheetParts.FirstOrDefault();

foreach (Row r in workSheet.Worksheet.Descendants<Row>())
{
//Note only has Colum B,C. Not A as A has an image???
foreach(Cell c in r.ChildElements)
{
foreach(object o in c.ChildElements)
{
Console.WriteLine(c.CellReference + " " + o.ToString());
}
}
}

最佳答案

    [Test]
public void IterateThruImages()
{
WorkbookPart wbPart = document.WorkbookPart;
var workSheet = wbPart.WorksheetParts.FirstOrDefault();

foreach (ImagePart i in workSheet.DrawingsPart.ImageParts)
{
var rId = workSheet.DrawingsPart.GetIdOfPart(i);

Stream stream = i.GetStream();
long length = stream.Length;
byte[] byteStream = new byte[length];
stream.Read(byteStream, 0, (int)length);

var imageAsString = Convert.ToBase64String(byteStream);
Console.WriteLine("The rId of this Image is '{0}'",rId);
}
}


[Test]
public void GetImageRelationshipIdAndImageOfThatId()
{
string row = "1";
string col = "0";

WorkbookPart wbPart = document.WorkbookPart;
var workSheet = wbPart.WorksheetParts.FirstOrDefault();

TwoCellAnchor cellHoldingPicture = workSheet.DrawingsPart.WorksheetDrawing.OfType<TwoCellAnchor>()
.Where(c => c.FromMarker.RowId.Text == row && c.FromMarker.ColumnId.Text == col).FirstOrDefault();

var picture = cellHoldingPicture.OfType<DocumentFormat.OpenXml.Drawing.Spreadsheet.Picture>().FirstOrDefault();
string rIdofPicture = picture.BlipFill.Blip.Embed;

Console.WriteLine("The rID of this Anchor's [{0},{1}] Picture is '{2}'" ,row,col, rIdofPicture);

ImagePart imageInThisCell = (ImagePart)workSheet.DrawingsPart.GetPartById(rIdofPicture);

}

关于c# - OpenXML:Excel,提取单元格文本和图像/图片数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11993979/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com