-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReports.cs
346 lines (301 loc) · 15.2 KB
/
Reports.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Reflection;
using Microsoft.Office.Interop.Excel;
using Application = Microsoft.Office.Interop.Excel.Application;
using Word = Microsoft.Office.Interop.Word;
using System.Reflection;
namespace Kursov
{
public partial class Reports : Form
{
public Reports()
{
InitializeComponent();
}
public SqlConnection sqlcon = new SqlConnection(@"Data Source=LAPTOP-8RIM0556\SQLEXPRESS;Initial Catalog=Tennis;Integrated Security=True");
private void LblChoose_Click(object sender, EventArgs e)
{
}
public int variety = 0;
private void BtnReportByShot_Click(object sender, EventArgs e)
{
txtFrom.Visible = true;
txtTo.Visible = true;
lblFrom.Visible = true;
lblTo.Visible = true;
lblFiltr.Visible = true;
variety = 1;
dataGridView1.ColumnCount = 2;
dataGridView1.RowCount = 1;
dataGridView1.Columns.Clear();
dataGridView1.Rows.Clear();
btnGetData.Visible = true;
chartTennisPlayers.Visible = false;
}
private void BtnGetData_Click(object sender, EventArgs e)
{
switch (variety)
{
case 1:
if (Convert.ToInt32(txtFrom.Text)<= Convert.ToInt32(txtTo.Text))
{
btnExport.Text = "Export to Excel";
dataGridView1.Visible = true;
sqlcon.Open();
//добавление необходимых колонок
DataGridViewTextBoxColumn dgvSpeed = new DataGridViewTextBoxColumn();
//установка свойств
dgvSpeed.Name = "Speed";
dgvSpeed.HeaderText = "Speed";
//добавили колонку
dataGridView1.Columns.Add(dgvSpeed);
DataGridViewTextBoxColumn dgvName = new DataGridViewTextBoxColumn();
//установка свойств
dgvName.Name = "Name";
dgvName.HeaderText = "Tennis Player";
//добавили колонку
dataGridView1.Columns.Add(dgvName);
List<string[]> data = new List<string[]>();
string query = @"select Shot.Speed,TennisPlayers.Surname from Shot
inner join Match_Progress on Shot.ID_Shot = Match_Progress.Shot_id
inner join TennisPlayers on TennisPlayers.ID_TennisPlayers = Match_Progress.ID_Player
where Shot.Speed>='"+txtFrom.Text+ "'and Shot.Speed<='"+txtTo.Text+"'";
SqlCommand com = new SqlCommand(query, sqlcon);
SqlDataReader reader = com.ExecuteReader();
while (reader.Read())
{
data.Add(new string[2]);
data[data.Count - 1][0] = reader[0].ToString();
data[data.Count - 1][1] = reader[1].ToString();
}
reader.Close();
int j = 0;
foreach (string[] s in data)
{
dataGridView1.RowCount += 1;
dataGridView1[0, j].Value = s[0];
dataGridView1[1, j].Value = s[1];
j++;
}
sqlcon.Close();
}
else
{
MessageBox.Show("Filter is not correct, try again");
txtFrom.Text = "";
txtTo.Text = "";
}
break;
case 2:
if (dtpStart.Value <= dtpFinish.Value)
{
btnExport.Text = "Export to Word";
dataGridView1.Visible = true;
sqlcon.Open();
//добавление необходимых колонок
DataGridViewTextBoxColumn dgvTournament = new DataGridViewTextBoxColumn();
//установка свойств
dgvTournament.Name = "Tournament";
dgvTournament.HeaderText = "Tournament";
//добавили колонку
dataGridView1.Columns.Add(dgvTournament);
DataGridViewTextBoxColumn dgvCountry= new DataGridViewTextBoxColumn();
//установка свойств
dgvCountry.Name = "Country";
dgvCountry.HeaderText = "Country";
//добавили колонку
dataGridView1.Columns.Add(dgvCountry);
DataGridViewTextBoxColumn dgvDate = new DataGridViewTextBoxColumn();
//установка свойств
dgvDate.Name = "Date";
dgvDate.HeaderText = "Date";
//добавили колонку
dataGridView1.Columns.Add(dgvDate);
List<string[]> data = new List<string[]>();
string query = @"select Tournament.Name_Tournament,Country.Country_Name, Tournament.Date_Start,Tournament.Date_Finish from Tournament
inner join Country on Country.ID_Country = Tournament.Country_info";
/*"'and Tournament.Date_Finish between '" + dtpStart.Value.ToShortDateString() + "'and '" + dtpFinish.Value.ToShortDateString() + */
//"'";
SqlCommand com = new SqlCommand(query, sqlcon);
SqlDataReader reader = com.ExecuteReader();
while (reader.Read())
{
DateTime s = (DateTime) reader[2];
DateTime f = (DateTime)reader[3];
if (s >= dtpStart.Value && f <= dtpFinish.Value)
{
data.Add(new string[3]);
data[data.Count - 1][0] = reader[0].ToString();
data[data.Count - 1][1] = reader[1].ToString();
data[data.Count - 1][2] = reader[2].ToString() + " - " + reader[3].ToString();
}
}
reader.Close();
int j = 0;
foreach (string[] s in data)
{
dataGridView1.RowCount += 1;
dataGridView1[0, j].Value = s[0];
dataGridView1[1, j].Value = s[1];
dataGridView1[2, j].Value = s[2];
j++;
}
sqlcon.Close();
}
break;
}
}
private void BtnExport_Click(object sender, EventArgs e)
{
switch (variety)
{
case 1:
var app = new Application();
var wb = app.Workbooks.Add();
var ws = wb.Worksheets[1] as Worksheet;
ws.Cells[1, 1].Value = "Отчет по ударам со скорость от " + txtFrom.Text + " по " + txtTo.Text;
ws.Cells[3, 1].Value = "Speed";
ws.Cells[3, 2].Value = "Tenis Player";
for (int i = 0; i < dataGridView1.ColumnCount; i++)
{
for (int j = 0; j < dataGridView1.RowCount; j++)
{
if (dataGridView1[i, j].Value != null)
app.Cells[j + 4, i + 1] = (dataGridView1[i, j].Value).ToString();
}
app.Visible = true;
}
wb.SaveAs("Отчет по ударам со скорость от " + txtFrom.Text + " по " + txtTo.Text + ".xlsx");
app.Quit();
break;
case 2:
if (dataGridView1.Rows.Count != 0)
{
int RowCount = dataGridView1.Rows.Count;
int ColumnCount = dataGridView1.Columns.Count;
Object[,] DataArray = new object[RowCount + 1, ColumnCount + 1];
//add rows
int r = 0;
for (int c = 0; c <= ColumnCount - 1; c++)
{
for (r = 0; r <= RowCount - 1; r++)
{
DataArray[r, c] = dataGridView1.Rows[r].Cells[c].Value;
} //end row loop
} //end column loop
Word.Document oDoc = new Word.Document();
oDoc.Application.Visible = true;
//page orintation
oDoc.PageSetup.Orientation = Word.WdOrientation.wdOrientLandscape;
dynamic oRange = oDoc.Content.Application.Selection.Range;
string oTemp = "";
for (r = 0; r <= RowCount - 1; r++)
{
for (int c = 0; c <= ColumnCount - 1; c++)
{
oTemp = oTemp + DataArray[r, c] + "\t";
}
}
//table format
oRange.Text = oTemp;
object Separator = Word.WdTableFieldSeparator.wdSeparateByTabs;
object ApplyBorders = true;
object AutoFit = true;
object AutoFitBehavior = Word.WdAutoFitBehavior.wdAutoFitContent;
oRange.ConvertToTable(ref Separator, ref RowCount, ref ColumnCount,
Type.Missing, Type.Missing, ref ApplyBorders,
Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing,
Type.Missing, ref AutoFit, ref AutoFitBehavior, Type.Missing);
oRange.Select();
oDoc.Application.Selection.Tables[1].Select();
oDoc.Application.Selection.Tables[1].Rows.AllowBreakAcrossPages = 0;
oDoc.Application.Selection.Tables[1].Rows.Alignment = 0;
oDoc.Application.Selection.Tables[1].Rows[1].Select();
oDoc.Application.Selection.InsertRowsAbove(1);
oDoc.Application.Selection.Tables[1].Rows[1].Select();
//header row style
oDoc.Application.Selection.Tables[1].Rows[1].Range.Bold = 1;
oDoc.Application.Selection.Tables[1].Rows[1].Range.Font.Name = "Tahoma";
oDoc.Application.Selection.Tables[1].Rows[1].Range.Font.Size = 14;
//add header row manually
for (int c = 0; c <= ColumnCount - 1; c++)
{
oDoc.Application.Selection.Tables[1].Cell(1, c + 1).Range.Text = dataGridView1.Columns[c].HeaderText;
}
//table style
//oDoc.Application.Selection.Tables[1].set_Style("Grid Table 4 - Accent 5");
oDoc.Application.Selection.Tables[1].Rows[1].Select();
oDoc.Application.Selection.Cells.VerticalAlignment = Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;
//header text
foreach (Word.Section section in oDoc.Application.ActiveDocument.Sections)
{
Word.Range headerRange = section.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
headerRange.Fields.Add(headerRange, Word.WdFieldType.wdFieldPage);
headerRange.Text = "Отчет по турнирам с " + dtpStart.Value.ToShortDateString() + " по " + dtpFinish.Value.ToShortDateString();
headerRange.Font.Size = 16;
headerRange.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
}
//save the file
oDoc.SaveAs("Отчет по турнирам с " + dtpStart.Value.ToShortDateString() + " по " + dtpFinish.Value.ToShortDateString() + ".docx");
//oDoc.ClosePrintPreview();
}
break;
}
}
private void BtnChart_Click(object sender, EventArgs e)
{
chartTennisPlayers.Visible = true;
sqlcon.Open();
dataGridView1.Visible = false;
btnExport.Visible = false;
chartTennisPlayers.Titles.Add(@"Char rating of tennis players");
List<string[]> chardate = new List<string[]>();
string query = @"select Surname, Rating from TennisPlayers";
SqlCommand com = new SqlCommand(query, sqlcon);
SqlDataReader reader = com.ExecuteReader();
while (reader.Read())
{
chardate.Add(new string[2]);
chardate[chardate.Count - 1][0] = reader[0].ToString();
chardate[chardate.Count - 1][1] = reader[1].ToString();
}
reader.Close();
foreach (string[] s in chardate)
{
chartTennisPlayers.Series["Player"].IsValueShownAsLabel = true;
chartTennisPlayers.Series["Player"].Points.AddXY(s[0], s[1]);
}
sqlcon.Close();
}
private void BtnReportByTournament_Click(object sender, EventArgs e)
{
variety = 2;
lblFiltr.Visible = false;
dtpStart.Visible = true;
dtpFinish.Visible = true;
btnGetData.Visible = true;
lblFrom.Visible = true;
lblTo.Visible = true;
dataGridView1.Visible = true;
chartTennisPlayers.Visible = false;
dataGridView1.ColumnCount = 3;
dataGridView1.RowCount = 1;
dataGridView1.Columns.Clear();
dataGridView1.Rows.Clear();
btnExport.Text = "Export to Word";
}
private void ChartTennisPlayers_Click(object sender, EventArgs e)
{
}
}
}