Click here to Skip to main content
15,741,981 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have image in one column of grid view along with some value. When I tried to export it in excel all goes well except image is not exported. It shows blank image icon in that column with value. Tried below code for this. I want image in that column. plz help.In worst case plz suggest how I can remove that blank image icon from that column. As it has value with image I can't simply delete that column

What I have tried:

C#
public static void MultitbleDataset2SingleSheetGridExport(GridView[] gv, String filename)
   {
       string Filename = filename + ".xls";
       //if (ds == null || ds.Tables.Count == 0)
       //{
       //    throw new ArgumentException("DataSet needs to have at least one DataTable", "dataset");
       //}
       HttpResponse Response = System.Web.HttpContext.Current.Response;
       Response.Clear();
       Response.Buffer = true;
       Response.ClearContent();
       Response.AddHeader("content-disposition", "attachment;filename=" + Filename);
       Response.Cache.SetCacheability(HttpCacheability.NoCache);
       Response.ContentType = "application/ms-excel";
       StringWriter sw = new StringWriter();
       HtmlTextWriter htw = new HtmlTextWriter(sw);

       Table tb = new Table();
       for (int i = 0; i < gv.Length; i++)
       {
           GridView gridvw = new GridView();
           // string grid = ConvertDataTableToHTML(ds.Tables[i]);
           //gridvw.DataSource = gv[i];
           //gridvw.DataBind();
          PrepareForExport(gv[i]);

           TableRow tr1 = new TableRow();
           TableCell cell1 = new TableCell();
           cell1.Controls.Add(gv[i]);
           tr1.Cells.Add(cell1);
           TableCell cell2 = new TableCell();
           cell2.Text = "&nbsp;";
           TableRow tr2 = new TableRow();
           tr2.Cells.Add(cell2);
           TableRow tr3 = new TableRow();
           tb.Rows.Add(tr1);
           tb.Rows.Add(tr2);
           tb.Rows.Add(tr3);

       }
       tb.RenderControl(htw);

       //style to format numbers to string
       string style = @"<style> .textmode { mso-number-format:\@; } </style>";
       Response.Write(style);
       Response.Output.Write(sw.ToString());
       Response.Flush();
       Response.End();
   }

   public static void PrepareForExport(GridView Gridview)
   {

         Gridview.DataBind();

       //Change the Header Row back to white color
       Gridview.HeaderRow.Style.Add("background-color", "#FFFFFF");

       //Apply style to Individual Cells
       for (int k = 0; k < Gridview.HeaderRow.Cells.Count; k++)
       {
           Gridview.HeaderRow.Cells[k].Style.Add("background-color", "#436F9F");
       }

       for (int i = 0; i < Gridview.Rows.Count; i++)
       {
           GridViewRow row = Gridview.Rows[i];

           //Change Color back to white
           row.BackColor = System.Drawing.Color.White;
           row.Attributes.Add("class", "textmode");
           row.Attributes.Remove("image");
           //Apply style to Individual Cells of Alternating Row
           if (i % 2 != 0)
           {
               for (int j = 0; j < Gridview.Rows[i].Cells.Count; j++)
               {
                   row.Cells[j].Style.Add("background-color", "#C2D69B");
               }
           }
       }
   }
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