Skip to content

Commit a1cf39c

Browse files
committed
v0.1.0 released
1 parent 34c07dc commit a1cf39c

File tree

14 files changed

+299
-18
lines changed

14 files changed

+299
-18
lines changed

EPT/Microsoft.Data.SqlClient.SNI.dll

499 KB
Binary file not shown.

EPT/e_sqlite3.dll

1.53 MB
Binary file not shown.

EPT/ept.exe

74.6 MB
Binary file not shown.

EPT/mato.db

32 KB
Binary file not shown.

EPT/sample/pattern.json

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
{
2+
"ExcelImport": {
3+
"SheetName": "",
4+
"SheetNumber": 0,
5+
"SkipRow": 3,
6+
"GenHeaderRow": true
7+
},
8+
"ExcelExport": {
9+
"SheetName": "ept-sheet1",
10+
"SheetNumber": 0,
11+
"SkipRow": 0,
12+
"GenHeaderRow": true
13+
},
14+
"DbExport": {
15+
"TableKeyType": "Guid",
16+
"TableName": "Employee"
17+
},
18+
"Patterns": [
19+
{
20+
"PropName": "stringValue",
21+
"HeaderName": "常规",
22+
"PropType": "string",
23+
"CellType": "常规",
24+
"Ignore": false,
25+
"Order": 0
26+
},
27+
{
28+
"PropName": "DateTimeValue",
29+
"HeaderName": "日期",
30+
"PropType": "DateTime",
31+
"CellType": "常规",
32+
"Ignore": false,
33+
"Order": 1
34+
},
35+
{
36+
"PropName": "IntValue",
37+
"HeaderName": "整数",
38+
"PropType": "int",
39+
"CellType": "常规",
40+
"Ignore": false,
41+
"Order": 2,
42+
"Validation": {
43+
"Target": "单元格数值",
44+
"Description": "整数值需要大于2",
45+
"Convention": "普通校验器",
46+
"Expression": "{value}>=2"
47+
}
48+
},
49+
{
50+
"PropName": "DoubleValue",
51+
"HeaderName": "小数",
52+
"PropType": "double",
53+
"CellType": "常规",
54+
"Ignore": false,
55+
"Order": 3
56+
},
57+
{
58+
"PropName": "boolValue",
59+
"HeaderName": "布尔值",
60+
"PropType": "bool",
61+
"CellType": "常规",
62+
"Ignore": false,
63+
"Order": 4
64+
},
65+
{
66+
"PropName": "StringWithNoteValue",
67+
"HeaderName": "常规(注释)",
68+
"PropType": "string",
69+
"CellType": "包含注解",
70+
"Ignore": false,
71+
"Order": 5
72+
},
73+
{
74+
"PropName": "StringWithStyleValue",
75+
"HeaderName": "常规(样式)",
76+
"PropType": "string",
77+
"CellType": "包含样式",
78+
"Ignore": false,
79+
"Order": 6
80+
},
81+
{
82+
"PropName": "IntWithFormula",
83+
"HeaderName": "公式",
84+
"PropType": "int",
85+
"CellType": "包含公式",
86+
"Ignore": false,
87+
"Order": 10,
88+
"Validation": {
89+
"Target": "单元格公式",
90+
"Description": "需要满足正则表达式",
91+
"Convention": "正则表达式校验器",
92+
"Expression": "^SUM\\(I\\d+,J\\d+\\)$"
93+
}
94+
}
95+
]
96+
}

EPT/sample/test.xlsx

13.7 KB
Binary file not shown.

EPT/screenshots/1.png

67.8 KB
Loading

EPT/screenshots/2.png

9.83 KB
Loading

EPT/screenshots/3.png

13.6 KB
Loading

ExcelPatternTool.Core/Excel/Core/BaseReader.cs

