简体   繁体   中英

Insert Same Image Multiple Times Into Excel Using EPplus

I want to insert the same image multiple times into excel using EPplus method. I researched online and tried the method in this link( Adding images into Excel using EPPlus ) but I received the following error 'Name already exists in the drawings collection' on this line:

var picture = ws.Drawings.AddPicture(a.ToString(), repeatimage);

This is my code:

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";
        Bitmap repeatimage= new Bitmap(repeatimagepath);
        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);
                var pic = ws.Drawings.AddPicture(count1.ToString(), myImage);  

                **for (int a = 0; a < 5; a++)
                {
                    var picture = ws.Drawings.AddPicture(a.ToString(), repeatimage);
                    picture.SetPosition(a * 5, 0, 2, 0);
                }**

                // 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();
                repeatimage.Dispose();
            }
        }

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

Question: How can I insert the same image multiple times into excel using EPplus?

Please help me on this, thanks.

The error already told you the answer
This line your count1 is 1

var pic = ws.Drawings.AddPicture(count1.ToString(), myImage);  

Then this line

for (int a = 0; a < 5; a++)
{
    var picture = ws.Drawings.AddPicture(a.ToString(), repeatimage);
    picture.SetPosition(a * 5, 0, 2, 0);
}

Your a will be 0 , 1 , 2 , 3 , 4 as the name of the picture
You see the error ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM