Click here to Skip to main content
15,886,067 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am trying to export some html to excel using below code.


C#
objstrbuild.Append(@"<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.0 Transitional//EN"">");
         objstrbuild.Append("<head>");

         objstrbuild.Append("<style type='text/css'>.divBlockcss{height:30px;padding:5px;width:150px;background-color: #67da67;}</style>");



         objstrbuild.Append("</head>");
         objstrbuild.Append("<body>");
         if (ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
         {

             objstrbuild.Append("<table border='1'>");
             objstrbuild.Append("<tr>");
             for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
             {
                 objstrbuild.Append("<td style='border='1'>");





                     if (ds.Tables[0].Rows[i]["PositionCode"].ToString() != "*")
                     {


                         for (int j = Convert.ToInt16(ds.Tables[0].Rows[i]["PositionLevel"]); j>0 ; j--)
                         {

                             objstrbuild.Append("<div>");


                             objstrbuild.Append("<div  style='height:30px;padding:5px;width:150px;background-color: #67da67;'>");


                             for (int k = 0; k < ds.Tables[1].Rows.Count; k++)
                             {
                                 if (ds.Tables[0].Rows[i]["PositionID"].ToString() == ds.Tables[1].Rows[k]["PositionID"].ToString() && (j) == Convert.ToInt32(ds.Tables[1].Rows[k]["Level"]) && ds.Tables[0].Rows[i]["BlockNumber"].ToString() == ds.Tables[1].Rows[k]["Block"].ToString())
                                 {
                                     objstrbuild.Append(ds.Tables[1].Rows[k]["ContainerCode"].ToString());
                                     break;
                                 }

                             }


                                 objstrbuild.Append("</div>");


                                 objstrbuild.Append("<div style='font-weight:bold;'>");

                             objstrbuild.Append(ds.Tables[0].Rows[i]["PositionCode"].ToString() + "(Level-" + j.ToString() + ")");

                             objstrbuild.Append("</div>");


                             objstrbuild.Append("</div>");

                         }

                     }
                     else
                     {
                         objstrbuild.Append("<div>");
                         objstrbuild.Append("</div>");
                     }
                     objstrbuild.Append("</td>");
                 }


             objstrbuild.Append("</tr>");
             objstrbuild.Append("</table>");
         }
         objstrbuild.Append("</body>");
         objstrbuild.Append("</html>");


         Response.Clear();
         Response.AddHeader("Content-Disposition", "attachment;filename=" + "filename" + ".xls");
         Response.ContentType = "application/ms-excel";
         Response.ContentEncoding = System.Text.Encoding.Unicode;
         Response.BinaryWrite(System.Text.Encoding.Unicode.GetPreamble());

         Response.Cache.SetCacheability(HttpCacheability.NoCache); // not necessarily required
         Response.Charset = "";
         Response.Output.Write(objstrbuild.ToString());
         Response.End();



but somehow the background color not applying to the cell in excel. some one can please tell me what i am doing wrong in my code.
Posted
Updated 19-Sep-14 0:32am
v2
Comments
gggustafson 20-Sep-14 17:28pm    
Missing required <title></title>

In <td style='border='1'> the style literal is missing its closing delimiter

I suggest that you place your desired HTML into a HTML page and try to display it before you start playing with StringBuilder. You might also consider using the W3C Markup Validation Service (http://validator.w3.org/).

Also, you are not using the style divBlockcss that you defined in the head

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