Skip to content

Commit ce1f9f8

Browse files
author
283591387@qq.com
committed
增加导入时读取原生excel方法(可以自定义读取excel内容)
1 parent ed95bb2 commit ce1f9f8

File tree

9 files changed

+84
-17
lines changed

9 files changed

+84
-17
lines changed

.Net6版本/VOL.Core/BaseProvider/ServiceBase.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ private void CheckUpdateMultiTenancy(string ids, string tableKey)
125125
//例如sql,只能(编辑)自己创建的数据:判断数据是不是当前用户创建的
126126
//sql = $" {sql} and createid!={UserContext.Current.UserId}";
127127
object obj = repository.DapperContext.ExecuteScalar(sql, null);
128-
if (obj == null || obj.GetInt() > 0)
128+
if (obj == null || obj.GetInt() == 0)
129129
{
130130
Response.Error("不能编辑此数据");
131131
}
@@ -143,7 +143,8 @@ private void CheckDelMultiTenancy(string ids, string tableKey)
143143
//例如sql,只能(删除)自己创建的数据:找出不是自己创建的数据
144144
//sql = $" {sql} and createid!={UserContext.Current.UserId}";
145145
object obj = repository.DapperContext.ExecuteScalar(sql, null);
146-
if (obj == null || obj.GetInt() > 0)
146+
int idsCount = ids.Split(",").Distinct().Count();
147+
if (obj == null || obj.GetInt() != idsCount)
147148
{
148149
Response.Error("不能删除此数据");
149150
}
@@ -425,7 +426,8 @@ public virtual WebResponseContent Import(List<Microsoft.AspNetCore.Http.IFormFil
425426
}
426427
try
427428
{
428-
Response = EPPlusHelper.ReadToDataTable<T>(dicPath, DownLoadTemplateColumns, GetIgnoreTemplate());
429+
//2022.06.20增加原生excel读取方法(导入时可以自定义读取excel内容)
430+
Response = EPPlusHelper.ReadToDataTable<T>(dicPath, DownLoadTemplateColumns, GetIgnoreTemplate(), readValue: ImportOnReadCellValue);
429431
}
430432
catch (Exception ex)
431433
{
@@ -443,15 +445,15 @@ public virtual WebResponseContent Import(List<Microsoft.AspNetCore.Http.IFormFil
443445
if (HttpContext.Current.Request.Query.ContainsKey("table"))
444446
{
445447
ImportOnExecuted?.Invoke(list);
446-
return Response.OK("文件上传成功",list.Serialize());
448+
return Response.OK("文件上传成功", list.Serialize());
447449
}
448450
repository.AddRange(list, true);
449451
if (ImportOnExecuted != null)
450452
{
451453
Response = ImportOnExecuted.Invoke(list);
452454
if (CheckResponseResult()) return Response;
453455
}
454-
return Response.OK("文件上传成功" );
456+
return Response.OK("文件上传成功");
455457
}
456458

457459
/// <summary>

.Net6版本/VOL.Core/Filters/ServiceFunFilter.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
using OfficeOpenXml;
2+
using System;
23
using System.Collections.Generic;
34
using System.Linq;
45
using System.Linq.Expressions;
@@ -8,7 +9,7 @@
89

910
namespace VOL.Core.Filters
1011
{
11-
public abstract class ServiceFunFilter<T> where T : class
12+
public abstract class ServiceFunFilter<T> where T : class
1213
{
1314

1415
/// <summary>
@@ -192,5 +193,16 @@ public abstract class ServiceFunFilter<T> where T : class
192193
/// </summary>
193194
protected Func<List<T>, WebResponseContent> ImportOnExecuting;
194195

196+
/// <summary>
197+
/// 2022.06.20增加原生excel读取方法(导入时可以自定义读取excel内容)
198+
/// string=当前读取的excel单元格的值
199+
/// ExcelWorksheet=excel对象
200+
/// ExcelRange当前excel单元格对象
201+
/// int=当前读取的第几数
202+
/// int=当前读取的第几列
203+
/// string=返回的值
204+
/// </summary>
205+
protected Func<string, ExcelWorksheet, ExcelRange, int, int, string> ImportOnReadCellValue;
206+
195207
}
196208
}

.Net6版本/VOL.Core/Utilities/EPPlusHelper.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ public class EPPlusHelper
2626
/// <param name="ignoreColumns">忽略不导出的列(如果设置了exportColumns,ignoreColumns不会生效)</param>
2727
/// <returns></returns>
2828

29-
public static WebResponseContent ReadToDataTable<T>(string path, Expression<Func<T, object>> exportColumns = null, List<string> ignoreTemplate = null)
29+
public static WebResponseContent ReadToDataTable<T>(string path,
30+
Expression<Func<T, object>> exportColumns = null,
31+
List<string> ignoreTemplate = null,
32+
Func<string, ExcelWorksheet, ExcelRange, int, int, string> readValue = null)
3033
{
3134
WebResponseContent responseContent = new WebResponseContent();
3235

@@ -90,7 +93,13 @@ public static WebResponseContent ReadToDataTable<T>(string path, Expression<Func
9093
T entity = Activator.CreateInstance<T>();
9194
for (int j = sheet.Dimension.Start.Column, k = sheet.Dimension.End.Column; j <= k; j++)
9295
{
96+
9397
string value = sheet.Cells[m, j].Value?.ToString();
98+
//2022.06.20增加原生excel读取方法
99+
if (readValue != null)
100+
{
101+
value = readValue(value, sheet, sheet.Cells[m, j], m, j);
102+
}
94103

95104
CellOptions options = cellOptions.Where(x => x.Index == j).FirstOrDefault();
96105
PropertyInfo property = propertyInfos.Where(x => x.Name == options.ColumnName).FirstOrDefault();
@@ -516,7 +525,7 @@ private static List<CellOptions> GetExportColumnInfo(string tableName, bool teml
516525
}
517526
return cellOptions;
518527
}
519-
528+
520529
/// <summary>
521530
/// 2021.01.10增加通过excel导出功能
522531
/// </summary>

Vue.Net/VOL.Core/BaseProvider/ServiceBase.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,8 @@ public virtual WebResponseContent Import(List<Microsoft.AspNetCore.Http.IFormFil
426426
}
427427
try
428428
{
429-
Response = EPPlusHelper.ReadToDataTable<T>(dicPath, DownLoadTemplateColumns, GetIgnoreTemplate());
429+
//2022.06.20增加原生excel读取方法(导入时可以自定义读取excel内容)
430+
Response = EPPlusHelper.ReadToDataTable<T>(dicPath, DownLoadTemplateColumns, GetIgnoreTemplate(),readValue: ImportOnReadCellValue);
430431
}
431432
catch (Exception ex)
432433
{

Vue.Net/VOL.Core/Filters/ServiceFunFilter.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
using OfficeOpenXml;
2+
using System;
23
using System.Collections.Generic;
34
using System.Linq;
45
using System.Linq.Expressions;
@@ -192,5 +193,16 @@ public abstract class ServiceFunFilter<T> where T : class
192193
/// </summary>
193194
protected Func<List<T>, WebResponseContent> ImportOnExecuting;
194195

196+
/// <summary>
197+
/// 2022.06.20增加原生excel读取方法(导入时可以自定义读取excel内容)
198+
/// string=当前读取的excel单元格的值
199+
/// ExcelWorksheet=excel对象
200+
/// ExcelRange当前excel单元格对象
201+
/// int=当前读取的第几数
202+
/// int=当前读取的第几列
203+
/// string=返回的值
204+
/// </summary>
205+
protected Func<string, ExcelWorksheet, ExcelRange,int,int, string> ImportOnReadCellValue;
206+
195207
}
196208
}

Vue.Net/VOL.Core/Utilities/EPPlusHelper.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ public class EPPlusHelper
2626
/// <param name="ignoreColumns">忽略不导出的列(如果设置了exportColumns,ignoreColumns不会生效)</param>
2727
/// <returns></returns>
2828

29-
public static WebResponseContent ReadToDataTable<T>(string path, Expression<Func<T, object>> exportColumns = null, List<string> ignoreTemplate = null)
29+
public static WebResponseContent ReadToDataTable<T>(string path,
30+
Expression<Func<T, object>> exportColumns = null,
31+
List<string> ignoreTemplate = null,
32+
Func<string, ExcelWorksheet, ExcelRange,int,int, string> readValue = null)
3033
{
3134
WebResponseContent responseContent = new WebResponseContent();
3235

@@ -90,7 +93,13 @@ public static WebResponseContent ReadToDataTable<T>(string path, Expression<Func
9093
T entity = Activator.CreateInstance<T>();
9194
for (int j = sheet.Dimension.Start.Column, k = sheet.Dimension.End.Column; j <= k; j++)
9295
{
96+
9397
string value = sheet.Cells[m, j].Value?.ToString();
98+
//2022.06.20增加原生excel读取方法
99+
if (readValue != null)
100+
{
101+
value = readValue(value, sheet, sheet.Cells[m, j],m,j);
102+
}
94103

95104
CellOptions options = cellOptions.Where(x => x.Index == j).FirstOrDefault();
96105
PropertyInfo property = propertyInfos.Where(x => x.Name == options.ColumnName).FirstOrDefault();
@@ -516,7 +525,7 @@ private static List<CellOptions> GetExportColumnInfo(string tableName, bool teml
516525
}
517526
return cellOptions;
518527
}
519-
528+
520529
/// <summary>
521530
/// 2021.01.10增加通过excel导出功能
522531
/// </summary>

开发版dev/Vue.NetCore/Vue.Net/VOL.Core/BaseProvider/ServiceBase.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,8 @@ public virtual WebResponseContent Import(List<Microsoft.AspNetCore.Http.IFormFil
426426
}
427427
try
428428
{
429-
Response = EPPlusHelper.ReadToDataTable<T>(dicPath, DownLoadTemplateColumns, GetIgnoreTemplate());
429+
//2022.06.20增加原生excel读取方法(导入时可以自定义读取excel内容)
430+
Response = EPPlusHelper.ReadToDataTable<T>(dicPath, DownLoadTemplateColumns, GetIgnoreTemplate(),readValue: ImportOnReadCellValue);
430431
}
431432
catch (Exception ex)
432433
{

开发版dev/Vue.NetCore/Vue.Net/VOL.Core/Filters/ServiceFunFilter.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
using OfficeOpenXml;
2+
using System;
23
using System.Collections.Generic;
34
using System.Linq;
45
using System.Linq.Expressions;
@@ -192,5 +193,16 @@ public abstract class ServiceFunFilter<T> where T : class
192193
/// </summary>
193194
protected Func<List<T>, WebResponseContent> ImportOnExecuting;
194195

196+
/// <summary>
197+
/// 2022.06.20增加原生excel读取方法(导入时可以自定义读取excel内容)
198+
/// string=当前读取的excel单元格的值
199+
/// ExcelWorksheet=excel对象
200+
/// ExcelRange当前excel单元格对象
201+
/// int=当前读取的第几数
202+
/// int=当前读取的第几列
203+
/// string=返回的值
204+
/// </summary>
205+
protected Func<string, ExcelWorksheet, ExcelRange,int,int, string> ImportOnReadCellValue;
206+
195207
}
196208
}

开发版dev/Vue.NetCore/Vue.Net/VOL.Core/Utilities/EPPlusHelper.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ public class EPPlusHelper
2626
/// <param name="ignoreColumns">忽略不导出的列(如果设置了exportColumns,ignoreColumns不会生效)</param>
2727
/// <returns></returns>
2828

29-
public static WebResponseContent ReadToDataTable<T>(string path, Expression<Func<T, object>> exportColumns = null, List<string> ignoreTemplate = null)
29+
public static WebResponseContent ReadToDataTable<T>(string path,
30+
Expression<Func<T, object>> exportColumns = null,
31+
List<string> ignoreTemplate = null,
32+
Func<string, ExcelWorksheet, ExcelRange,int,int, string> readValue = null)
3033
{
3134
WebResponseContent responseContent = new WebResponseContent();
3235

@@ -90,7 +93,13 @@ public static WebResponseContent ReadToDataTable<T>(string path, Expression<Func
9093
T entity = Activator.CreateInstance<T>();
9194
for (int j = sheet.Dimension.Start.Column, k = sheet.Dimension.End.Column; j <= k; j++)
9295
{
96+
9397
string value = sheet.Cells[m, j].Value?.ToString();
98+
//2022.06.20增加原生excel读取方法
99+
if (readValue != null)
100+
{
101+
value = readValue(value, sheet, sheet.Cells[m, j],m,j);
102+
}
94103

95104
CellOptions options = cellOptions.Where(x => x.Index == j).FirstOrDefault();
96105
PropertyInfo property = propertyInfos.Where(x => x.Name == options.ColumnName).FirstOrDefault();
@@ -516,7 +525,7 @@ private static List<CellOptions> GetExportColumnInfo(string tableName, bool teml
516525
}
517526
return cellOptions;
518527
}
519-
528+
520529
/// <summary>
521530
/// 2021.01.10增加通过excel导出功能
522531
/// </summary>

0 commit comments

Comments
 (0)