Click here to Skip to main content
15,891,513 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
hi all,
I would like to export all images from an Image folder to an excel with all images on 1 excel worksheet(Please refer to link below). I have managed to export image to excel but only 1 image appeared on the excel worksheet instead of all images.

This is my code:
public void ExportToExcel()
        {
            //for export
            ExcelPackage objExcelPackage = new ExcelPackage();   //create new workbook

            string[] filesindirectory = Directory.GetFiles(Server.MapPath("~/Image"));
            int count = 0;

            ExcelWorksheet ws = objExcelPackage.Workbook.Worksheets.Add("Worksheet" + count); //create new worksheet

                        foreach (string img in filesindirectory)
                        {
                        count++;
                        System.Web.UI.WebControls.Image TEST_IMAGE = new System.Web.UI.WebControls.Image();
                        System.Drawing.Image myImage = System.Drawing.Image.FromFile(img);
                        var pic = ws.Drawings.AddPicture(count.ToString(), myImage);
                        // Row, RowoffsetPixel, Column, ColumnOffSetPixel
                        pic.SetPosition(1, 0, 2, 0);
                            }
                    }                            
            var filepath = new FileInfo(@"C:\Users\user\Desktop\Test\" + datetime.ToString("dd-MM-yyyy_hh-mm-ss") + ".xlsx");
            objExcelPackage.SaveAs(filepath);
        }

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

Question: How to export all images from a directory to excel in 1 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