导入功能修改

This commit is contained in:
FanLian
2023-08-07 17:48:36 +08:00
parent b895b49f51
commit 96a99ed2dc
2 changed files with 68 additions and 137 deletions

View File

@@ -16,6 +16,7 @@ using JNPF.Common.Extension;
using JNPF.DataEncryption;
using JNPF.DependencyInjection;
using JNPF.DynamicApiController;
using JNPF.Extras.CollectiveOAuth.Enums;
using JNPF.FriendlyException;
using JNPF.Systems.Interfaces.System;
using JNPF.VisualDev;
@@ -92,15 +93,16 @@ namespace Tnb.WarehouseMgr
}
return Task.FromResult(isMatch);
}
/// <summary>
/// 解析Excel到内存
/// </summary>
/// <param name="file"></param>
/// <returns></returns>
protected Task<Dictionary<string, object>> ImportExcelToMemory(IFormFile file)
protected Task<List<Dictionary<string, object>>> ImportExcelToMemory(IFormFile file)
{
int rowIndex = 1;
Dictionary<string, object> dic = new();
List<Dictionary<string, object>> dics = new();
IWorkbook? workbook = null;
try
{
@@ -127,6 +129,7 @@ namespace Tnb.WarehouseMgr
var nameRow = sheet?.GetRow(0);
if (nameRow != null)
{
Dictionary<string, object> dic = new Dictionary<string, object>();
List<string> columns = new List<string>();
columns = nameRow.Cells.Select(x => x.StringCellValue).ToList();
if (columns?.Count > 0)
@@ -141,19 +144,57 @@ namespace Tnb.WarehouseMgr
ICell cell = row.GetCell(columns.IndexOf(col));
dic.Add(col, cell.StringCellValue);
}
dics.Add(dic);
}
}
}
}
}
}
catch (Exception)
{
throw;
}
return Task.FromResult(dic);
}
return Task.FromResult(dics);
}
/// <summary>
/// WMS导入模板
/// </summary>
/// <param name="dic"></param>
/// <param name="sheetName"></param>
/// <returns></returns>
protected dynamic WmsTemplate(Dictionary<string, string> dic, string sheetName)
{
XSSFWorkbook workbook = new XSSFWorkbook();
ISheet sheet = workbook.CreateSheet(sheetName);
IRow nRow = sheet.CreateRow(0);
IRow dRow = sheet.CreateRow(1);
dRow.Hidden = true;
dic.Remove("id");
dic.Remove("create_id");
dic.Remove("create_time");
//sheet.SetColumnHidden(1, true);
for (int i = 0; i < dic?.Count; i++)
{
ICell cell1 = nRow.CreateCell(i);
cell1.SetCellValue(dic.Values.GetIndex(i));
ICell cell2 = dRow.CreateCell(i);
cell2.SetCellValue(dic.Keys.GetIndex(i));
}
MemoryStream ms = new MemoryStream();
workbook.Write(ms);
MemoryStream ms2 = new MemoryStream(ms.ToArray());
ms2.Position = 0;
FileStreamResult fileStreamResult = new FileStreamResult(ms2, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") { FileDownloadName = "template.xlsx" };
return fileStreamResult;
}
/// <summary>
/// 数据导入
/// </summary>