-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
163 lines (135 loc) · 5.67 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
159
160
161
162
163
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using Excel = Microsoft.Office.Interop.Excel;
namespace DatabaseToExcel
{
class Program
{
private static readonly AppArgs parsedArgs = new AppArgs();
static void Main(string[] args)
{
if (Parser.ParseArgumentsWithUsage(args, parsedArgs))
{
try
{
Log("Building Connection String");
string connString = GetConnectionString(parsedArgs);
Log("Fetching Data");
DataSet ds = GetData(parsedArgs.queryFile, connString);
Log("Creating Excel File");
CreateExcelFile(parsedArgs, ds);
}
catch (Exception ex)
{
Log("Error occured: " + ex.Message);
Environment.Exit(1);
}
}
else
{
Log("Invalid arguments");
Environment.Exit(1);
}
}
private static void Log(string msg)
{
Console.WriteLine(msg);
Trace.WriteLine(msg);
Debug.WriteLine(msg);
}
private static void CreateExcelFile(AppArgs appArgs, DataSet ds)
{
Excel.Application app = null;
Excel.Workbook workbook = null;
Excel.Worksheet worksheet = null;
var sheetNames = new List<string>();
if (!string.IsNullOrWhiteSpace(appArgs.sheetFile) && File.Exists(appArgs.sheetFile))
sheetNames = File.ReadAllLines(appArgs.sheetFile).ToList();
try
{
app = new Excel.Application {Visible = false};
workbook = app.Workbooks.Add(1);
// = (Excel.Worksheet)workbook.Sheets[1];
for (int i = 0; i < ds.Tables.Count - 1; i++)
workbook.Sheets.Add(Missing.Value, workbook.Sheets[i + 1]);
// now name them
for (int i = 0; i < sheetNames.Count; i++)
{
if (workbook.Sheets.Count < i) break;
worksheet = (Excel.Worksheet)workbook.Sheets[i + 1];
worksheet.Name = sheetNames[i];
}
// now populate the spreadsheet
for (int i = 0; i < ds.Tables.Count; i++)
{
//var columnNames = new List<string>();
DataTable dt = ds.Tables[i];
worksheet = (Excel.Worksheet)workbook.Sheets[i + 1];
List<string> columnNames = dt.Columns.Cast<DataColumn>().Select(cln => cln.ColumnName).ToList();
//foreach (DataColumn item in dt.Columns)
// columnNames.Add(item.ColumnName);
Utilities.RenderDataTableOnXlSheet(dt, worksheet, columnNames.ToArray(), columnNames.ToArray());
}
// select the 1st worksheet
app.ActiveWorkbook.Sheets[1].Activate();
// fix up the output file. If the path is not absolute, then Excel will save it in the Documents folder
// we want to save it in the current directory
if (!appArgs.outputFile.Contains(@"\"))
appArgs.outputFile = Path.Combine(Environment.CurrentDirectory, appArgs.outputFile);
// delete output file if exists
if (File.Exists(appArgs.outputFile)) File.Delete(appArgs.outputFile);
workbook.SaveAs(appArgs.outputFile);
app.Quit();
if (appArgs.launchAfterCreation)
Process.Start(appArgs.outputFile);
}
catch (Exception e)
{
Console.Write("Error: " + e.Message);
}
finally
{
Utilities.ReleaseComObject(worksheet);
Utilities.ReleaseComObject(workbook);
Utilities.ReleaseComObject(app);
}
}
private static string GetConnectionString(AppArgs args)
{
return args.integratedSecurity ? string.Format("data source={0};initial catalog={1};Integrated Security=SSPI",
args.server, args.database)
: string.Format("data source={0};initial catalog={1};user id={2};password={3}",
args.server, args.database, args.user, args.password);
}
private static DataSet GetData(string queryFile, string connString)
{
string queryContents = File.ReadAllText(queryFile);
return ExecuteDataSet(connString, queryContents);
}
protected static DataSet ExecuteDataSet(string connectionString, string commandText)
{
using (var conn = new SqlConnection(connectionString))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = commandText;
cmd.CommandType = CommandType.Text;
cmd.CommandTimeout = 120;
using (var da = new SqlDataAdapter(cmd))
{
var ds = new DataSet();
da.Fill(ds);
return ds;
}
}
}
}
}
}