none
Copy Image from One excel sheet to another

    Question

  • 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

    Monday, April 30, 2012 3:26 PM

Answers

  • 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

    • Marked as answer by Kishor Reddy Thursday, May 03, 2012 5:26 PM
    Thursday, May 03, 2012 5:26 PM

All replies

  • 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

    Tuesday, May 01, 2012 8:54 AM
  • 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

    Tuesday, May 01, 2012 6:13 PM
  • 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

    • Marked as answer by Kishor Reddy Thursday, May 03, 2012 5:26 PM
    Thursday, May 03, 2012 5:26 PM
  • hello Kishor Reddy excuse me wonder if you still have the code to copy image from one sheet to another...

    thanks

    Tuesday, March 12, 2013 9:06 PM