-
Notifications
You must be signed in to change notification settings - Fork 0
/
Extract data - SQL to Excel.cs
497 lines (411 loc) · 24.4 KB
/
Extract data - SQL to Excel.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
#region Help: Introduction to the script task
/* The Script Task allows you to perform virtually any operation that can be accomplished in
* a .Net application within the context of an Integration Services control flow.
*
* Expand the other regions which have "Help" prefixes for examples of specific ways to use
* Integration Services features within this script task. */
#endregion
#region Namespaces
using System;
using System.IO.Compression;
using System.Data;
using System.Data.SqlClient;
using System.Data.OleDb;
using System.IO;
using SColor = System.Drawing; /// Changing font color
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
using System.Net.Mail;
using System.Net;
using Microsoft.CSharp;
using System.Collections; /// Adding Arraylists
using Excel = Microsoft.Office.Interop.Excel; /// Excel Applications
#endregion
namespace ExcelPOC
{
/// <summary>
/// ScriptMain is the entry point class of the script. Do not change the name, attributes,
/// or parent of this class.
/// </summary>
[Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute]
public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
{
private int i;
#region Help: Using Integration Services variables and parameters in a script
/* To use a variable in this script, first ensure that the variable has been added to
* either the list contained in the ReadOnlyVariables property or the list contained in
* the ReadWriteVariables property of this script task, according to whether or not your
* code needs to write to the variable. To add the variable, save this script, close this instance of
* Visual Studio, and update the ReadOnlyVariables and
* ReadWriteVariables properties in the Script Transformation Editor window.
* To use a parameter in this script, follow the same steps. Parameters are always read-only.
*
* Example of reading from a variable:
* DateTime startTime = (DateTime) Dts.Variables["System::StartTime"].Value;
*
* Example of writing to a variable:
* Dts.Variables["User::myStringVariable"].Value = "new value";
*
* Example of reading from a package parameter:
* int batchId = (int) Dts.Variables["$Package::batchId"].Value;
*
* Example of reading from a project parameter:
* int batchId = (int) Dts.Variables["$Project::batchId"].Value;
*
* Example of reading from a sensitive project parameter:
* int batchId = (int) Dts.Variables["$Project::batchId"].GetSensitiveValue();
* */
#endregion
#region Help: Firing Integration Services events from a script
/* This script task can fire events for logging purposes.
*
* Example of firing an error event:
* Dts.Events.FireError(18, "Process Values", "Bad value", "", 0);
*
* Example of firing an information event:
* Dts.Events.FireInformation(3, "Process Values", "Processing has started", "", 0, ref fireAgain)
*
* Example of firing a warning event:
* Dts.Events.FireWarning(14, "Process Values", "No values received for input", "", 0);
* */
#endregion
#region Help: Using Integration Services connection managers in a script
/* Some types of connection managers can be used in this script task. See the topic
* "Working with Connection Managers Programatically" for details.
*
* Example of using an ADO.Net connection manager:
* object rawConnection = Dts.Connections["Sales DB"].AcquireConnection(Dts.Transaction);
* SqlConnection myADONETConnection = (SqlConnection)rawConnection;
* //Use the connection in some code here, then release the connection
* Dts.Connections["Sales DB"].ReleaseConnection(rawConnection);
*
* Example of using a File connection manager
* object rawConnection = Dts.Connections["Prices.zip"].AcquireConnection(Dts.Transaction);
* string filePath = (string)rawConnection;
* //Use the connection in some code here, then release the connection
* Dts.Connections["Prices.zip"].ReleaseConnection(rawConnection);
* */
#endregion
/// <summary>
/// This method is called when this script task executes in the control flow.
/// Before returning from this method, set the value of Dts.TaskResult to indicate success or failure.
/// To open Help, press F1.
/// </summary>
///
public void Main()
{
string SMTPServer = "smtp.com";
string MailFrom = "[email protected]";
string MailTo = "[email protected]";
string MailCC = "[email protected]";
string Database = Dts.Variables["User::Database"].Value.ToString();
string Server = Dts.Variables["User::Server"].Value.ToString();
string SQLConnString = @"Data Source=" + Server + ";Initial Catalog=" + Database + ";Integrated Security=SSPI;"; // SQL Connection string
Excel.Application ExcelApp = new Excel.Application(); // Initialize Excel Application
ExcelApp.DisplayAlerts = false;
string TemplateFilewithPath = Dts.Variables["User::FILE_TemplateFolder"].Value.ToString() + Dts.Variables["User::FILE_FileName"].Value.ToString();
Excel.Workbook WB = ExcelApp.Workbooks.Open(TemplateFilewithPath); // Initialize Excel Workbook
Excel.Worksheets WS = ExcelApp.ActiveSheet as Excel.Worksheets; // Initialize Excel Worksheet
CallManagerData_To_Excel(ExcelApp, WB, MailFrom, MailTo, MailCC, SMTPServer); // Write CallManager data to Template
string FinalOutputFileName = CreateFileFormTemplate(); // Create a copy of from Template file to Output file
WB = ExcelApp.Workbooks.Open(FinalOutputFileName); // Initialize Excel Workbook with output file
WS = ExcelApp.ActiveSheet as Excel.Worksheets; // Initialize Excel Worksheet with output file
Notifications_To_Excel(ExcelApp, WB, SQLConnString, MailFrom, MailTo, MailCC, SMTPServer); // Export data from SQL Server to Excel
Occurences_To_Excel(ExcelApp, WB, SQLConnString, MailFrom, MailTo, MailCC, SMTPServer); // Export data from SQL Server to Excel
Settelments_To_Excel(ExcelApp, WB, SQLConnString, MailFrom, MailTo, MailCC, SMTPServer); // Export data from SQL Server to Excel
Outstandings_To_Excel(ExcelApp, WB, SQLConnString, MailFrom, MailTo, MailCC, SMTPServer); // Export data from SQL Server to Excel
WB.Save();
WB.Close(0);
ExcelApp.Quit();
GC.Collect();
Dts.TaskResult = (int)ScriptResults.Success;
}
private string CreateFileFormTemplate()
{
string SQLConnString = @"Data Source=MissDBServer01;Initial Catalog=Callmanager;Integrated Security=SSPI;";
string TemplateFolder = Dts.Variables["User::FILE_TemplateFolder"].Value.ToString();
string OutputFolder = Dts.Variables["User::FILE_OutputFolder"].Value.ToString();
string FileName = Dts.Variables["User::FILE_FileName"].Value.ToString();
string QueryWeekName = Dts.Variables["User::QueryWeekName"].Value.ToString();
string WeekName = "";
// Generating WeekNumber
SqlConnection SQLCON = new SqlConnection(SQLConnString);
SQLCON.Open();
SqlCommand SQLCommand = new SqlCommand(QueryWeekName, SQLCON);
WeekName = (string)SQLCommand.ExecuteScalar();
SQLCON.Close();
Dts.Variables["User::FILE_OutputFolderwithFileName"].Value = OutputFolder + FileName + "-" + WeekName + ".xls";
Dts.Variables["User::FILE_ZipFileNamewithPath"].Value = OutputFolder + FileName + "-" + WeekName + ".zip";
Dts.Variables["User::FILE_ZipFileName"].Value = FileName + "-" + WeekName + ".zip";
Dts.Variables["User::WeekNumber"].Value = (Convert.ToInt16(WeekName.Substring(WeekName.Length - 2, 2)));
//MessageBox.Show(Dts.Variables["User::WeekNumber"].Value.ToString());
string FinalOutputFileNamewithPath = OutputFolder + FileName + "-" + WeekName + ".xls";
string TemplateFile = System.IO.Path.Combine(TemplateFolder, FileName + ".xls");
if (!System.IO.Directory.Exists(OutputFolder))
{
System.IO.Directory.CreateDirectory(OutputFolder);
}
System.IO.File.Copy(TemplateFile, FinalOutputFileNamewithPath, true);
return FinalOutputFileNamewithPath;
}
private void Notifications_To_Excel(Excel.Application ExcelApp, Excel.Workbook WB, string SQLConnString, string MailFrom, string MailTo, string MailCC, string SMTPServer)
{
try
{
string query = "SELECT [Cover Level 1 Name],[Claim Type Name],[Product Name],[Cause Code Name]" +
",[Week_Notified],[CountOfCustomer Claim Number],[SumOfSumOfTotal_Paid],[SumOfSumOfTotal_Estimate]" +
",[SumOfSumOfTotal_Incurred],[SumOfSumOfAD_Paid],[SumOfSumOfAD_Estimate],[SumOfSumOfTP_Paid]" +
",[SumOfSumOfTP_Estimate],[SumOfSumOfBI_Paid],[SumOfSumOfBI_Estimate],[SumOfSumOfRec_Paid]" +
",[SumOfSumOfRec_Estimate],[Responsibilty Percentage] FROM [AUT].[tbl_FACT_Notifications]";
using (SqlConnection connection = new SqlConnection(SQLConnString))
{
SqlCommand command = new SqlCommand(query, connection);
connection.Open();
SqlDataAdapter da = new SqlDataAdapter(command);
DataTable dt = new DataTable();
da.Fill(dt);
Excel.Worksheet WS = ((Excel.Worksheet)WB.Worksheets.get_Item("Notifications_Dataset"));
int firstRow = 2; int firstCol = 1; int lastRow = 0; int lastCol = 0;
lastRow = dt.Rows.Count;
lastCol = dt.Columns.Count;
Excel.Range top = WS.Cells[firstRow, firstCol];
Excel.Range bottom = WS.Cells[lastRow, lastCol];
Excel.Range all = (Excel.Range)WS.get_Range(top, bottom);
object[,] arrayDT = new object[dt.Rows.Count, dt.Columns.Count];
for (int i = 0; i < dt.Rows.Count; i++)
for (int j = 0; j < dt.Columns.Count; j++)
{
arrayDT[i, j] = dt.Rows[i][j];
}
all.Value2 = arrayDT;
WS.Columns.AutoFit();
}
}
catch (Exception ex)
{
string MailBody, MailSub = "Team - Weekly - Notifications data from SQL to Excel failed";
MailBody = "Hello Team </BR></BR>";
MailBody += string.Format("Data load for Notifications from SQL to Excel failed due to:</BR><b>Error message:</b>{0}", ex.Message);
SendMail(MailFrom, MailTo, MailCC, MailSub, SMTPServer, MailBody);
WB.Save();
WB.Close(0);
ExcelApp.Quit();
GC.Collect();
Dts.TaskResult = (int)ScriptResults.Failure;
}
}
private void Occurences_To_Excel(Excel.Application ExcelApp, Excel.Workbook WB, string SQLConnString, string MailFrom, string MailTo, string MailCC, string SMTPServer)
{
try
{
string query = "SELECT [Cover Level 1 Name],[Claim Type Name],[Product Name],[Cause Code Name]"+
",[Week_Occurred],[CountOfCustomer Claim Number],[SumOfSumOfTotal_Paid]"+
",[SumOfSumOfTotal_Estimate],[SumOfSumOfTotal_Incurred],[SumOfSumOfAD_Paid]"+
",[SumOfSumOfAD_Estimate],[SumOfSumOfTP_Paid],[SumOfSumOfTP_Estimate]"+
",[SumOfSumOfBI_Paid],[SumOfSumOfBI_Estimate],[SumOfSumOfRec_Paid]"+
",[SumOfSumOfRec_Estimate],[Responsibilty Percentage] FROM [AUT].[tbl_Fact_Occurences]";
using (SqlConnection connection = new SqlConnection(SQLConnString))
{
SqlCommand command = new SqlCommand(query, connection);
connection.Open();
SqlDataAdapter da = new SqlDataAdapter(command);
DataTable dt = new DataTable();
da.Fill(dt);
Excel.Worksheet WS = ((Excel.Worksheet)WB.Worksheets.get_Item("Occurrences_Dataset"));
int firstRow = 2; int firstCol = 1; int lastRow = 0; int lastCol = 0;
lastRow = dt.Rows.Count;
lastCol = dt.Columns.Count;
Excel.Range top = WS.Cells[firstRow, firstCol];
Excel.Range bottom = WS.Cells[lastRow, lastCol];
Excel.Range all = (Excel.Range)WS.get_Range(top, bottom);
object[,] arrayDT = new object[dt.Rows.Count, dt.Columns.Count];
for (int i = 0; i < dt.Rows.Count; i++)
for (int j = 0; j < dt.Columns.Count; j++)
{
arrayDT[i, j] = dt.Rows[i][j];
}
all.Value2 = arrayDT;
WS.Columns.AutoFit();
}
}
catch (Exception ex)
{
string MailBody, MailSub = "Team - Weekly - Occurences data from SQL to Excel failed";
MailBody = "Hello Team </BR></BR>";
MailBody += string.Format("Data load for Occurences from SQL to Excel failed due to:</BR><b>Error message:</b>{0}", ex.Message);
SendMail(MailFrom, MailTo, MailCC, MailSub, SMTPServer, MailBody);
WB.Save();
WB.Close(0);
ExcelApp.Quit();
GC.Collect();
Dts.TaskResult = (int)ScriptResults.Failure;
}
}
private void Settelments_To_Excel(Excel.Application ExcelApp, Excel.Workbook WB, string SQLConnString, string MailFrom, string MailTo, string MailCC, string SMTPServer)
{
try
{
string query = "SELECT [Cover Level 1 Name],[Claim Type Name],[Product Name],[Cause Code Name]"+
",[Week_Settled],[Nil_Claim],[Outcome_Type],[Claim Indicator Name]"+
",[Lifecycle],[CountOfCustomer Claim Number],[SumOfSumOfTotal_Paid]"+
",[SumOfSumOfAD_Paid],[SumOfSumOfTP_Paid],[SumOfSumOfBI_Paid]"+
",[SumOfSumOfRec_Paid],[Responsibilty Percentage] FROM [MI_SS_Collections].[AUT].[tbl_Fact_Settled]";
using (SqlConnection connection = new SqlConnection(SQLConnString))
{
SqlCommand command = new SqlCommand(query, connection);
connection.Open();
SqlDataAdapter da = new SqlDataAdapter(command);
DataTable dt = new DataTable();
da.Fill(dt);
Excel.Worksheet WS = ((Excel.Worksheet)WB.Worksheets.get_Item("Settlements_Dataset"));
int firstRow = 2; int firstCol = 1; int lastRow = 0; int lastCol = 0;
lastRow = dt.Rows.Count;
lastCol = dt.Columns.Count;
Excel.Range top = WS.Cells[firstRow, firstCol];
Excel.Range bottom = WS.Cells[lastRow, lastCol];
Excel.Range all = (Excel.Range)WS.get_Range(top, bottom);
object[,] arrayDT = new object[dt.Rows.Count, dt.Columns.Count];
for (int i = 0; i < dt.Rows.Count; i++)
for (int j = 0; j < dt.Columns.Count; j++)
{
arrayDT[i, j] = dt.Rows[i][j];
}
all.Value2 = arrayDT;
WS.Columns.AutoFit();
}
}
catch (Exception ex)
{
string MailBody, MailSub = "Team - Weekly - Settelments data from SQL to Excel failed";
MailBody = "Hello Team </BR></BR>";
MailBody += string.Format("Data load for Settelments from SQL to Excel failed due to:</BR><b>Error message:</b>{0}", ex.Message);
SendMail(MailFrom, MailTo, MailCC, MailSub, SMTPServer, MailBody);
WB.Save();
WB.Close(0);
ExcelApp.Quit();
GC.Collect();
Dts.TaskResult = (int)ScriptResults.Failure;
}
}
private void Outstandings_To_Excel(Excel.Application ExcelApp, Excel.Workbook WB, string SQLConnString, string MailFrom, string MailTo, string MailCC, string SMTPServer)
{
try
{
string query = "SELECT [Cover Level 1 Name],[Claim Type Name],[Cause Code Name],[Product Name]"+
",[Age_Banding],[Lifecycle],[CountOfCustomer Claim Number],[SumOfSumOfTotal_Paid]"+
",[SumOfSumOfTotal_Estimate],[SumOfSumOfTotal_Incurred],[SumOfSumOfAD_Paid]"+
",[SumOfSumOfAD_Estimate],[SumOfSumOfTP_Paid],[SumOfSumOfTP_Estimate]"+
",[SumOfSumOfBI_Paid],[SumOfSumOfBI_Estimate],[SumOfSumOfRec_Paid],[SumOfSumOfRec_Estimate]"+
",[Responsibilty Percentage] FROM [MI_SS_Collections].[AUT].[tbl_Fact_Outstanding]";
using (SqlConnection connection = new SqlConnection(SQLConnString))
{
SqlCommand command = new SqlCommand(query, connection);
connection.Open();
SqlDataAdapter da = new SqlDataAdapter(command);
DataTable dt = new DataTable();
da.Fill(dt);
Excel.Worksheet WS = ((Excel.Worksheet)WB.Worksheets.get_Item("Outstanding_Dataset"));
int firstRow = 2; int firstCol = 1; int lastRow = 0; int lastCol = 0;
lastRow = dt.Rows.Count;
lastCol = dt.Columns.Count;
Excel.Range top = WS.Cells[firstRow, firstCol];
Excel.Range bottom = WS.Cells[lastRow, lastCol];
Excel.Range all = (Excel.Range)WS.get_Range(top, bottom);
object[,] arrayDT = new object[dt.Rows.Count, dt.Columns.Count];
for (int i = 0; i < dt.Rows.Count; i++)
for (int j = 0; j < dt.Columns.Count; j++)
{
arrayDT[i, j] = dt.Rows[i][j];
}
all.Value2 = arrayDT;
WS.Columns.AutoFit();
}
}
catch (Exception ex)
{
string MailBody, MailSub = "Team - Weekly - Outstanding data from SQL to Excel failed";
MailBody = "Hello Team </BR></BR>";
MailBody += string.Format("Data load for Outstanding from SQL to Excel failed due to:</BR><b>Error message:</b>{0}", ex.Message);
SendMail(MailFrom, MailTo, MailCC, MailSub, SMTPServer, MailBody);
WB.Save();
WB.Close(0);
ExcelApp.Quit();
GC.Collect();
Dts.TaskResult = (int)ScriptResults.Failure;
}
}
private void CallManagerData_To_Excel(Excel.Application ExcelApp, Excel.Workbook WB, string MailFrom, string MailTo, string MailCC, string SMTPServer)
{
try
{
string SQLConnString = @"Data Source=Testserver;Initial Catalog=testdb;Integrated Security=SSPI;";
string query = Dts.Variables["User::SQLQuery_Tesco"].Value.ToString();
using (SqlConnection connection = new SqlConnection(SQLConnString))
{
SqlCommand command = new SqlCommand(query, connection);
connection.Open();
SqlDataAdapter da = new SqlDataAdapter(command);
DataTable dt = new DataTable();
da.Fill(dt);
Excel.Worksheet WS = ((Excel.Worksheet)WB.Worksheets.get_Item("Data"));
WS.Columns.ClearFormats(); // Clear cell format
WS.Rows.ClearFormats(); // Clear cell format
int firstRow = 0; int firstCol = 1; int lastRow = 0; int lastCol = 0;
firstRow = WS.UsedRange.Rows.Count + 1;
lastRow = WS.UsedRange.Rows.Count + dt.Rows.Count;
lastCol = dt.Columns.Count;
Excel.Range top = WS.Cells[firstRow, firstCol];
Excel.Range bottom = WS.Cells[lastRow, lastCol];
Excel.Range all = (Excel.Range)WS.get_Range(top, bottom);
object[,] arrayDT = new object[dt.Rows.Count, dt.Columns.Count];
for (int i = 0; i < dt.Rows.Count; i++)
for (int j = 0; j < dt.Columns.Count; j++)
{
arrayDT[i, j] = dt.Rows[i][j];
}
all.Value2 = arrayDT;
WS.Columns.AutoFit();
WB.Save();
}
}
catch (Exception ex)
{
string MailBody, MailSub = "Team - Weekly - data to Excel failed";
MailBody = "Hello Team </BR></BR>";
MailBody += string.Format("Data load from data to Excel failed due to:</BR><b>Error message:</b>{0}", ex.Message);
SendMail(MailFrom, MailTo, MailCC, MailSub, SMTPServer, MailBody);
WB.Save();
WB.Close(0);
ExcelApp.Quit();
GC.Collect();
Dts.TaskResult = (int)ScriptResults.Failure;
}
}
private void SendMail(string MailFrom, string MailTo, string MailCC, string MailSub, string SMTPServer, string MailBody)
{
MailMessage htmlMessage = new MailMessage();
SmtpClient mySmtpClient = new SmtpClient(SMTPServer);
MailBody += "</BR></BR>";
MailBody += "Thank you,</BR>";
MailBody += "Your Team";
htmlMessage.Body = MailBody.ToString();
htmlMessage = new MailMessage(MailFrom, MailTo, MailSub, htmlMessage.Body.ToString());
htmlMessage.IsBodyHtml = true;
mySmtpClient.Credentials = CredentialCache.DefaultNetworkCredentials;
mySmtpClient.Send(htmlMessage);
}
#region ScriptResults declaration
/// <summary>
/// This enum provides a convenient shorthand within the scope of this class for setting the
/// result of the script.
///
/// This code was generated automatically.
/// </summary>
enum ScriptResults
{
Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
};
#endregion
}
}