public void SendAutomatedEmail(string yyyyMMdd)
{
string oSavePath = workDir + @"\your.file.name";
string ReceiveMail = "target@abc.com.tw";
string smtpHost = "ms.abc.com.tw";
string mailSubject = "IThelp";
string mailContext = stringHtml;
MailMessage MyMail = new System.Net.Mail.MailMessage();
MyMail.From = new System.Net.Mail.MailAddress("wkh@cht.com.tw");
MyMail.To.Add(ReceiveMail); //設定收件者Email
//MyMail.Bcc.Add("密件副本的收件者Mail"); //加入密件副本的Mail
MyMail.Subject = mailSubject + "(" + yyyyMMdd + ")";
MyMail.Body = mailContext ; //設定信件內容
MyMail.IsBodyHtml = true; //是否使用html格式
Attachment data = new Attachment(oSavePath, MediaTypeNames.Application.Octet);
MyMail.Attachments.Add(data);
System.Net.Mail.SmtpClient MySMTP = new System.Net.Mail.SmtpClient(smtpHost);
//MySMTP.Credentials = new System.Net.NetworkCredential("account", "PW");
try
{
MySMTP.Send(MyMail);
MyMail.Dispose(); //釋放資源
}
catch (Exception ex)
{
ex.ToString();
}
}
2019/7/2
如何以C#將DataTable轉成html?
private string ExportHtml(DataTable dt)
{
StringBuilder strHTMLBuilder = new StringBuilder();
strHTMLBuilder.Append("");
strHTMLBuilder.Append("");
strHTMLBuilder.Append("
"); strHTMLBuilder.Append("");
strHTMLBuilder.Append("
");
strHTMLBuilder.Append(" ");
foreach (DataColumn myColumn in dt.Columns)
{
strHTMLBuilder.Append("
");
strHTMLBuilder.Append(myColumn.ColumnName);
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();
return Htmltext;
}
由string反序列化Json物件的方法
1. 透過http://json2csharp.com/ 直接貼上json語法,會產生對應的C#物件。
2. using Newtonsoft.Json;
3. 以 JsonConvert.DeserializeObject 將 string反序列化Json物件成為C#內的物件
在整理成DataTable 後,轉成html
private void parseJson(string json)
{
//ReadFromFile();
RootObject rootObject = JsonConvert.DeserializeObject(json);
int days = rootObject.days;
//MessageBox.Show("共" + days + "筆資料");
DataTable newDt = new DataTable();
newDt.Columns.Add("column1", typeof(string));
newDt.Columns.Add("column2", typeof(string));
newDt.Columns.Add("column3", typeof(string));
newDt.Columns.Add("column4", typeof(string));
newDt.Columns.Add("column5", typeof(string));
int i = 0;
while (++i <= days)
{
newDt.Columns.Add(i.ToString(), typeof(string));
}
//rootObject.aaData.Sort(CompareFloat);
foreach (DataRow row in rootObject.aaData.Rows)
{
//MessageBox.Show(row["id"] + " - " + row["item"]);
if ( !row["score_0"].Equals("100.0000"))
{
DataRow workRow;
workRow = newDt.NewRow();
workRow[0] = row["eDeskId"];
workRow[1] = row["systemName"];
workRow[2] = "";
workRow[3] = row["keySystem"];
for (i = 0; i <= days; i++)
{
string score = row["score_" + i].ToString();
if( score.Equals("100.0000") )
{
workRow[i + 4] = "100";;
}
else
{
redCount++;
workRow[i + 3] = "" + row["score_" + i] + "";
}
}
newDt.Rows.Add(workRow);
}
}
stringHtml = ExportHtml(newDt);
textBox2.Text = stringHtml;
htmlFileName = "qos_" + DateTime.Now.Month + "_" + DateTime.Now.Day + ".html";
StreamWriter sw = new StreamWriter(workDir + @"\" + htmlFileName, true, Encoding.Default);
sw.Write(stringHtml);
sw.Close();
StreamWriter sw2 = new StreamWriter(workDir + @"\redCount.txt", true, Encoding.Default);
sw2.Write(Convert.ToString(redCount));
sw2.Close();
}
2. using Newtonsoft.Json;
3. 以 JsonConvert.DeserializeObject 將 string反序列化Json物件成為C#內的物件
在整理成DataTable 後,轉成html
private void parseJson(string json)
{
//ReadFromFile();
RootObject rootObject = JsonConvert.DeserializeObject
int days = rootObject.days;
//MessageBox.Show("共" + days + "筆資料");
DataTable newDt = new DataTable();
newDt.Columns.Add("column1", typeof(string));
newDt.Columns.Add("column2", typeof(string));
newDt.Columns.Add("column3", typeof(string));
newDt.Columns.Add("column4", typeof(string));
newDt.Columns.Add("column5", typeof(string));
int i = 0;
while (++i <= days)
{
newDt.Columns.Add(i.ToString(), typeof(string));
}
//rootObject.aaData.Sort(CompareFloat);
foreach (DataRow row in rootObject.aaData.Rows)
{
//MessageBox.Show(row["id"] + " - " + row["item"]);
if ( !row["score_0"].Equals("100.0000"))
{
DataRow workRow;
workRow = newDt.NewRow();
workRow[0] = row["eDeskId"];
workRow[1] = row["systemName"];
workRow[2] = "";
workRow[3] = row["keySystem"];
for (i = 0; i <= days; i++)
{
string score = row["score_" + i].ToString();
if( score.Equals("100.0000") )
{
workRow[i + 4] = "100";;
}
else
{
redCount++;
workRow[i + 3] = "" + row["score_" + i] + "";
}
}
newDt.Rows.Add(workRow);
}
}
stringHtml = ExportHtml(newDt);
textBox2.Text = stringHtml;
htmlFileName = "qos_" + DateTime.Now.Month + "_" + DateTime.Now.Day + ".html";
StreamWriter sw = new StreamWriter(workDir + @"\" + htmlFileName, true, Encoding.Default);
sw.Write(stringHtml);
sw.Close();
StreamWriter sw2 = new StreamWriter(workDir + @"\redCount.txt", true, Encoding.Default);
sw2.Write(Convert.ToString(redCount));
sw2.Close();
}
C#.net 用webbrowser類別自動抓取網頁程式
用webbrowser類別自動抓取網頁程式
protected void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "" || textBox1.Text == "https://")
{
textBox1.Text = "https://qos.cht.com.tw";
}
webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
Navigate(textBox1.Text);
/// 確認網頁是否讀取完畢的方法
private bool checkComplete(string keyword)
{
bool b = false;
while (!b)
{
if (webBrowser1.Document != null)
{
foreach (HtmlElement he in webBrowser1.Document.All)
{
//if(he.Id.Contains(keyword) )
if (he.Id == keyword)
//if (he.OuterText == keyword)
{
b = true;
}
}
}
Application.DoEvents();
}
return b;
}
private bool checkComplete2(string keyword)
{
bool b = false;
while (!b)
{
if (webBrowser1.Document != null)
{
foreach (HtmlElement he in webBrowser1.Document.All)
{
//MessageBox.Show("]" + he.OuterHtml + "[");
if(he.OuterHtml.Contains(keyword) )
//if (he.OuterHtml == keyword)
//if (he.OuterText == keyword)
{
b = true;
}
}
}
Application.DoEvents();
}
return b;
}
如何在C#.net中加入元件
/// 在類別內加入一個data member,並馬上new物件,呼叫建構子。
partial class Form1 : Form
{
private Button button1 = new Button();
///建構子中設定物件屬性
public Form1()
{
button1.Location = new Point(10, 10);
button1.TabIndex = 0;
button1.Text = "Navigate Qos";
button1.AutoSize = true;
button1.Click += new EventHandler(button1_Click);
///最後加入Control中
this.Controls.Add(button1);
訂閱:
文章 (Atom)