i had a c# code where i was getting a table from sql database and mailing it as html table now i want to send two tables...how can i do that.
what i tried previously:
public static string HtmlTable(DataTable table)
{
try
{
string messageBody = "<font> " + "table value" + " </font><br><br>";
string empty_message = "Null DATA in table value";
if (table.Rows.Count == 0)
{
return empty_message;
}
string htmlTableStart = "<table style=\"border-collapse:collapse; text-align:center;\" >";
string htmlTableEnd = "</table>";
string htmlHeaderRowStart = "<tr style =\"background-color:#6FA1D2; color:#ffffff;\">";
string htmlHeaderRowEnd = "</tr>";
string htmlTrStart = "<tr style =\"color:#555555;\">";
string htmlTrEnd = "</tr>";
string htmlTdStart = "<td style=\" border-color:#5c87b2; border-style:solid; border-width:thin; padding: 5px;\">";
string htmlTdEnd = "</td>";
messageBody += htmlTableStart;
messageBody += htmlHeaderRowStart;
foreach (DataColumn column in table.Columns)
messageBody += htmlTdStart + column + htmlTdEnd;
messageBody += htmlHeaderRowEnd;
foreach (DataRow row in table.Rows)
{
messageBody += htmlTrStart;
foreach (var item in row.ItemArray)
{
messageBody += htmlTdStart;
messageBody += item;
messageBody += htmlTdEnd;
}
messageBody += htmlTrEnd;
}
messageBody += htmlTableEnd;
return messageBody;
}
catch (Exception ex)
{
return null;
}
}
the above code only generates one html table but what i need is i will be passing 2 tables. i need two tables to genertaed in my html. i will be passing two tables as arguments.on simple note i need 2 tables to be printed in my html body.
i will be passing two tables or data set which has two tables
public static string HtmlTable(DataTable table,datatable table2)
This could be as simple as including a builder method which will take in a list of DataTable using the seperation of concerns principle and build each table out, returning the end result for you to display to the client.
public static string HtmlTableBuilder(List<DataTable> tables)
{
string tableResponse = "";
foreach (var table in tables)
{
tableResponse += HtmlTable(table);
}
return tableResponse;
}
Related
I am using this command to get data from my query:
private void loadData()
{
numRows = 0;
try
{
data = gpso.gpEntitiesObject.ExecuteStoreQuery<My_Class>(sql);
dataGrid.ItemsSource = data;
dataGrid.IsReadOnly = true;
numRows = dataGrid.Items.Count;
numRowsTB.Text = "Number of rows: " + numRows;
}
catch (Exception ex)
{
statusBarTB.Text = "exception: " + ex.Message;
}
}
Although this works and I see the data in my DataGrid, I am not able to save the data to a CSV file since I am trying to reenumerate the query result again, which is not allowed.
What would be the right way to get the data and save it to CSV?
Should I use the DataGrid API to scroll trough the rows and get the data in a foreach() loop?
Here is my CSV function:
public static string exportToCSV(IEnumerable<My_Class> data, string separator)
{
string ishod = "";
string csvRed = "";
ishod += "Key1" + separator + "Key2" + separator;
ishod += "Key3" + separator + "Key4" + separator;
ishod += "\n";
foreach (My_Class item in data)
{
csvRed += item.Key1+ separator + item.Key2+ separator;
csvRed += item.Key3+ separator + item.Key4+ separator;
csvRed += "\n";
ishod += csvRed;
csvRed = "";
}
return ishod;
}
I followed #Clemens advice. I used
IEnumerable dataList = data.ToList<My_Class>();
dataGrid.ItemSource = dataList;
I then called
exportToCSV(dataList, "\t");
with dataList as parameter and it worked!
I have the following code, which adds rowspan to a datatable in an html string, but I want it not to receive as a parameter a datatable, but instead to receive as a parameter an html string in C#.
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("Subject");
dt.Columns.Add("Code");
dt.Columns.Add("Test");
dt.Columns.Add("Class");
dt.Rows.Add("Math", "9", "ABC", "D1");
dt.Rows.Add("Math", "9", "ABD", "D2");
dt.Rows.Add("Math", "9", "ABE", "D3");
dt.Rows.Add("Math", "9", "ABF", "D4");
dt.Rows.Add("Science", "91", "ABG", "D1");
dt.Rows.Add("Science", "91", "ABH", "D2");
dt.Rows.Add("Science", "91", "ABI", "D3");
dt.Rows.Add("English", "191", "ABJ", "D1");
Label1.Text = ConvertDataTableToHTML(dt);
}
public static string ConvertDataTableToHTML(DataTable dt)
{
string html = "<table border=1>";
//add header row
html += "<tr>";
for (int i = 0; i < dt.Columns.Count; i++)
{
html += "<td>" + dt.Columns[i].ColumnName + "</td>";
}
html += "</tr>";
//add rows
string sub = "";
for (int i = 0; i < dt.Rows.Count; i++)
{
html += "<tr>";
int count = dt.Select("Subject ='" + dt.Rows[i][0].ToString() + "'").Count();
for (int j = 0; j < dt.Columns.Count; j++)
{
if (j < 2)
{
if (sub != dt.Rows[i][0].ToString())
{
html += "<td rowspan='" + count + "'>" + dt.Rows[i][j].ToString() + "</td>";
}
continue;
}
html += "<td>" + dt.Rows[i][j].ToString() + "</td>";
}
sub = dt.Rows[i][0].ToString();
html += "</tr>";
}
html += "</table>";
return html;
}
This is the result that I hope
There are a few things wrong with the code, and a few ways you can improve your code:
<table border=1> is missing the quotes around the 1. It should be <table border='1'>.
You are missing the <thead> and <tbody> tags around the row tags.
Instead of constantly using +=, use a StringBuilder.
To make your code cleaner, you can change some of your for loops to foreach loops.
Your code should be changed to something like this:
public static string ConvertDataTableToHTML(DataTable dt)
{
StringBuilder builder = new StringBuilder();
//add header row
builder.Append("<table border='1'><thead><tr>");
foreach (DataColumn col in dt.Columns)
{
builder.Append("<td>");
builder.Append(col.ColumnName);
builder.Append("</td>");
}
builder.Append("</tr></thead><tbody>");
//add rows
string sub = "";
...
builder.Append("</tbody></table>");
return builder.ToString();
}
(Just apply the same concepts to the middle code that I removed.)
This is my code to send datagridview as email. This code works for me by just sending the data in the datagridview.
Please guide me how to add the table header in it. I want the email to be sent as whole table including the table header.
var client = new SmtpClient("smtp.gmail.com", 587);
client.EnableSsl = true;
client.Credentials = new NetworkCredential("from_mail", "password");
var mail = new MailMessage();
mail.From = new MailAddress("from_mail");
mail.To.Add("to_mail");
mail.IsBodyHtml = true;
mail.Subject = "test";
string mailBody = "<table width='100%' style='border:Solid 1px Black;'>";
foreach (DataGridViewRow row in dataGridView2.Rows)
{
mailBody += "<tr>";
foreach (DataGridViewCell cell in row.Cells)
{
mailBody += "<td>" + cell.Value + "</td>";
}
mailBody += "</tr>";
}
mailBody += "</table>";
//your rest of the original code
mail.Body = mailBody;
client.Send(mail);
MessageBox.Show("mail send");
this.Close();
For converting your DataGridView to HTML for sending it in an email, use the function below:
private StringBuilder DataGridtoHTML(DataGridView dg)
{
StringBuilder strB = new StringBuilder();
//create html & table
strB.AppendLine("<html><body><center><" +
"table border='1' cellpadding='0' cellspacing='0'>");
strB.AppendLine("<tr>");
//create table header
for (int i = 0; i < dg.Columns.Count; i++)
{
strB.AppendLine("<td align='center' valign='middle'>" +
dg.Columns[i].HeaderText + "</td>");
}
//Close the header row
strB.AppendLine("</tr>");
//create table body
for (int i = 0; i < dg.Rows.Count; i++)
{
strB.AppendLine("<tr>");
foreach (DataGridViewCell dgvc in dg.Rows[i].Cells)
{
strB.AppendLine("<td align='center' valign='middle'>" +
dgvc.Value.ToString() + "</td>");
}
strB.AppendLine("</tr>");
}
//table footer & end of html file
strB.AppendLine("</table></center></body></html>");
return strB;
}
I want to create an HTML table with my c# program.
I've tried this:
internal string CreateMailBody(mailObj dataObj)
{
string body = string.Empty;
body +=
"Dear all, there is my new table:" +
"TableNo: " + dataObj.Obj.Table.TableNumber+ "<br /> <br />";
body += "<table border='1'><tr><th>Line No</th><th>Table</th><th>Description</th><th>Count</th><th>Met</th><th>something</th></tr>";
foreach (var item in dataObj.Table.TableLineCollection)
{
body += "<tr><td>" + item.LineNumber +"</td>";
body += "<tr><td>" + item.Table+"</td>";
body += "<tr><td>" + item.Description+"</td></tr>";
}
return body;
}
Will it give a better solution for this ?
You may mix StringBuilder using the method AppendFormat and string literals (# in from of a string, so you can write multiple lines).
StringBuilder body = new StringBuilder();
body.AppendFormat (
#"Dear all, there is my new table:
TableNo: {0} <br /> <br />
<table border='1'><tr><th>Line No</th><th>Table</th><th>Description</th><th>Count</th><th>Met</th><th>something</th></tr>"
, dataObj.Obj.Table.TableNumber);
foreach (var item in dataObj.Table.TableLineCollection)
{
body.AppendFormat(
#"<tr><td> {0}</td>
<tr><td> {1}</td>
<tr><td> {2}</td></tr>",
item.LineNumber, item.Table, item.Description);
}
I m developing a web project in asp.net 3.5
I want to export datatable to Excel. But there are 20.000 rows in datatable. Sometimes timeout problem happens..
protected string Worksheet97_Header()
{
string s = "<tr>";
foreach (ExcelColumn col in Columns)
{
s += "<th>" + col.Header_Text + "</th>";
}
s+="</tr>";
return s;
}
protected string Worksheet97_Data()
{
string s = "";
try
{
for (int i = 0; i < data.Rows.Count; i++)
{
s += "<tr>";
foreach (ExcelColumn col in Columns)
{
if (col.Column_Type == "System.String")
s += "<td>" + data.Rows[i][col.Field_Name].ToString() + "</td>";
if (col.Column_Type == "System.DateTime")
s += "<td>" + Convert.ToDateTime(data.Rows[i][col.Field_Name]).ToString("dd.MM.yyyy HH:mm:ss") + "</td>";
if (col.Column_Type == "System.Int32")
s += "<td>" + data.Rows[i][col.Field_Name].ToString() + "</td>";
if ((col.Column_Type == "System.Double") |
(col.Column_Type == "System.Decimal") |
(col.Column_Type == "System.Int16") |
(col.Column_Type == "System.Int32") |
(col.Column_Type == "System.Int64"))
s += "<td>" + Convert.ToDouble(data.Rows[i][col.Field_Name]).ToString("0.00") + "</td>";
}
}
}
catch (Exception ex)
{
string a = ex.ToString();
}
return s;
}
public string Export_Excel97()
{
string s = "";
s = "<table border=\"1\">";
s += Worksheet97_Header();
s += Worksheet97_Data();
s += "</table>";
return s;
}
Thanks.
IMHO, i think you should page the query so that you don't load everything into memory.
To write the excel file you may want to try this solution too and compare results of performance: http://msmvps.com/blogs/deborahk/archive/2009/07/23/writing-data-from-a-datatable-to-excel.aspx which will use Microsoft Excel Object Library, so you'll need to have Excel installed in the machine where you're running your code.
HTH somehow.
Regards!
This might help... http://www.dotnetjohn.com/PrintFriend.aspx?articleid=36
It is in VB.NET but you should be able to convert it anyway ;-)