Dto
Dto设置参考
[ExcelImporter(IsLabelingError = true)]
public class MyDto
{
[ImporterHeader(Name = "序号")]
[ExporterHeader(DisplayName = "序号")]
public int Id { get; set; }
}
Controller
生成模板
[HttpGet("GenerateTemplate")]
//[WrapResult]
[SwaggerOperation(summary: "生成故障配置导入模板", Tags = new[] { "云端-故障告警" })]
public async Task<ActionResult> GenerateExcelImportTemplate()
{
return await _service.GenerateExcelImportTemplate();
}
下载生成的模板返回数据时,不能使用WrapResult注解,否则不会生成下载链接
返回结果为ActionResult
导入数据
[HttpPost("ImportConfig")]
[WrapResult]
[SwaggerOperation(summary: "导入故障配置信息")]
public async Task ImportEventConfig(IFormFile formFile)
{
await _service.ImportEventConfig(formFile);
}
Service
导出模板
public async Task<ActionResult> GenerateExcelImportTemplate()
{
var gg = await Importer.GenerateTemplateBytes<MyDto>();
var stream = gg.ToStream();
FileStreamResult resultStream = new FileStreamResult(stream, "application/octet-stream")
{
FileDownloadName = "模板.xlsx"
};
return resultStream;
}
获取生成的模板的byte数组,并转换成Stream流,将Stream流作为结果返回
导入数据
public async Task ImportEventConfig(IFormFile formFile)
{
var fileStream = formFile.OpenReadStream();
var import = Importer.Import<MyDto>(fileStream);
var inputs = import.Result.Data.ToList();
var list = ObjectMapper.Map<List<EventConfigInput>, List<EventConfig>>(inputs);
await _myDtoRepository.Insert(list);
}
标签:vnext,Task,formFile,Abp,var,async,public,模板
From: https://www.cnblogs.com/kelvinxiong/p/17968133