Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
hi Members,

I have 1 directory with many subdirectories. Inside each subdirectory there are many images. I have successfully exported all images from each subdirectory into one excel worksheet each on 1 excel workbook.
For e.g. if I have 10 subdirectories, I will have 1 excel workbook with 10 excel worksheets, then in each excel worksheet there will be images from each subdirectory.

What I would like to do now is there's an image 'ColorBar' in my image folder, I want to add this image many times in each excel worksheet based on the number of images there are in each worksheet.
For e.g. if there are 3 images in worksheet1 and 2 images in worksheet2, I want to add 'ColorBar' image 3 times in worksheet1 and 2 times in worksheet2 (Please refer to links below).

This is my code:
C#
public void ExportToExcel()
        {
            //for export

                ExcelPackage objExcelPackage = new ExcelPackage();   //create new workbook
            
                string[] filesindirectory = Directory.GetDirectories(Server.MapPath("~/Folder"));

                int count = 0;
                int count1 = 0;
                int x = 25;
                int finalValue = 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.Web.UI.WebControls.Image TEST_IMAGE = new System.Web.UI.WebControls.Image();
                        System.Drawing.Image myImage = System.Drawing.Image.FromFile(img);


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

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

This is my image folder path:
C#
C:\Users\user\Desktop\Project\Project1\Project1\NewProject\NewProject\Image

This is the output I have currently:
http://i.stack.imgur.com/lIalH.png[^]

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

Question: How to add same image from Image folder multiple times to each excel worksheet using EPplus?

Please help me on this, thanks!
Posted

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