-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
158 lines (135 loc) · 5.41 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
using System;
using System.Data;
using System.Data.OleDb;
using System.Configuration;
using System.IO;
using System.Xml.Linq;
using System.Xml;
using System.Xml.Xsl;
namespace MySchedule
{
class Program
{
static void Main(string[] args)
{
string conString = ConfigurationManager.ConnectionStrings["constring"].ConnectionString;
OleDbConnection con = new OleDbConnection(conString);
OleDbCommand cmd = new OleDbCommand();
OleDbDataAdapter adapter = new OleDbDataAdapter();
DataTable dt = new DataTable();
string select = "select * from [Sheet1$]";
cmd.Connection = con;
cmd.CommandText = select;
adapter.InsertCommand = cmd;
adapter.SelectCommand = cmd;
con.Open();
adapter.Fill(dt);
con.Close();
DataTable scheduledt = new DataTable();
scheduledt.TableName = "Table";
DateTime today = DateTime.Now.Date;
DateTime reference = new DateTime(2014, 09, 08);
DayOfWeek dowToday = today.DayOfWeek;
int diff = dowToday - DayOfWeek.Monday;
if (diff < 0)
{
diff += 7;
}
DateTime monday;
if (dowToday == DayOfWeek.Sunday)
{
monday = today.AddDays(1).Date;
}
else
{
monday = today.AddDays(-1 * diff).Date;
}
int scheduleNum = 1;
if (Convert.ToInt32((monday - reference).Days) % 2 == 0)
{
scheduleNum = 1;
}
else
{
scheduleNum = 2;
}
int dtRowCount = 0;
bool newLine = false;
if (dowToday == DayOfWeek.Sunday)
{
dowToday++;
scheduledt.Columns.Add("Day");
scheduledt.Columns.Add("Time");
scheduledt.Columns.Add("Schedule");
for (int i = 0; i < dt.Rows.Count; i++)
{
if (Convert.ToInt32(dt.Rows[i][5].ToString()) == scheduleNum)
{
if (dtRowCount > 0)
{
if (scheduledt.Rows[dtRowCount - 1][0].ToString() != dt.Rows[i][4].ToString() && newLine == false)
{
scheduledt.Rows.Add();
scheduledt.Rows[dtRowCount][0] = "---";
scheduledt.Rows[dtRowCount][1] = "---";
scheduledt.Rows[dtRowCount][2] = "---";
i--;
newLine = true;
}
else
{
scheduledt.Rows.Add();
scheduledt.Rows[dtRowCount][0] = dt.Rows[i][4];
scheduledt.Rows[dtRowCount][1] = dt.Rows[i][2];
scheduledt.Rows[dtRowCount][2] = dt.Rows[i][0] + " " + dt.Rows[i][1] + " " + dt.Rows[i][3] + " ";
newLine = false;
}
}
else if (dtRowCount == 0)
{
scheduledt.Rows.Add();
scheduledt.Rows[dtRowCount][0] = dt.Rows[i][4];
scheduledt.Rows[dtRowCount][1] = dt.Rows[i][2];
scheduledt.Rows[dtRowCount][2] = dt.Rows[i][0] + " " + dt.Rows[i][1] + " " + dt.Rows[i][3] + " ";
newLine = false;
}
dtRowCount++;
}
}
}
else
{
scheduledt.Columns.Add("Time");
scheduledt.Columns.Add("Schedule");
for (int i = 0; i < dt.Rows.Count; i++)
{
if (dt.Rows[i][4].ToString() == dowToday.ToString() && Convert.ToInt32(dt.Rows[i][5].ToString()) == scheduleNum)
{
scheduledt.Rows.Add();
scheduledt.Rows[dtRowCount][0] = dt.Rows[i][2];
scheduledt.Rows[dtRowCount][1] = dt.Rows[i][0] + " " + dt.Rows[i][1] + " " + dt.Rows[i][3] + " ";
dtRowCount++;
}
}
}
string html = getHTML(scheduledt);
Mail m1 = new Mail(html);
}
//construct HTML code
static string getHTML(System.Data.DataTable dt)
{
string xsltFilePath = @"C:\Users\Kelly\Documents\schedule\MySchedule\MySchedule\bin\Debug\XSLTFile1.xslt";
TextWriter txtwriter = new StringWriter();
dt.WriteXml(txtwriter);
XDocument xmlDoc = XDocument.Parse(txtwriter.ToString());
XDocument htmlDoc = new XDocument();
using (XmlWriter writer = htmlDoc.CreateWriter())
{
XslCompiledTransform xslt = new XslCompiledTransform();
xslt.Load(xsltFilePath);
xslt.Transform(xmlDoc.CreateReader(), writer);
}
return htmlDoc.Document.ToString();
}
}
}