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

时间:2021-05-05 22:20:07

I am writing code to extract the cells and pictures/images in cells from an Excel doc into a database.

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

With this code I can get the cell values that are not Pictures.

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

However, the Cell object doesn't have a picture it in. Note that every row starts with an image.

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

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

How do I get the image from the "A" column?

如何从“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());
                }
            }
        }

1 个解决方案

#1


4  

    [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);

    }

#1


4  

    [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);

    }