You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

64 lines
2.3 KiB

using Magicodes.ExporterAndImporter.Excel;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Text;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using TaskManager.Entity;
using TaskManager.EntityFramework.Repository;
using TaskManager.EntityFramework;
using Wood.Service.Controllers;
using Magicodes.ExporterAndImporter.Core.Models;
namespace Wood.Service.Datas
{
public class SupplierEmployeeDtService : NormalBaseController<SUPPLIER_EMPLOYEE_DT>
{
public SupplierEmployeeDtService(JobDbContext context, IServiceProvider builder, IConfiguration configuration, IRepository<SUPPLIER_EMPLOYEE_DT> repository) : base(context, builder, configuration, repository)
{
}
[HttpPost]
[Route("import")]
public override async Task<IActionResult> Import(IFormFile file)
{
if (file == null || file.Length == 0)
{
return NotFound();
}
MemoryStream memStream = new MemoryStream();
await file.CopyToAsync(memStream);
var importer = new ExcelImporter();
ImportResult<SUPPLIER_EMPLOYEE_DT> impResult = await importer.Import<SUPPLIER_EMPLOYEE_DT>(memStream);
if (impResult.HasError)
{
StringBuilder sb = new StringBuilder();
foreach (var rowErr in impResult.RowErrors)
{
string temp = string.Join(";", rowErr.FieldErrors.Select(itm => $"第{rowErr.RowIndex}行:{itm.Key}-{itm.Value}"));
sb.AppendLine(temp);
}
foreach (var templateErr in impResult.TemplateErrors)
{
string temp = $"列名:{templateErr.RequireColumnName},错误信息:{templateErr.Message}";
sb.AppendLine(temp);
}
throw new Exception(sb.ToString());
}
foreach (var item in impResult.Data) //??增加批量插入
{
await Create(item);
}
return Ok(true);
}
}
}