Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
140
Preserving Excel 2007 Features when using Templates, Code examples to insert Images
posted

Dear Infragistics Support,

  1. I have an Excel XLSX template I'd like to use so that I can have the header and footer already set up the way I want them to.  There are images in both the header and footer. If I simply open the Template and then immediately save it with no changes I get an error.  Obviously this has to be addressed before I can consider making changes to the workbook before saving.  The Excel template is an embedded resource, so I get the stream for it using Application.GetResourceStream, and then open the stream using Excel.Workbook.Load.  I get the error when I try to save the workbook using Workbook.Save(to_another_stream). I seem to recall that for the 2011.1 release there were issues with Excel preserving certain advanced worksheet features involving images.  I am currently using the 2011.2 release. Is this what's happening here?  Perhaps this has already been fixed in a more recent release?
  2. I'd like the option of not using a template but I couldn't find any detailed code examples of how to put images in parts of the Excel headers and footers.  Can you provide some?
  3. I'd also like to have detailed code examples for putting images in a cell.  In this case, I'd also like to know if it is possible to make it so the image does not re-size but simply moves when the row or column containing the cell with an image in it is re-sized, preserving the original aspect ratio of the image.  Each image would be anchored completely within one cell so that it appears to be a cell value.

Below is the error I am getting (referred to in 1. above) when trying to save the unmodified template XLSX file back to disk:

'summaryBelow' is a duplicate attribute name (at XmlWellFormedWriter.AddAttribute)

The Excel Template file I'm using is attached.

Thanks!

ExcelTemplate.zip
Parents
No Data
Reply
  • 44743
    posted

    The error you are describing seems to be fixed in the 11.2.20112.2068 Silverlight SR and later. Unfortunately, there is currently no support for round-tripping header/footer images, so they are lost when saving and loading. It may be possible to add this support as a bug fix, so I have forwarded this post to the Developer Support Manager and a DS engineer will be contacting you about this issue. However, if the scope of this support is too large, it may need to be implemented as a new feature.

    As far as adding images to cells, I don't think Excel supports the scenario you are describing: having an image move and size with cells but keep its aspect ratio. If I am wrong, please let me know how do to this. But I think your best option here would be to size the image to the cell and let it move with cells, but not size with them. That way the image size would never be changed automatically and you can ensure the aspect ratio is maintained. The following code will center the image in cell D12 while maintaining the aspect ratio:

    Workbook workbook = new Workbook(WorkbookFormat.Excel2007);

    Worksheet worksheet = workbook.Worksheets.Add("Sheet1");

    WorksheetImage image = new WorksheetImage(...);

    image.SetBoundsInTwips(worksheet, worksheet.GetCell("D12").GetBoundsInTwips(), true);

    image.PositioningMode = ShapePositioningMode.MoveWithCells;

    worksheet.Shapes.Add(image);

Children