BaseWarehouse类,新增 ImportExcelToMemory,DataImport函数

This commit is contained in:
alex
2023-08-07 16:30:07 +08:00
parent 6602a4d7d2
commit af9265ea20
3 changed files with 129 additions and 7 deletions

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)