Copy Image from One excel sheet to another

Answered Copy Image from One excel sheet to another

  • 2012年4月30日 15:26
     
     

    Hi,

    I have a requirement that i need to copy data from one excel sheet to another sheet. For example "Sheet1" contains some data (images, comments etc). I have image and few cells in "Sheet2".

    Now, i want to copy contents of "Sheet2" at the end of contents of "Sheet1". I was able to copy entire sheet including images but i am not able to position the image in the destination sheet.

    To be more clear, on "Sheet2", the image is located at Row Number : 7. On "Sheet1", i want the image to be placed at Row Number: 30.

    I have tried modifing "RowId.Text" value but the output is getting opened with Errors on Drawing part.

    Can anyone help me on this ?

    Thanks and Regards,

    YKK Reddy

全部回复

  • 2012年5月1日 8:54
    版主
     
     

    Hi Kishor,

    Thanks for posting in the MSDN Forum.

    This image have been stored in DrawingsPart in each WorksheetPart and the postition has been defined in WorksheetDrawing element of the DrawingsPart.

    I hope it can help you.

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us

  • 2012年5月1日 18:13
     
     

    Hi Tom,

    Thanks for your reply.

    I cloned the child elements of Source Worksheet Drawings and tried to append to destination worksheet drawing. In the end, i found that the image which is already present in the destination sheet keeps on pasting again and again.

    Can you help me overcome this problem?

    I am posting a sample code for your reference.

            private void CopyImageFromRange(WorksheetPart srcWorkSheetPart, WorksheetPart destWorkSheetPart, int nImageOffset)
            {
                if (null != destWorkSheetPart.DrawingsPart.WorksheetDrawing)
                {
                    if (null != srcWorkSheetPart.DrawingsPart.WorksheetDrawing)
                    {
                        foreach (OneCellAnchor Item in srcWorkSheetPart.DrawingsPart.WorksheetDrawing.Descendants<OneCellAnchor>().ToList())
                        {
                            OneCellAnchor anchor = (OneCellAnchor)Item.Clone();
                            anchor.FromMarker.RowId.Text = (Convert.ToInt16(anchor.FromMarker.RowId.Text) + nImageOffset).ToString();
                            destWorkSheetPart.DrawingsPart.WorksheetDrawing.Append(anchor);
                        }
                    }
                }

                m_objSpreadSheetDocument.WorkbookPart.Workbook.Save();
            }

    Thanks and Regards,

    YKK Reddy

  • 2012年5月3日 17:26
     
     已答复

    Hi,

    I solved it myself and thought of posting it to forum so that anyone facing the same problem can be benefited.

    I followed the following steps:

    1. Iterate through all the Image parts (present under Drawing Part) of source worksheet and add them to destination worksheet Drawing part.

    2. Now get the "WorksheetDrawing" of Source Worksheet DrawingPart and add get the Child elements. Add them to Destination Worksheets drawing part.

    3. While adding to "WorksheetDrawing" of Destination worksheet drawing, assign a parameter (Picture.BlipFill.Blip.Embed) to the newly added Image part's rID.

    I can share the code also in case anyone needs...

    Thanks and Regards

    YKK Reddy

  • 2013年3月12日 21:06
     
     

    hello Kishor Reddy excuse me wonder if you still have the code to copy image from one sheet to another...

    thanks