-
Notifications
You must be signed in to change notification settings - Fork 1
/
ImportHelper.cs
176 lines (141 loc) · 6.39 KB
/
ImportHelper.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
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace iDiTect.Excel.Demo
{
public static class ImportHelper
{
public static void ImportDataFromArray()
{
//Create a data array
var arrayData = new List<object[]>()
{
new object[] {"Name", "BuyIn", "SaleOut", "OrderDate"},
new object[] {"Orange", 63, 45, "2000-04-25"},
new object[] {"Apple", 89, 92, "2000-01-12"},
new object[] {"Banana", 67, 83, "2000-05-21"},
new object[] {"Pear", 21, 43, "2000-03-09"},
new object[] {"Grape", 71, 17, "2000-02-03"},
new object[] {"Watermelon", 33, 55, "2000-03-09"},
};
var excelApp = new Application();
var worksheet = excelApp.Workbook.Worksheets.Add("ImportFromArray");
//Insert array to worksheet cells
var dataRange = worksheet.Cells["A1"].LoadFromArrays(arrayData);
worksheet.Cells["B2:B7"].Style.Numberformat.Format = "#,##0";
worksheet.Cells["C2:C7"].Style.Numberformat.Format = "#,##0";
worksheet.Cells["D2:D7"].Style.Numberformat.Format = "yyyy-mm-dd";
excelApp.Save("ImportFromArray.xlsx");
}
public static void ImportDataFromDataTable()
{
//Create a data table
string[] fruits = { "Orange", "Apple", "Banana", "Pear", "Grape", "Watermelon" };
DataTable dt = new DataTable("Fruit Sales");
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Monday", typeof(long));
dt.Columns.Add("Tuesday", typeof(long));
dt.Columns.Add("Wednesday", typeof(long));
dt.Columns.Add("Thursday", typeof(long));
dt.Columns.Add("Friday", typeof(long));
dt.Columns.Add("Saturday ", typeof(long));
dt.Columns.Add("Sunday", typeof(long));
Random r = new Random();
for (int row = 0; row < 6; row++)
{
var dr = dt.NewRow();
dr[0] = fruits[row];
for (int col = 1; col < 8; col++)
{
dr[col] = r.Next(10, 100);
}
dt.Rows.Add(dr);
}
var excelApp = new Application();
var worksheet = excelApp.Workbook.Worksheets.Add("ImportFromDataTable");
//Insert table to worksheet cells
var dataRange = worksheet.Cells["A1"].LoadFromDataTable(dt, true);
worksheet.Cells["B2:H7"].Style.Numberformat.Format = "#,##0";
excelApp.Save("ImportFromDataTable.xlsx");
}
public static void ImportDataFromDataReader()
{
var excelApp = new Application();
//Create new worksheet by name
var worksheet = excelApp.Workbook.Worksheets.Add("ImportFromDataReader");
//Load data from database
string connectionStr = "Data Source=(local);Initial Catalog=Test;User ID=sa;Password=123456";
using (SqlConnection sqlConn = new SqlConnection(connectionStr))
{
sqlConn.Open();
using (SqlCommand sqlCmd = new SqlCommand("select column-a, column-b, column-c from table", sqlConn))
{
using (SqlDataReader sqlReader = sqlCmd.ExecuteReader())
{
//Insert data from sql DataReader to worksheet cells
worksheet.Cells["A1"].LoadFromDataReader(sqlReader, true);
sqlReader.Close();
}
}
sqlConn.Close();
}
excelApp.Save("ImportFromDataReader.xlsx");
}
public static void ImportDataFromIEnumerable()
{
//Creat data from list of customized object
List<Product> fruits = new List<Product>();
var names = new string[] { "Orange", "Apple", "Banana", "Pear", "Grape", "Watermelon" };
Random r = new Random();
for (int i = 0; i < 50; i++)
{
fruits.Add(
new Product()
{
Name = names[r.Next(6)],
BuyIn = r.Next(50, 200),
SaleOut = r.Next(50, 200),
OrderDate = new DateTime(2000, 1, 1).AddDays(r.Next(1000)),
});
}
var excelApp = new Application();
var worksheet = excelApp.Workbook.Worksheets.Add("ImportFromIEnumerable");
//Insert IEnumerable<T> to worksheet cells
var dataRange = worksheet.Cells["A1"].LoadFromCollection(fruits);
worksheet.Cells["B2:B50"].Style.Numberformat.Format = "#,##0";
worksheet.Cells["C2:C50"].Style.Numberformat.Format = "#,##0";
worksheet.Cells["D2:D50"].Style.Numberformat.Format = "yyyy-mm-dd";
excelApp.Save("ImportFromIEnumerable.xlsx");
}
public static void ImportDataFromTextFile()
{
var excelApp = new Application();
var worksheet = excelApp.Workbook.Worksheets.Add("ImportFromTextFile");
//Create the format object to describe the text file
var format = new TextFormat();
format.Delimiter = '\t'; //Tab
format.TextQualifier = '"';
format.SkipLinesBeginning = 1;
FileInfo textFile = new FileInfo("load-from-text.txt");
//Insert data from text/csv file to worksheet cells
var dataRange = worksheet.Cells["A1"].LoadFromText(textFile, format);
worksheet.Cells["B2:B50"].Style.Numberformat.Format = "#,##0";
worksheet.Cells["C2:C50"].Style.Numberformat.Format = "#,##0";
worksheet.Cells["D2:D50"].Style.Numberformat.Format = "yyyy-mm-dd";
worksheet.Cells["D2:D50"].AutoFitColumns();
excelApp.Save("ImportFromTextFile.xlsx");
}
public class Product
{
public string Name { get; set; }
public int BuyIn { get; set; }
public int SaleOut { get; set; }
public DateTime OrderDate { get; set; }
}
}
}