+5-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ internal object GetDataToObject(Type objType, IRow row, List<ColumnMetadata> col
2727

2828
for (int j = 0; j < columns.Count; j++)
2929
{
30+
if (columns[j].ColumnOrder<0)
31+
{
32+
continue;
33+
}
3034
ICell cell = row.GetCell(columns[j].ColumnOrder);
3135
if (cell==null)
3236
{
@@ -454,7 +458,7 @@ internal List<ColumnMetadata> GetTypeDefinition(Type type)
454458
tmp.PropName = prop.Name;
455459
tmp.PropType = prop.PropertyType;
456460
tmp.ColumnName = prop.Name;
457-
tmp.ColumnOrder = int.MaxValue;
461+
tmp.ColumnOrder = -1;
458462
foreach (var attr in attrs)
459463
{
460464
if (attr is ImportableAttribute)

ExcelPatternTool.Core/Excel/Core/BaseWriter.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ internal IEnumerable<ColumnMetadata> GetTypeDefinition(Type type)
2828
tmp.PropName = prop.Name;
2929
tmp.PropType = prop.PropertyType;
3030
tmp.ColumnName = prop.Name;
31-
tmp.ColumnOrder = int.MaxValue;
31+
tmp.ColumnOrder = -1;
3232
tmp.DefaultForNullOrInvalidValues = string.Empty;
3333

3434
foreach (var attr in attrs)

ExcelPatternTool.Tests/ExcelCore/XlsxWriterTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public void WriteRowsTest()
2323
var filePath = @"D:\test2.xlsx";
2424
exporter.DumpXlsx(filePath);
2525

26-
var eo=new ExportOption(1,1);
26+
var eo=new ExportOption<EmployeeEntity>(1,1);
2727
eo.SheetName = "Sheet1";
2828
eo.GenHeaderRow = true;
2929

ExcelPatternTool/CliProcessor.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
using ExcelPatternTool.Core.Helper;
2+
using System;
23
using System.Collections.Generic;
34
using System.Diagnostics;
45
using System.Linq;
@@ -19,7 +20,7 @@ partial class CliProcessor
1920

2021
public static void Usage()
2122
{
22-
var versionInfo = FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location);
23+
var versionInfo = FileVersionInfo.GetVersionInfo(Environment.ProcessPath);
2324
Console.WriteLine();
2425
Console.WriteLine("Excel Pattern Tool v{0}.{1}", versionInfo.FileMajorPart, versionInfo.FileMinorPart);
2526
Console.WriteLine("参数列表:");

README.md

+193-13
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,209 @@
11
# ExcelPatternTool
2-
## 介绍
32

4-
一个Excel与数据库互导工具。
5-
6-
## 特点:
7-
8-
9-
## 内容:
3+
Excel表格-数据库互导工具
104

5+
## 介绍:
6+
指定Pattern文件-一个规则描述的json文档,基于此规则实现Excel表格与数据库之间的导入导出,校验等功能。
117

8+
## 特点:
9+
1. 小巧,轻量化的命令行工具
10+
2. 基于json文档的配置
11+
3. 支持Excel97-2003(xls)与Excel2007及以上(xlsx)格式
12+
4. 数据库支持SQL server、Sqlite、MySql
13+
5. 支持单元格注解,样式,公式的导出(导出至Excel)
14+
6. 内置lambda表达式和正则表达式两种校验器
1215

1316
## 更新内容:
1417

15-
1618
Date | Version | Content
1719
:----------: | :-----------: | :-----------
1820
V0.1.0 | 2022-7-29 | 初始版本
1921

2022

2123
## 快速开始
2224

23-
完整示例请参考 [Sample]()
24-
25-
更多资讯请阅读系列博客
26-
27-
## 系列博客
25+
### 编写Pattern文档
26+
27+
1. 导入规则编写
28+
29+
* 指定表格的工作表名称SheetName或者工作表序号SheetNumber,二者选一配置即可,SheetName优先,SheetNumber从0开始
30+
* 指定开始行数SkipRow,这个是实际数据的开始行数,不包含表头行。在Sample中,这个行数为3
31+
32+
Sample:
33+
```
34+
"ExcelImport": { // excel导入规则
35+
"SheetName": "", // 工作表名称
36+
"SheetNumber": 0, // 工作表序号
37+
"SkipRow": 3 // 开始行数
38+
}
39+
```
40+
2. 导出规则编写
41+
* 指定数据库表的名称,主键类型。数据库类型将在Cli参数中指定
42+
43+
Sample:
44+
```
45+
"DbExport": { // Db导出规则
46+
"TableKeyType": "Guid", // 表主键类型 可选 "无","int","long","Guid",
47+
"TableName": "Employee" // 表名称
48+
}
49+
```
50+
3. Pattern配置
51+
52+
对列进行配置
53+
54+
* 列指定列标题名称,属性名称,类型和排序
55+
* 单元格类型为普通类型是"常规"时,直接输出的为单元格值,"包含注解","包含样式","包含公式","全包含"仅对导出至Excel有效
56+
* Ignore 为True时将忽略这一列,等效于无此列的Pattern设置
57+
* 列序号为此列在Excel中的编号,从0开始,即A列对应0,B列对应1 ...
58+
* 列属性类型PropType为bool时,可支持0,1,True,False
59+
60+
Sample:
61+
```
62+
63+
"Patterns": [ // Pattern配置
64+
{
65+
"PropName": "EmployeeName", // 属性名称
66+
"HeaderName": "姓名", // 列标题名称
67+
"PropType": "string", // 属性类型,可选 "string", "DateTime","int","double","bool",
68+
"CellType": "常规", // 单元格类型 可选 "常规","包含注解","包含样式","包含公式","全包含"
69+
"Ignore": false, // 是否忽略
70+
"Order": 0, // 列序号
71+
"Validation": { // 校验配置
72+
...
73+
}
74+
},
75+
```
76+
配置校验
77+
78+
* 配置Target,可对单元格值或单元格公式进行校验
79+
* 普通校验器时,{value}占位符代表当前单元格值或公式的内容
80+
* Sample1为普通校验器,校验单元格数值,Sample2为正则校验器,校验单元格公式
81+
82+
Sample1:
83+
```
84+
85+
"Validation": {
86+
"Target": "单元格数值",
87+
"Description": "整数值需要大于2",
88+
"Convention": "普通校验器",
89+
"Expression": "{value}>=2"
90+
}
91+
92+
```
93+
Sample2:
94+
```
95+
96+
"Validation": {
97+
"Target": "单元格公式",
98+
"Description": "需要满足正则表达式",
99+
"Convention": "正则表达式校验器",
100+
"Expression": "^ROUND\\(AN\\d+\\+BC\\d+\\+BD\\d+\\+BE\\d+\\+BF\\d+\\+BG\\d+\\+BH\\d+,2\\)$"
101+
}
102+
103+
```
104+
105+
完整示例请参考 [Sample](https://github.com/MatoApps/ExcelPatternTool/raw/master/EPT/sample/pattern.json)
106+
107+
### 安装
108+
109+
不需要特别的安装,在此获取[ept.exe](https://github.com/MatoApps/ExcelPatternTool/raw/master/EPT/ept.exe),或git pull代码后`生成`可执行文件
110+
111+
### 运行
112+
1. 进入可执行文件所在目录,并运行
113+
* 若要导出至Sqlite,请确保相同目录下包含`e_sqlite3.dll`
114+
* 若要导出至SQL server,请确保相同录下包含`Microsoft.Data.SqlClient.SNI.dll`
115+
116+
导出至Sqlite的Sample
117+
```
118+
.\ept.exe -p .\sample\pattern.json -i .\sample\test.xlsx -o "Data Source=mato.db" -s excel -d sqlite
119+
```
120+
导出至Excel的Sample
121+
```
122+
.\ept.exe -p .\sample\pattern.json -i .\sample\test.xlsx -o .\sample\output.xlsx -s excel -d excel
123+
```
124+
125+
2. 等待程序执行完毕
126+
![ss1](https://github.com/MatoApps/ExcelPatternTool/blob/master/EPT/screenshots/1.png)
127+
128+
### 结果
129+
130+
将在-o 参数指定的地址生成数据
131+
生成至Excel
132+
133+
![ss1](https://github.com/MatoApps/ExcelPatternTool/blob/master/EPT/screenshots/2.png)
134+
135+
136+
生成至Sqlite
137+
138+
![ss1](https://github.com/MatoApps/ExcelPatternTool/blob/master/EPT/screenshots/3.png)
139+
140+
141+
参数列表:
142+
143+
参数 | 含义 | 用法
144+
:----------: | :-----------: | :-----------
145+
-p | PatternFile | 指定一个Pattern文件(Json), 作为转换的模型文件
146+
-i | Input | 指定一个Excel文件路径,此文件将作为导入数据源<br>支持Xls或者Xlsx文件
147+
-o | Output | 指定一个路径,或Sql连接字符串作为导出目标<br>当指定 -d 参数为sqlserver, sqlite, mysql时,需指定为连接字符串;<br>当指定 -d 参数为excel时,需指定为将要另存的Excel文件路径,支持Xls或者Xlsx文件
148+
-s | Source | 值为excel
149+
-d | Destination | 值为excel, sqlserver, sqlite或者mysql
150+
-w | WaitAtEnd | 指定时,程序执行完成后,将等待用户输入退出
151+
-h | Help | 查看帮助
152+
153+
## 其他
154+
### 配置
155+
156+
ept.exe 相同目录下新建`appsettings.json`可自定义配置,若无此文件将采用自定义样式配置,如下:
157+
158+
```
159+
{
160+
"HeaderDefaultStyle": {
161+
"DefaultFontName": "宋体",
162+
"DefaultFontColor": "#FFFFFF",
163+
"DefaultFontSize": 10,
164+
"DefaultBorderColor": "#000000",
165+
"DefaultBackColor": "#888888"
166+
},
167+
"BodyDefaultStyle": {
168+
"DefaultFontName": "宋体",
169+
"DefaultFontColor": "#000000",
170+
"DefaultFontSize": 10,
171+
"DefaultBorderColor": "#000000",
172+
"DefaultBackColor": "#FFFFFF"
173+
},
174+
"CellComment": {
175+
"DefaultAuthor": "Linxiao"
176+
177+
}
178+
}
179+
```
180+
### 可扩展性
181+
182+
检验提供类ValidatorProvider类具有一定的扩展功能,
183+
InitConventions方法对校验行为进行初始化,默认提供RegularExpression,LambdaExpression对应的委托函数分别实现了正则表达式校验和普通表达式校验,重写InitConventions可实现一个自定义方式校验
184+
185+
Sample:
186+
187+
```
188+
public override Dictionary<string, ValidateConvention> InitConventions()
189+
{
190+
191+
var defaultConventions = base.InitConventions();
192+
//x 为当前列轮询的字段规则PatternItem对象,
193+
//e 为当前行轮询的Entity对象
194+
//返回ProcessResult作为校验结果
195+
defaultConventions.Add("MyExpression", new ValidateConvention((x, e) =>
196+
{
197+
//再此编写自定义校验功能
198+
//可用 x.PropName(或PropertyTypeMaper(x.PropName)) 获取当前列轮询的字段(Excel表头)名称
199+
//返回ProcessResult作为校验结果,IsValidated设置为true表示校验通过
200+
x.Validation.ProcessResult.IsValidated = true;
201+
return x.Validation.ProcessResult;
202+
}));
203+
204+
return defaultConventions;
205+
}
206+
```
28207

29208

30209
## 工具
@@ -37,6 +216,7 @@ V0.1.0 | 2022-7-29 | 初始版本
37216
## 已知问题
38217

39218

219+
40220
## 作者信息
41221

42222
作者:林小

0 commit comments

Comments
 (0)