abp-vnext-pro 实战(七,导出excel)
HttpApi项目,vben28\nswag\refresh.bat 更新前端接口
[HttpPost("export")] [SwaggerOperation(summary: "导出客户列表", Tags = new[] { "Customers" })] [ProducesResponseType(typeof(FileContentResult), (int)HttpStatusCode.OK)] public Task<ActionResult> ExportAsync(PageCustomerInput input) { return _customerAppService.ExportAsync(input); }
application.Contract 项目
public interface ICustomerAppService : IApplicationService { 。。。 Task<ActionResult> ExportAsync(PageCustomerInput input); }
public class ExportCustomerOutput { [ExporterHeader(DisplayName = "客户编号")] public string Code { get; set; } [ExporterHeader(DisplayName = "客户名称")] public string Name { get; set; } }
application 项目
/// <summary> /// 导出列表 /// </summary> [Authorize(BasicManagementPermissions.SystemManagement.UserExport)] public async Task<ActionResult> ExportAsync(PageCustomerInput input) { var list = await _customerManager.GetListAsync(input.PageSize, input.SkipCount, input.Filter); var result = ObjectMapper.Map<List<CustomerDto>, List<ExportCustomerOutput>>(list); var bytes = await _excelExporter.ExportAsByteArray<ExportCustomerOutput>(result); return new XlsxFileResult(bytes: bytes, fileDownloadName: $"导出列表{Clock.Now:yyyyMMdd}"); }
public ERPApplicationAutoMapperProfile() { CreateMap<CustomerDto, ExportCustomerOutput>(); }
前端页面 index.ts
const [openFullLoading, closeFullLoading] = useLoading({ tip: 'Loading...', }); export function exportAsync({ request }) { openFullLoading(); const customerServiceProxy = new CustomersServiceProxy(); customerServiceProxy.export(request).then((res) => { const a = document.createElement('a'); a.href = URL.createObjectURL(res.data); a.download = '用户列表导出.xlsx'; a.click(); closeFullLoading(); }); }
index.vue
<script lang="ts"> ... import { tableColumns, searchFormSchema, pageAsync, deleteAsync,exportAsync } from './Index'; const handleExport = () => { const { getFieldsValue } = getForm(); let request = getFieldsValue(); exportAsync({ request }); };