Export Data From Database Table to html File in ado.net c#
  • please how i can Export Data From Database Table to html  File in  ado.net c#  thank you so much :) .
  • 3 Comments sorted by
  • Vote Up0Vote Down atirnaveedatirnaveed
    17.50 Karma Accepted Answer
        SqlConnection xcon = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
                DataTable xtable = new DataTable();
                SqlDataAdapter xadp = new SqlDataAdapter("Select * from tblChartOfAccount", xcon);
                xadp.Fill(xtable);
                string table = "<table>";
                table += "<tr>";
                foreach (DataColumn item in xtable.Columns)
                {
                    table += "<th>";
                    table += item.ColumnName;
                    table += "</th>";
                }
                table += "</tr>";
                for (int i = 0; i < xtable.Rows.Count; i++)
                {
                    table += "<tr>";
                    for (int j = 0; j < xtable.Columns.Count; j++)
                    {
                       
                        table += "<td>" + xtable.Rows[i][j] + "</td>";

                    } 
                    table += "</tr>";
                }
                table += "</table>";
                FileStream str = new FileStream(AppDomain.CurrentDomain.BaseDirectory + "/HtmlFiles/" + Guid.NewGuid() + ".html", FileMode.OpenOrCreate);
                StreamWriter strw = new StreamWriter(str);
                strw.Write(table);
                strw.Flush();
                strw.Dispose();
                str.Dispose();
  • Vote Up0Vote Down atirnaveedatirnaveed
    17.50 Karma Accepted Answer
    here is an example 
    SaveHtml.rar
    39K
  • What if you don't want to include all the columns?

Howdy, Stranger!

It looks like you're new here. If you want to get involved, or you want to Ask a new Question, Please Login or Create a new Account by Clicking below

Login with Facebook

Popular Posts of the Week

Optimum Creative