Skip to content

Commit

Permalink
v0.1.0 released
Browse files Browse the repository at this point in the history
  • Loading branch information
jevonsflash committed Aug 1, 2022
1 parent 34c07dc commit a1cf39c
Show file tree
Hide file tree
Showing 14 changed files with 299 additions and 18 deletions.
Binary file added EPT/Microsoft.Data.SqlClient.SNI.dll
Binary file not shown.
Binary file added EPT/e_sqlite3.dll
Binary file not shown.
Binary file added EPT/ept.exe
Binary file not shown.
Binary file added EPT/mato.db
Binary file not shown.
96 changes: 96 additions & 0 deletions EPT/sample/pattern.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
{
"ExcelImport": {
"SheetName": "",
"SheetNumber": 0,
"SkipRow": 3,
"GenHeaderRow": true
},
"ExcelExport": {
"SheetName": "ept-sheet1",
"SheetNumber": 0,
"SkipRow": 0,
"GenHeaderRow": true
},
"DbExport": {
"TableKeyType": "Guid",
"TableName": "Employee"
},
"Patterns": [
{
"PropName": "stringValue",
"HeaderName": "常规",
"PropType": "string",
"CellType": "常规",
"Ignore": false,
"Order": 0
},
{
"PropName": "DateTimeValue",
"HeaderName": "日期",
"PropType": "DateTime",
"CellType": "常规",
"Ignore": false,
"Order": 1
},
{
"PropName": "IntValue",
"HeaderName": "整数",
"PropType": "int",
"CellType": "常规",
"Ignore": false,
"Order": 2,
"Validation": {
"Target": "单元格数值",
"Description": "整数值需要大于2",
"Convention": "普通校验器",
"Expression": "{value}>=2"
}
},
{
"PropName": "DoubleValue",
"HeaderName": "小数",
"PropType": "double",
"CellType": "常规",
"Ignore": false,
"Order": 3
},
{
"PropName": "boolValue",
"HeaderName": "布尔值",
"PropType": "bool",
"CellType": "常规",
"Ignore": false,
"Order": 4
},
{
"PropName": "StringWithNoteValue",
"HeaderName": "常规(注释)",
"PropType": "string",
"CellType": "包含注解",
"Ignore": false,
"Order": 5
},
{
"PropName": "StringWithStyleValue",
"HeaderName": "常规(样式)",
"PropType": "string",
"CellType": "包含样式",
"Ignore": false,
"Order": 6
},
{
"PropName": "IntWithFormula",
"HeaderName": "公式",
"PropType": "int",
"CellType": "包含公式",
"Ignore": false,
"Order": 10,
"Validation": {
"Target": "单元格公式",
"Description": "需要满足正则表达式",
"Convention": "正则表达式校验器",
"Expression": "^SUM\\(I\\d+,J\\d+\\)$"
}
}
]
}
Binary file added EPT/sample/test.xlsx
Binary file not shown.
Binary file added EPT/screenshots/1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added EPT/screenshots/2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added EPT/screenshots/3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 5 additions & 1 deletion ExcelPatternTool.Core/Excel/Core/BaseReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ internal object GetDataToObject(Type objType, IRow row, List<ColumnMetadata> col

for (int j = 0; j < columns.Count; j++)
{
if (columns[j].ColumnOrder<0)
{
continue;
}
ICell cell = row.GetCell(columns[j].ColumnOrder);
if (cell==null)
{
Expand Down Expand Up @@ -454,7 +458,7 @@ internal List<ColumnMetadata> GetTypeDefinition(Type type)
tmp.PropName = prop.Name;
tmp.PropType = prop.PropertyType;
tmp.ColumnName = prop.Name;
tmp.ColumnOrder = int.MaxValue;
tmp.ColumnOrder = -1;
foreach (var attr in attrs)
{
if (attr is ImportableAttribute)
Expand Down
2 changes: 1 addition & 1 deletion ExcelPatternTool.Core/Excel/Core/BaseWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ internal IEnumerable<ColumnMetadata> GetTypeDefinition(Type type)
tmp.PropName = prop.Name;
tmp.PropType = prop.PropertyType;
tmp.ColumnName = prop.Name;
tmp.ColumnOrder = int.MaxValue;
tmp.ColumnOrder = -1;
tmp.DefaultForNullOrInvalidValues = string.Empty;

foreach (var attr in attrs)
Expand Down
2 changes: 1 addition & 1 deletion ExcelPatternTool.Tests/ExcelCore/XlsxWriterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public void WriteRowsTest()
var filePath = @"D:\test2.xlsx";
exporter.DumpXlsx(filePath);

var eo=new ExportOption(1,1);
var eo=new ExportOption<EmployeeEntity>(1,1);
eo.SheetName = "Sheet1";
eo.GenHeaderRow = true;

Expand Down
5 changes: 3 additions & 2 deletions ExcelPatternTool/CliProcessor.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using ExcelPatternTool.Core.Helper;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
Expand All @@ -19,7 +20,7 @@ partial class CliProcessor

public static void Usage()
{
var versionInfo = FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location);
var versionInfo = FileVersionInfo.GetVersionInfo(Environment.ProcessPath);
Console.WriteLine();
Console.WriteLine("Excel Pattern Tool v{0}.{1}", versionInfo.FileMajorPart, versionInfo.FileMinorPart);
Console.WriteLine("参数列表:");
Expand Down
206 changes: 193 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,30 +1,209 @@
# ExcelPatternTool
## 介绍

一个Excel与数据库互导工具。

## 特点:


## 内容:
Excel表格-数据库互导工具

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

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

## 更新内容:


Date | Version | Content
:----------: | :-----------: | :-----------
V0.1.0 | 2022-7-29 | 初始版本


## 快速开始

完整示例请参考 [Sample]()

更多资讯请阅读系列博客

## 系列博客
### 编写Pattern文档

1. 导入规则编写

* 指定表格的工作表名称SheetName或者工作表序号SheetNumber,二者选一配置即可,SheetName优先,SheetNumber从0开始
* 指定开始行数SkipRow,这个是实际数据的开始行数,不包含表头行。在Sample中,这个行数为3

Sample:
```
"ExcelImport": { // excel导入规则
"SheetName": "", // 工作表名称
"SheetNumber": 0, // 工作表序号
"SkipRow": 3 // 开始行数
}
```
2. 导出规则编写
* 指定数据库表的名称,主键类型。数据库类型将在Cli参数中指定

Sample:
```
"DbExport": { // Db导出规则
"TableKeyType": "Guid", // 表主键类型 可选 "无","int","long","Guid",
"TableName": "Employee" // 表名称
}
```
3. Pattern配置

对列进行配置

* 列指定列标题名称,属性名称,类型和排序
* 单元格类型为普通类型是"常规"时,直接输出的为单元格值,"包含注解","包含样式","包含公式","全包含"仅对导出至Excel有效
* Ignore 为True时将忽略这一列,等效于无此列的Pattern设置
* 列序号为此列在Excel中的编号,从0开始,即A列对应0,B列对应1 ...
* 列属性类型PropType为bool时,可支持0,1,True,False

Sample:
```
"Patterns": [ // Pattern配置
{
"PropName": "EmployeeName", // 属性名称
"HeaderName": "姓名", // 列标题名称
"PropType": "string", // 属性类型,可选 "string", "DateTime","int","double","bool",
"CellType": "常规", // 单元格类型 可选 "常规","包含注解","包含样式","包含公式","全包含"
"Ignore": false, // 是否忽略
"Order": 0, // 列序号
"Validation": { // 校验配置
...
}
},
```
配置校验

* 配置Target,可对单元格值或单元格公式进行校验
* 普通校验器时,{value}占位符代表当前单元格值或公式的内容
* Sample1为普通校验器,校验单元格数值,Sample2为正则校验器,校验单元格公式

Sample1:
```
"Validation": {
"Target": "单元格数值",
"Description": "整数值需要大于2",
"Convention": "普通校验器",
"Expression": "{value}>=2"
}
```
Sample2:
```
"Validation": {
"Target": "单元格公式",
"Description": "需要满足正则表达式",
"Convention": "正则表达式校验器",
"Expression": "^ROUND\\(AN\\d+\\+BC\\d+\\+BD\\d+\\+BE\\d+\\+BF\\d+\\+BG\\d+\\+BH\\d+,2\\)$"
}
```

完整示例请参考 [Sample](https://github.com/MatoApps/ExcelPatternTool/raw/master/EPT/sample/pattern.json)

### 安装

不需要特别的安装,在此获取[ept.exe](https://github.com/MatoApps/ExcelPatternTool/raw/master/EPT/ept.exe),或git pull代码后`生成`可执行文件

### 运行
1. 进入可执行文件所在目录,并运行
* 若要导出至Sqlite,请确保相同目录下包含`e_sqlite3.dll`
* 若要导出至SQL server,请确保相同录下包含`Microsoft.Data.SqlClient.SNI.dll`

导出至Sqlite的Sample
```
.\ept.exe -p .\sample\pattern.json -i .\sample\test.xlsx -o "Data Source=mato.db" -s excel -d sqlite
```
导出至Excel的Sample
```
.\ept.exe -p .\sample\pattern.json -i .\sample\test.xlsx -o .\sample\output.xlsx -s excel -d excel
```

2. 等待程序执行完毕
![ss1](https://github.com/MatoApps/ExcelPatternTool/blob/master/EPT/screenshots/1.png)

### 结果

将在-o 参数指定的地址生成数据
生成至Excel

![ss1](https://github.com/MatoApps/ExcelPatternTool/blob/master/EPT/screenshots/2.png)


生成至Sqlite

![ss1](https://github.com/MatoApps/ExcelPatternTool/blob/master/EPT/screenshots/3.png)


参数列表:

参数 | 含义 | 用法
:----------: | :-----------: | :-----------
-p | PatternFile | 指定一个Pattern文件(Json), 作为转换的模型文件
-i | Input | 指定一个Excel文件路径,此文件将作为导入数据源<br>支持Xls或者Xlsx文件
-o | Output | 指定一个路径,或Sql连接字符串作为导出目标<br>当指定 -d 参数为sqlserver, sqlite, mysql时,需指定为连接字符串;<br>当指定 -d 参数为excel时,需指定为将要另存的Excel文件路径,支持Xls或者Xlsx文件
-s | Source | 值为excel
-d | Destination | 值为excel, sqlserver, sqlite或者mysql
-w | WaitAtEnd | 指定时,程序执行完成后,将等待用户输入退出
-h | Help | 查看帮助

## 其他
### 配置

ept.exe 相同目录下新建`appsettings.json`可自定义配置,若无此文件将采用自定义样式配置,如下:

```
{
"HeaderDefaultStyle": {
"DefaultFontName": "宋体",
"DefaultFontColor": "#FFFFFF",
"DefaultFontSize": 10,
"DefaultBorderColor": "#000000",
"DefaultBackColor": "#888888"
},
"BodyDefaultStyle": {
"DefaultFontName": "宋体",
"DefaultFontColor": "#000000",
"DefaultFontSize": 10,
"DefaultBorderColor": "#000000",
"DefaultBackColor": "#FFFFFF"
},
"CellComment": {
"DefaultAuthor": "Linxiao"
}
}
```
### 可扩展性

检验提供类ValidatorProvider类具有一定的扩展功能,
InitConventions方法对校验行为进行初始化,默认提供RegularExpression,LambdaExpression对应的委托函数分别实现了正则表达式校验和普通表达式校验,重写InitConventions可实现一个自定义方式校验

Sample:

```
public override Dictionary<string, ValidateConvention> InitConventions()
{
var defaultConventions = base.InitConventions();
//x 为当前列轮询的字段规则PatternItem对象,
//e 为当前行轮询的Entity对象
//返回ProcessResult作为校验结果
defaultConventions.Add("MyExpression", new ValidateConvention((x, e) =>
{
//再此编写自定义校验功能
//可用 x.PropName(或PropertyTypeMaper(x.PropName)) 获取当前列轮询的字段(Excel表头)名称
//返回ProcessResult作为校验结果,IsValidated设置为true表示校验通过
x.Validation.ProcessResult.IsValidated = true;
return x.Validation.ProcessResult;
}));
return defaultConventions;
}
```


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



## 作者信息

作者:林小
Expand Down

0 comments on commit a1cf39c

Please sign in to comment.