This commit is contained in:
FanLian
2023-08-07 16:58:51 +08:00
3 changed files with 129 additions and 7 deletions

View File

@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Tnb.WarehouseMgr.Entities.Attributes
{
[AttributeUsage(AttributeTargets.Property)]
public class RelationalFieldAttribute : Attribute
{
/// <summary>
/// 关联表名称
/// </summary>
public string Table { get; set; }
/// <summary>
/// 字段
/// </summary>
public string Field { get; set; }
}
}

View File

@@ -4,6 +4,7 @@ using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Security.Claims;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using Aspose.Cells.Drawing;
@@ -15,10 +16,15 @@ using JNPF.Common.Extension;
using JNPF.DataEncryption;
using JNPF.DependencyInjection;
using JNPF.DynamicApiController;
using JNPF.FriendlyException;
using JNPF.Systems.Interfaces.System;
using JNPF.VisualDev;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using SqlSugar;
using Tnb.BasicData.Entities;
using Tnb.WarehouseMgr.Entities;
@@ -86,6 +92,77 @@ namespace Tnb.WarehouseMgr
}
return Task.FromResult(isMatch);
}
/// <summary>
/// 解析Excel到内存
/// </summary>
/// <param name="file"></param>
/// <returns></returns>
protected Task<Dictionary<string, object>> ImportExcelToMemory(IFormFile file)
{
int rowIndex = 1;
Dictionary<string, object> dic = new();
IWorkbook? workbook = null;
try
{
using (Stream stream = file.OpenReadStream())
{
// 2007版本
if (file.Name.IndexOf(".xlsx") > 0)
workbook = new XSSFWorkbook(stream);
else if (file.Name.IndexOf(".xls") > 0)
workbook = new HSSFWorkbook(stream);
ISheet? sheet = workbook?.GetSheetAt(0);
if (workbook == null || sheet == null)
throw Oops.Bah("无导入数据");
if (sheet?.LastRowNum <= 1)
throw Oops.Bah("无导入数据");
ICellStyle style = workbook.CreateCellStyle();
IFont font = workbook.CreateFont();
font.Color = IndexedColors.Red.Index; // 将字体颜色设置为红色
style.SetFont(font);
var nameRow = sheet?.GetRow(0);
if (nameRow != null)
{
List<string> columns = new List<string>();
columns = nameRow.Cells.Select(x => x.StringCellValue).ToList();
if (columns?.Count > 0)
{
for (rowIndex = 1; rowIndex <= sheet?.LastRowNum; rowIndex++)
{
var row = sheet?.GetRow(rowIndex);
if (row != null)
{
foreach (var col in columns)
{
ICell cell = row.GetCell(columns.IndexOf(col));
dic.Add(col, cell.StringCellValue);
}
}
}
}
}
}
}
catch (Exception)
{
}
return Task.FromResult(dic);
}
/// <summary>
/// 数据导入
/// </summary>
/// <param name="file"></param>
/// <returns></returns>
protected virtual Task DataImport(IFormFile file)
{
return Task.CompletedTask;
}
[NonAction]
protected async Task DoUpdate(WareHouseUpInput input)

View File

@@ -1,10 +1,12 @@
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DingTalk.Api.Request;
using JNPF.Common.Configuration;
using JNPF.Common.Contracts;
using JNPF.Common.Core.Manager;
using JNPF.Common.Extension;
using JNPF.Common.Helper;
@@ -68,7 +70,9 @@ namespace Tnb.WarehouseMgr
int row = 0;
try
{
List<Dictionary<string, string>> dics = WmsImportExcelToMemory(file);
//例1 获取所有表
List<Dictionary<string, string>> dics = WmsImport(file);
List<WmsCarryH> carrys = new List<WmsCarryH>();
var carryStdDic = await _db.Queryable<WmsCarrystd>().ToDictionaryAsync(x => x.carrystd_code, x => x.id);
List<string> locCodes = new();
@@ -112,14 +116,35 @@ namespace Tnb.WarehouseMgr
return row > 0;
}
public async Task DataImport<T>(IFormFile file,Func<List<T>> getColumns) where T : BaseEntity<string>, new()
{
var nonBeNullColumnList = new List<string>();
var entityInfo = _db.EntityMaintenance.GetEntityInfo<T>();
foreach (var column in entityInfo.Columns)
{
if (!column.IsNullable)
{
nonBeNullColumnList.Add(column.DbColumnName);
}
}
var importData = WmsImport(file); //ImportExcelToMemory<T> where T:BaseEntity<string>,new()
var fillColumns = nonBeNullColumnList.Except(importData.Keys).ToList();
if (fillColumns?.Count > 0)
{
}
}
/// <summary>
/// WMS导入
/// </summary>
/// <returns></returns>
private static List<Dictionary<string, string>> WmsImportExcelToMemory(IFormFile file)
private static Dictionary<string, string> WmsImport(IFormFile file)
{
int rowIndex = 1;
List<Dictionary<string, string>> dics = new List<Dictionary<string, string>>();
Dictionary<string, string> dic = new();
IWorkbook? workbook = null;
try
{
@@ -144,8 +169,6 @@ namespace Tnb.WarehouseMgr
font.Color = IndexedColors.Red.Index; // 将字体颜色设置为红色
style.SetFont(font);
var nameRow = sheet?.GetRow(0);
Dictionary<string, string> dic = new Dictionary<string, string>();
if (nameRow != null)
{
List<string> columns = new List<string>();
@@ -162,11 +185,11 @@ namespace Tnb.WarehouseMgr
ICell cell = row.GetCell(columns.IndexOf(col));
dic.Add(col, cell.StringCellValue);
}
dics.Add(dic);
}
}
}
}
}
}
catch (Exception)
@@ -175,7 +198,7 @@ namespace Tnb.WarehouseMgr
throw;
}
return dics;
return dic;
}
/// <summary>