-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
157 lines (138 loc) · 5.83 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
using Microsoft.Extensions.Configuration;
using ranxlib;
using System;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
namespace ranx
{
class Program
{
static void Main(string[] args)
{
//var env = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
var builder = new ConfigurationBuilder()
.AddJsonFile($"appsettings.json", true, true)
//.AddJsonFile($"appsettings.{env}.json", true, true)
.AddEnvironmentVariables();
var config = builder.Build();
BuildMenu();
//TestBuildDataForPivot(config);
//TestAddRowTotal(config);
//TestBuildPivot(config);
RunAll(config);
Console.ReadLine();
}
public static void BuildMenu()
{
}
public static void RunAll(IConfigurationRoot config)
{
TestAddRowTotal(config);
//TestBuildPivot(config);
TestPivotAtOnce(config);
}
public static void TestAddRowTotal(IConfigurationRoot config)
{
Console.WriteLine("Test addRowTotal");
string sql = @"SELECT TOP 10
ProductName
,sum(quantity) as Bottles_sold
,sum(ExtendedPrice) as Subtotal_per_Wine
FROM [dbo].[Order Details Extended]
group by ProductName ORDER BY sum(ExtendedPrice) DESC";
var results = GetDataTable(config, sql, null);
var file = AppDomain.CurrentDomain.BaseDirectory + "/output/rowtotal.xlsx";
if (System.IO.File.Exists(file))
{
System.IO.File.Delete(file);
}
ExcelHelper eh = ExcelHelper.Create(file)
.Dump(results, "SHEET1")
//.DoPivotTable("SHEET1","SHEET2", new string[]{"ProductName"}, new string[]{"Bottles_sold"}, new string[]{"Subtotal_per_Wine" })
.AddRowTotal("SHEET1")
.Save();
eh.Dispose();
Console.WriteLine("file created in " + file);
Console.WriteLine("End testAddRowTotal");
}
public static void TestBuildDataForPivot(IConfigurationRoot config)
{
string sql = "SELECT * FROM Invoices ORDER BY OrderDate";
var results = GetDataTable(config, sql, null);
var file = AppDomain.CurrentDomain.BaseDirectory + "/output/pivot.xlsx";
ExcelHelper eh = ExcelHelper.Create(file)
.Dump(results, "SHEET1")
.Save();
eh.Dispose();
}
public static void TestBuildPivot(IConfigurationRoot config)
{
Console.WriteLine("Begin TestBuildPivot");
var file = AppDomain.CurrentDomain.BaseDirectory + "/output/pivot.xlsx";
if (System.IO.File.Exists(file) != true)
{
TestBuildDataForPivot(config);
}
ExcelHelper eh = ExcelHelper.Create(file)
.DoPivotTable("SHEET1",
"SHEET2",
new string[] { "ShipCountry" },
new string[] { "ProductName" },
new string[] { "ExtendedPrice" }
)
.Save();
eh.Dispose();
Console.WriteLine("file created in " + file);
Console.WriteLine("End testBuildPivot");
}
public static void TestPivotAtOnce(IConfigurationRoot config)
{
Console.WriteLine("Begin TestPivotAtOnce");
string sql = "SELECT * FROM Invoices ORDER BY OrderDate";
var results = GetDataTable(config, sql, null);
var file = AppDomain.CurrentDomain.BaseDirectory + "/output/pivot_at_once.xlsx";
if (System.IO.File.Exists(file))
{
System.IO.File.Delete(file);
}
ExcelHelper eh = ExcelHelper.Create(file)
.Dump(results, "SHEET1")
.DoPivotTable("SHEET1",
"SHEET2",
new string[] { "ShipCountry" },
new string[] { "ProductName" },
new string[] { "ExtendedPrice" }
)
.Save();
eh.Dispose();
Console.WriteLine("file created in " + file);
Console.WriteLine("End testPivotAtOnce");
}
public static DataTable GetDataTable(IConfigurationRoot config, string sql, Hashtable _params = null)
{
//Console.WriteLine(config["ConnectionString"]);
SqlConnection conn = new SqlConnection(config["ConnectionString"]);
DataTable result = new DataTable();
SqlCommand cmd = null;
try
{
cmd = new SqlCommand(sql, conn);
cmd.Connection.Open();
if (_params != null)
{
}
SqlDataReader reader = cmd.ExecuteReader();
result.Load(reader);
reader.Close();
cmd.Connection.Close();
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
cmd.Connection.Close();
}
return result;
}
}
}