Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi all,
I have exported images from a folder to excel as shown below. There will be two images side by side each other everytime. I would like to know how can I set specific position for the same image that is being displayed out multiple times.

For example, now the repeated image is being displayed at the same start and end position of the image next to it. I want it to start to be displayed 3 rows after the image next to it has started, and end of display 3 rows before the image next to it has ended (Please see the image below).

This is the output I have currently:
http://i.stack.imgur.com/iGyE1.png[^]
This is the output I want to achieve:
http://i.stack.imgur.com/WA0nU.png[^]

This is my code: (same image to display multiple times: repeatimage, image next to repeatedimage: myImage)
C#
public void ExportToExcel()
    {
        //for export

            ExcelPackage objExcelPackage = new ExcelPackage();   //create new workbook

            string[] filesindirectory = Directory.GetDirectories(Server.MapPath("~/Folder"));
            string repeatimagepath=@"C:\Users\user\Desktop\Project\Project1\Project1\NewProject\NewProject\Image\repeatimage.png";
            int count = 0;
            int count1 = 0;
            int x = 25;
            int finalValue = 0;
            int finalValue1 = 0;

            foreach (string subdir in filesindirectory)
            {
                count++;
                string[] splitter = subdir.Split('\\');
                string folderName = splitter[splitter.Length - 1];
                ExcelWorksheet ws = objExcelPackage.Workbook.Worksheets.Add(folderName); //create new worksheet
                count1 = 0;
                foreach (string img in Directory.GetFiles(subdir))
                {
                    count1++;
                    System.Drawing.Image myImage = System.Drawing.Image.FromFile(img);
                    System.Drawing.Image repeatimage= System.Drawing.Image.FromFile(repeatimagepath);
                    var pic = ws.Drawings.AddPicture(count1.ToString(), myImage);

                    //for (a = 0; a < 5; a++)
                    //{
                        //count2++;
                        var picture = ws.Drawings.AddPicture("i" + count1.ToString(), repeatimage);   
                        //picture.SetPosition(a * 5, 0, 2, 0);


                        // Row, RowoffsetPixel, Column, ColumnOffSetPixel
                        if (count1 > 1)
                        {
                            pic.SetPosition(finalValue, 0, 2, 0);
                            picture.SetPosition(finalValue1, 0, 15, 0);
                            picture.SetSize(30, 400);
                            finalValue += (x + 1); // Add 1 to have 1 row of empty row
                            finalValue1 += (x + 3);
                        }                                
                        else
                        {
                            pic.SetPosition(count1, 0, 2, 0);
                            picture.SetPosition(count1, 0, 15, 0);
                            picture.SetSize(30, 400);
                            finalValue = (count1 + x) + 1; // Add 1 to have 1 row of empty
                            finalValue1 = (count1 + x) + 3;
                        }
                    //}
                    myImage.Dispose();
                    repeatimage.Dispose();
                }
            }

            var filepath = new FileInfo(@"C:\Users\user\Desktop\Test\" + datetime.ToString("dd-MM-yyyy_hh-mm-ss") + ".xlsx");
            objExcelPackage.SaveAs(filepath);
    }

Thanks.
Posted
Updated 25-Oct-15 22:17pm
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900