Link to home
Start Free TrialLog in
Avatar of rsgage
rsgageFlag for United States of America

asked on

Export DataGridView to HTML Page

Hello all. I have tried to find an answer to this question and I have not had any success. My issue is this. I have a comboBox that lists different formats to export a report to. One of those options is HTML. Basically what I want to do is take the dataGridView in a windows form and export as is to an HTML page. I would like to just export to an HTML table. I don't even know how to start on this so I don't have any sample code to provide. I am using c sharp 2008. Any advise would be greatly appreciated.
ASKER CERTIFIED SOLUTION
Avatar of Dmitry G
Dmitry G
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rsgage

ASKER

That works. I am closer than I was. I can now get ti inot the HTML table but if the cell is empty it does not create the cell border. I have included the code that I used. Thanks so much for your help.
string strPageTitle = "HTML Report";
            string filler = "none";
           
            StringBuilder sb = new StringBuilder();
            StreamWriter wr = new StreamWriter(txtFile);
 
 
            sb.Append("<html>");
            sb.Append("<head>");
            sb.Append("<title>");
            sb.Append(strPageTitle);
            sb.Append("</title>");
            sb.Append("<style>");
            sb.Append(".Header {color: #FFFFFF; font-family: Verdana, Arial, Helvetica, sans-serif;font-size: x-small;font-weight: bold;}");
            sb.Append(".rows {font-family: Verdana, Arial, Helvetica, sans-serif; font-size: x-small; }");
            sb.Append("</style>");
            sb.Append("</head>");
            sb.Append("<body>");
            sb.Append(@"<table width='90%'  border='1' align='center' cellpadding='2' cellspacing='0'>");
            
            if (dgv.Rows.Count > 0)
            {
                foreach (DataGridViewColumn col in dgv.Columns)
                {
                    if (col.Index == dgv.Columns.Count - 1)
                    {
                        sb.Append("<td>");
                        sb.Append(string.Concat(col.HeaderText));
                        sb.Append("</td>");
                        
                    }
                    else
                    {
                        sb.Append("<td>");
                        sb.Append(string.Concat(col.HeaderText));
                        sb.Append("</td>");
                    }
                }
                foreach (DataGridViewRow row in dgv.Rows)
                {
                    //Add rows to the table for the length of the dataset
                    sb.Append("  <tr>");
                    foreach (DataGridViewCell cell in row.Cells)
                    {
                        if (cell.OwningColumn.Index == dgv.Columns.Count - 1)
                        {
                            if (cell.Value != null)
                            {
                                sb.Append("<td>");
                                sb.Append(string.Concat(cell.Value.ToString(), ", "));
                                sb.Append("</td>");
                            }
                            else
                            {
                                sb.Append("<td>");
                                sb.Append(filler);
                                sb.Append("</td>");
                            }
                        }
                        else
                        {
                            if (cell.Value != null)
                            {
                                sb.Append("<td>");
                                sb.Append(string.Concat(cell.Value.ToString(), ", "));
                                sb.Append("</td>");
                            }
                            else
                            {
                                sb.Append("<td>");
                                sb.Append(string.Concat("", "none"));
                                sb.Append("</td>");
                            }
                        }
                    }
                }
                sb.Append("  </tr>");
                sb.Append("</table>");
                sb.Append("</body>");
                sb.Append("</html>");
                wr.Write(sb);
                wr.Close();

Open in new window

Avatar of code_me
code_me

hello
i just want to know that
what is that txtFile
in
   StreamWriter wr = new StreamWriter(txtFile);