Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
476 views
in Technique[技术] by (71.8m points)

c# - OpenXML: Excel, extracting Cell text and Image/Picture data

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

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.

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

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

        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());
                }
            }
        }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
    [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);

    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...