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.
47 lines
1.7 KiB
47 lines
1.7 KiB
using Magicodes.ExporterAndImporter.Core.Extension;
|
|
using Magicodes.ExporterAndImporter.Core.Models;
|
|
using Magicodes.ExporterAndImporter.Excel;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using Wood.Entity;
|
|
using Wood.Service.SystemManage.Param;
|
|
|
|
namespace Wood.Service
|
|
{
|
|
/// <summary>
|
|
/// service基础类
|
|
/// </summary>
|
|
public class ApiService : ControllerBase
|
|
{
|
|
protected async Task<FileStreamResult> ExportFile<T>(ICollection<T> dtos,string fileName) where T : class, new()
|
|
{
|
|
var excelExporter = HttpContext.RequestServices.GetRequiredService<IExcelExporter>();
|
|
var res = await excelExporter.ExportAsByteArray(dtos);
|
|
return new FileStreamResult(new MemoryStream(res), "application/octet-stream") { FileDownloadName = DateTime.Now.ToString("yyyyMMddHHmm") +"_"+ fileName };
|
|
}
|
|
|
|
protected async Task<ImportResult<T>> ImportFile<T>(IFormFile file,List<ImportErrorDto> errors) where T : class, new()
|
|
{
|
|
var excelImporter = HttpContext.RequestServices.GetRequiredService<IExcelImporter>();
|
|
|
|
ImportResult<T> result = await excelImporter.Import<T>(file.OpenReadStream());
|
|
if (result.HasError)
|
|
{
|
|
foreach (var item in result.RowErrors)
|
|
errors.Add(new ImportErrorDto { Index = item.RowIndex, Errors = string.Join(';', item.FieldErrors.Values.ToArray()) });
|
|
foreach (var item in result.TemplateErrors)
|
|
errors.Add(new ImportErrorDto { Index = 0, Errors = item.RequireColumnName + "," + item.Message });
|
|
}
|
|
|
|
return result;
|
|
}
|
|
}
|
|
}
|
|
|