Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
hi, I have one image folder with multiple images inside. I want to export all images from this folder into excel, does anyone know how I can accomplish this?

I am able to export 1 image from that folder to excel but unable to export multiple images to excel.

This is my codes:
C#
protected void EXPORT_BUTTON_Click(object sender, EventArgs e)
        {
         ExportToExcel();
        }

public void ExportToExcel()
        {
            string[] filesindirectory = Directory.GetFiles(Server.MapPath("~/Image"));

                    foreach (string img in filesindirectory)
                    {
                        using (StringWriter sw = new StringWriter())
                        {
                            using (HtmlTextWriter hw = new HtmlTextWriter(sw))
                            {
                        Table table = new Table();

                        System.Web.UI.WebControls.Image TEST_IMAGE = new System.Web.UI.WebControls.Image();
                        TEST_IMAGE.ImageUrl = "~/Image" + img;
                        TEST_IMAGE.ImageUrl = this.GetAbsoluteUrl(TEST_IMAGE.ImageUrl);
                        TableRow row = new TableRow();
                        row.Cells.Add(new TableCell());
                        row.Cells[0].Controls.Add(TEST_IMAGE);
                        table.Rows.Add(row);
                        table.RenderControl(hw);

                        //Export the Table to Excel.

                        Response.Clear();

                        Response.Buffer = true;

                        Response.AddHeader("content-disposition", "attachment;filename=Images.xls");

                        Response.Charset = "";

                        Response.ContentType = "application/vnd.ms-excel";

                        //Write the HTML string to Response.

                        Response.Write(sw.ToString());

                        Response.Flush();

                        Response.End();
                    }

                }
            }
        }

public override void VerifyRenderingInServerForm(Control control)
        {
            return;
        }

        private string GetAbsoluteUrl(string relativeUrl)
        {
            relativeUrl = relativeUrl.Replace("~/", string.Empty);

            string[] splits = Request.Url.AbsoluteUri.Split('/');

            if (splits.Length >= 2)
            {
                string url = splits[0] + "//";

                for (int i = 2; i < splits.Length - 1; i++)
                {
                    url += splits[i];

                    url += "/";
                }
                return url + relativeUrl;
            }
            return relativeUrl;
        }

Question: How to export multiple images from an image folder into Excel using C#?
Thanks for any help!
Posted
Updated 15-Oct-15 20:26pm
v2

1 solution

you can get the all the files in image folder as below
C#
string[] filesindirectory = Directory.GetFiles(Server.MapPath("~/Image"));

then create loop and add images to excel sheet
C#
Table table = new Table();
foreach(string img in filesindirectory)
    {
   System.Web.UI.WebControls.Image TEST_IMAGE = new System.Web.UI.WebControls.Image();
   TEST_IMAGE.ImageUrl = "Image/"+ Path.GetFileName(img) ;
   TEST_IMAGE.ImageUrl = this.GetAbsoluteUrl(TEST_IMAGE.ImageUrl);
   TableRow row = new TableRow();
   row.Cells.Add(new TableCell());
   row.Cells[0].Controls.Add(TEST_IMAGE);
       table.Rows.Add(row);
    }
   //rest of your code .....
 
Share this answer
 
v2
Comments
Member 11999641 16-Oct-15 2:26am    
hi DamithSL, I tried your method but not sure why the image can't be printed out in excel, an 'x' appeared as the image. Please take a look at my updated post thanks a lot!
DamithSL 16-Oct-15 3:52am    
small correction, use Path.GetFileName(img)
check my updated answer
Member 11999641 16-Oct-15 4:43am    
hi DamithSL, it works! Thanks for your help! :)

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