将Excel数据导入提取到单独的类中
This commit is contained in:
@@ -93,111 +93,6 @@ namespace Tnb.WarehouseMgr
|
||||
}
|
||||
return Task.FromResult(isMatch);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 解析Excel到内存
|
||||
/// </summary>
|
||||
/// <param name="file"></param>
|
||||
/// <returns></returns>
|
||||
protected Task<List<Dictionary<string, object>>> ImportExcelToMemory(IFormFile file)
|
||||
{
|
||||
int rowIndex = 1;
|
||||
List<Dictionary<string, object>> dics = 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.Black.Index; // 将字体颜色设置为红色
|
||||
style.SetFont(font);
|
||||
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();
|
||||
columns.ForEach(x => {
|
||||
var strings =x.Split(new char[2] { '(', ')' });
|
||||
x = strings[1];
|
||||
});
|
||||
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);
|
||||
}
|
||||
dics.Add(dic);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
|
||||
throw;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
[NonAction]
|
||||
|
||||
Reference in New Issue
Block a user