This repository was archived by the owner on Sep 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
95 lines (82 loc) · 3.17 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
using System;
using System.Globalization;
using System.IO;
using System.Text;
using ExcelDataReader;
namespace ExcelPass
{
class Program
{
static int Main(string[] args)
{
Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
string pass = args.Length > 0 ? args[0] : "123456";
var conf = new ExcelReaderConfiguration { Password = pass };
NumberFormatInfo nfi = new NumberFormatInfo
{
NumberDecimalSeparator = "."
};
try
{
ExcelTest(@"..\..\..\secretExcelFile.xlsx", @"..\..\..\wellKnownSecrets.csv", ';', conf, nfi);
}
catch (ExcelDataReader.Exceptions.InvalidPasswordException e)
{
Console.WriteLine(e.Message);
return 1;
}
catch (Exception e2)
{
Console.WriteLine(e2.Message);
return 2;
}
return 0;
}
static void ExcelTest(string xlsxPath, string csvPath, char d, ExcelReaderConfiguration conf, NumberFormatInfo nfi)
{
using (var stream = File.Open(xlsxPath, FileMode.Open, FileAccess.Read))
{
using (var reader = ExcelReaderFactory.CreateReader(stream, conf))
{
int wNum = 0;
do
{
int kolNum = reader.FieldCount;
int rowNum = reader.RowCount;
StreamWriter sw = new StreamWriter($"{csvPath[0..(csvPath.Length - 4)]}{++wNum}.csv", false, Encoding.UTF8);
Type cellType;
while (reader.Read())
{
for (int i = 0; i < kolNum; i++)
{
cellType = reader.GetFieldType(i);
if (cellType == typeof(string))
{
sw.Write(reader.GetString(i));
}
else if (cellType == typeof(double) || cellType == typeof(decimal))
{
sw.Write(reader.GetDouble(i).ToString(nfi));
}
else if (cellType == typeof(DateTime))
{
sw.Write(reader.GetDateTime(i).ToString("yyyy-MM-dd HH:mm:ss"));
}
else
{
sw.Write(reader.GetValue(i));
}
if (i < kolNum - 1)
{
sw.Write(d);
}
}
sw.WriteLine();
}
sw.Close();
} while (reader.NextResult());
}
}
}
}
}