Click here to Skip to main content
15,904,416 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i need to add the link of column (meetingID) while exporting the datatable to HTML

i added the output and code which iam using for conversion of Datatable to HTML

What I have tried:

public bool ExportToHtml(out string path)

        {
            DataTable dt = ToDataTable(_collection,_selectedColumns);
            StringBuilder strHTMLBuilder = new StringBuilder();
            strHTMLBuilder.Append("<html >");
            strHTMLBuilder.Append("<head>");
            strHTMLBuilder.Append("</head>");
            strHTMLBuilder.Append("<body>");
            strHTMLBuilder.Append("<table border='1px solid black' cellpadding='1' cellspacing='1' bgcolor='LightYellow' style='font-family:Garamond; font-size:medium; width=100%'>");

            strHTMLBuilder.Append("<tr width=100%  >");
            foreach (DataColumn myColumn in dt.Columns)
            {
                strHTMLBuilder.Append("<td style='font-size:medium; font-weight:bold;width=100%; padding-right: 30px' >");
                strHTMLBuilder.Append(myColumn.ColumnName);
                strHTMLBuilder.Append("</td>");

            }
            strHTMLBuilder.Append("</tr>");


            foreach (DataRow myRow in dt.Rows)
            {

                strHTMLBuilder.Append("<tr >");
                foreach (DataColumn myColumn in dt.Columns)
                {
                    strHTMLBuilder.Append("<td >");
                    strHTMLBuilder.Append(myRow[myColumn.ColumnName].ToString());
                    strHTMLBuilder.Append("</td>");

                }
                strHTMLBuilder.Append("</tr>");
            }

            //Close tags.  
            strHTMLBuilder.Append("</table>");
            strHTMLBuilder.Append("</body>");
            strHTMLBuilder.Append("</html>");

            string Htmltext = strHTMLBuilder.ToString();
            path = string.Format("{0}\\{1}.{2}", _basepath, _filename, "html");
            System.IO.File.WriteAllText(path, Htmltext);

            //return Htmltext;
            return true;

        }


<html ><head></head><body><table border='1px solid black' cellpadding='1' cellspacing='1' bgcolor='LightYellow' style='font-family:Garamond; font-size:medium; width=100%'><tr width=100%  ><td style='font-size:medium; font-weight:bold;width=100%; padding-right: 30px' >Meeting ID</td><td style='font-size:medium; font-weight:bold;width=100%; padding-right: 30px' >Portfolio Manager Name </td><td style='font-size:medium; font-weight:bold;width=100%; padding-right: 30px' >IVZ Meeting Cutoff Date GMT</td><td style='font-size:medium; font-weight:bold;width=100%; padding-right: 30px' >IVZ Meeting CutOff Date Local</td><td style='font-size:medium; font-weight:bold;width=100%; padding-right: 30px' >local time</td><td style='font-size:medium; font-weight:bold;width=100%; padding-right: 30px' >Issuer Name</td><td style='font-size:medium; font-weight:bold;width=100%; padding-right: 30px' >Ticker</td></tr><tr ><td >1190516</td><td >Josh Goldstein</td><td >2018-01-18 16:00:00</td><td >2018-01-18 10:00:00</td><td >CST/CDT</td><td >Siemens AG</td><td >SIE</td></tr></table></body></html>
Posted
Updated 8-Jan-18 21:24pm
Comments
Karthik_Mahalingam 9-Jan-18 4:23am    
like to what?
url ?

1 solution

I suggest you to add hyperlinks in sql query itself when you are forming table columns like below.

SELECT '<a href="'
       + HyperLink
       + '">'
       + TextLink
       + '</a>'
FROM YourTable

<or>

public bool ExportToHtml(out string path)

        {
            DataTable dt = ToDataTable(_collection,_selectedColumns);
            StringBuilder strHTMLBuilder = new StringBuilder();
            strHTMLBuilder.Append("<html >");
            strHTMLBuilder.Append("<head>");
            strHTMLBuilder.Append("</head>");
            strHTMLBuilder.Append("<body>");
            strHTMLBuilder.Append("<table border='1px solid black' cellpadding='1' cellspacing='1' bgcolor='LightYellow' style='font-family:Garamond; font-size:medium; width=100%'>");

            strHTMLBuilder.Append("<tr width=100%  >");
            foreach (DataColumn myColumn in dt.Columns)
            {
                strHTMLBuilder.Append("<td style='font-size:medium; font-weight:bold;width=100%; padding-right: 30px' >");
                strHTMLBuilder.Append(<big><pre>"<a href='"HyperLink"'>'"myColumn.ColumnName"'</a>"
);
strHTMLBuilder.Append("");

}
strHTMLBuilder.Append("");


foreach (DataRow myRow in dt.Rows)
{

strHTMLBuilder.Append("");
foreach (DataColumn myColumn in dt.Columns)
{
strHTMLBuilder.Append("");
strHTMLBuilder.Append(myRow[myColumn.ColumnName].ToString());
strHTMLBuilder.Append("");

}
strHTMLBuilder.Append("");
}

//Close tags.
strHTMLBuilder.Append("");
strHTMLBuilder.Append("");
strHTMLBuilder.Append("");

string Htmltext = strHTMLBuilder.ToString();
path = string.Format("{0}\\{1}.{2}", _basepath, _filename, "html");
System.IO.File.WriteAllText(path, Htmltext);

//return Htmltext;
return true;

}
Please up vote if this solution helps.
 
Share this answer
 
v5
Comments
Vadivel Murthy 9-Jan-18 3:42am    
@Debasish Mishra,

Thanks For Reply, the data , iam getting from Store Procedure and i want to give link for only one column (meetingId) first column
strHTMLBuilder.Append("");
strHTMLBuilder.Append(
"<a href=""HyperLink"">myColumn.ColumnName</a>"
for the above code it will create link for column , i want to link to column value , ex:-> the meetingid column have row value 1234 should have link
[no name] 9-Jan-18 4:28am    
Then do the same in second for each loop like i did it for column header and for first column data use the logic where count is always one for each data row and same for column header.

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