-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileHelper.cs
58 lines (52 loc) · 1.94 KB
/
FileHelper.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
using CsvHelper;
using CsvHelper.Configuration;
using ExcelDataReader;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace BatchAirdropClaim
{
public class WalletInfo
{
public int No { get; set; }
public string Address { get; set; }
public string PrivateKey { get; set; }
public decimal Amount { get; set; }
}
public static class FileHelper
{
public static List<WalletInfo> ImportCsv(string filePath)
{
using (var reader = new StreamReader(filePath))
using (var csv = new CsvReader(reader, new CsvConfiguration(System.Globalization.CultureInfo.InvariantCulture)))
{
return csv.GetRecords<WalletInfo>().ToList();
}
}
public static List<WalletInfo> ImportExcel(string filePath)
{
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
using (var stream = File.Open(filePath, FileMode.Open, FileAccess.Read))
{
using (var reader = ExcelReaderFactory.CreateReader(stream))
{
var result = reader.AsDataSet();
var dataTable = result.Tables[0];
var wallets = new List<WalletInfo>();
for (var i = 1; i < dataTable.Rows.Count; i++)
{
var row = dataTable.Rows[i];
wallets.Add(new WalletInfo
{
No = int.Parse(row[0].ToString()),
Address = row[1].ToString(),
PrivateKey = row[2].ToString(),
Amount = decimal.Parse(row[3].ToString())
});
}
return wallets;
}
}
}
}
}