Unreadable content when using template with image in the footer and insert image in the content

Mar 16, 2011 at 9:27 AM

Hi,

Thanks a lot for the great work.

I got the error "Excel found unreadable content, Do you want to recover ......" when I click recover the work sheet is empty

My case is : I had a template with a footer, the footer contain an image

When I write data (JUST TEXT) to that template, it open correctl:

using (ExcelPackage package = new ExcelPackage(newFile, templateFile))
            {
                //Open worksheet 1
                ExcelWorksheet worksheet = package.Workbook.Worksheets[1];
                worksheet.InsertRow(5, 2);

                worksheet.Cells["A5"].Value = "12010";

 package.Save();
                return "";

}

When I try to write image to that template, I got the above error

 using (ExcelPackage package = new ExcelPackage(newFile, templateFile))
            {
                //Open worksheet 1
                ExcelWorksheet worksheet = package.Workbook.Worksheets[1];
                worksheet.InsertRow(5, 2);

                worksheet.Cells["A5"].Value = "12010";

int currentRow = 5;
                int pictureCol = 1;
                int pictureWidth = 310;
                int pictureHeight = 120;
               

                //set picture column width to accommodate the picture
                //worksheet.Column(pictureCol).Width = ExcelHelper.Pixel2ColumnWidth(worksheet, pictureWidth + 1);

                using (System.Drawing.Image img = Image.FromFile(@"C:\Users\Moe\Downloads\c# Excel\trunk\SampleApp\1.png"))
                {
                    if (img != null)
                    {
                        //set row height to accommodate the picture
                        //worksheet.Row(currentRow).Height = ExcelHelper.Pixel2RowHeight(pictureHeight + 1);

                        //add picture to cell
                        ExcelPicture pic = worksheet.Drawings.AddPicture("PictureUniqueName", img);
                        //position picture on desired column
                        pic.From.Column = pictureCol - 1;
                        pic.From.Row = currentRow - 1;
                        pic.From.ColumnOff = ExcelHelper.Pixel2MTU(1);
                        pic.From.RowOff = ExcelHelper.Pixel2MTU(1);
                        //set picture size to fit inside the cell
                        pic.SetSize(pictureWidth, pictureHeight);
                    }
                }

                package.Save();
                return "";

}

Can you help me?

Mar 16, 2011 at 10:04 AM

I figure it out, the Excel file has to have the same pic in the content (I had to set width and height for the pic in the content to zero)

Mar 23, 2012 at 6:02 PM

You can add picture in excel cell like this

xlWorkSheet.Shapes.AddPicture("C:\\csharp-xl-picture.JPG", Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoCTrue, 50, 50, 300, 45);

find the full source code of this C# code

http://csharp.net-informations.com/excel/csharp-insert-picture-excel.htm

patter.