Magicodes.IE.AspNetCore之一行代码多格式导出
主要步驟
1.安裝包
Install-Package Magicodes.IE.AspNetCore2.開始配置
在Startup.cs的Configure()方法中,在UseRouting()中間件之后,注冊(cè)如下中間件
public void Configure(IApplicationBuilder app) {app.UseRouting();app.UseMagiCodesIE();app.UseEndpoints(endpoints =>{endpoints.MapControllers();}); }上面這種以中間件形式可以為我們提供導(dǎo)出服務(wù),那么我們?cè)倏匆幌铝硪环N方式如下所示:
public void ConfigureServices(IServiceCollection services){services.AddControllers(options=>options.Filters.Add(typeof(MagicodesFilter)));}上面兩種方式都可以為我們提供導(dǎo)出服務(wù),我們只需要對(duì)我們的控制器進(jìn)行配置我們的特性,在這邊呢 特性主要做的是一個(gè)標(biāo)識(shí)作用,標(biāo)識(shí)他的一些相關(guān)的內(nèi)容數(shù)據(jù),同時(shí)標(biāo)識(shí)他可以當(dāng)成文件導(dǎo)出。
[HttpGet("excel")] [Magicodes(Type = typeof(ExportTestDataWithAttrs))] public List<ExportTestDataWithAttrs> Excel() {return GenFu.GenFu.ListOf<ExportTestDataWithAttrs>(100); }上面代碼片段中我們標(biāo)識(shí)這個(gè)類允許被導(dǎo)出。同時(shí)我們需要通過Type指定我們被導(dǎo)出類的類型。
這樣填寫完后我們可以通過對(duì)該地址的調(diào)用,但是注意我們必須要添加請(qǐng)求頭以標(biāo)識(shí)被導(dǎo)出的文件類型。如果不添加請(qǐng)求頭,那么此處將返回的還是json格式的數(shù)據(jù)。請(qǐng)求頭名稱為Magicodes-Type
/// <summary>/// XLSX/// </summary>internal const string XLSXHttpContentMediaType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";/// <summary>/// PDF/// </summary>internal const string PDFHttpContentMediaType = "application/pdf";/// <summary>/// DOCX/// </summary>internal const string DOCXHttpContentMediaType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";/// <summary>/// HTML/// </summary>internal const string HTMLHttpContentMediaType = "text/html";如果說是模板導(dǎo)出word或者pdf甚至說html文件那么我們也是同樣的操作如下所示:
[HttpGet("Word")][Magicodes(Type = typeof(ReceiptInfo), TemplatePath = ".//ExportTemplates//receipt.cshtml")]public ReceiptInfo Word(){return new ReceiptInfo{Amount = 22939.43M,Grade = "2019秋",IdNo = "43062619890622xxxx",Name = "張三",Payee = "湖南心萊信息科技有限公司",PaymentMethod = "微信支付",Profession = "運(yùn)動(dòng)訓(xùn)練",Remark = "學(xué)費(fèi)",TradeStatus = "已完成",TradeTime = DateTime.Now,UppercaseAmount = "貳萬貳仟玖佰叁拾玖圓肆角叁分",Code = "19071800001"};}我們還是需要對(duì)其指定Type,然后通過TemplatePath進(jìn)行指定模板地址即可
同樣的我們還可以通過請(qǐng)求頭進(jìn)行標(biāo)識(shí)本次請(qǐng)求是否是文件格式導(dǎo)出。
[HttpGet("pdf")][Magicodes(Type = typeof(BatchPortraitReceiptInfoInput), TemplatePath = ".//ExportTemplates//batchReceipt.cshtml")]public BatchPortraitReceiptInfoInput Pdf(){var input = new BatchPortraitReceiptInfoInput{Payee = "湖南心萊信息科技有限公司",SealUrl =@"data:image/jpeg;base64....",LogoUrl =@"data:image/png;base64....",ReceiptInfoInputs = new List<BatchPortraitReceiptInfoDto>()};for (var i = 0; i < 500; i++)input.ReceiptInfoInputs.Add(new BatchPortraitReceiptInfoDto{Amount = 22939.43M,Grade = "2019秋",IdNo = "43062619890622xxxx",Name = "張三",PaymentMethod = "微信支付",Profession = "運(yùn)動(dòng)訓(xùn)練",Remark = "學(xué)費(fèi)",TradeStatus = "已完成",TradeTime = DateTime.Now,UppercaseAmount = "貳萬貳仟玖佰叁拾玖圓肆角叁分",Code = "1907180000" + i});return input;}[HttpGet("Html")][Magicodes(Type = typeof(ReceiptInfo), TemplatePath = ".//ExportTemplates//receipt.cshtml")]public ReceiptInfo Html(){return new ReceiptInfo{Amount = 22939.43M,Grade = "2019秋",IdNo = "43062619890622xxxx",Name = "張三",Payee = "湖南心萊信息科技有限公司",PaymentMethod = "微信支付",Profession = "運(yùn)動(dòng)訓(xùn)練",Remark = "學(xué)費(fèi)",TradeStatus = "已完成",TradeTime = DateTime.Now,UppercaseAmount = "貳萬貳仟玖佰叁拾玖圓肆角叁分",Code = "19071800001"};}Swagger中使用
通過繼承IOperationFilter接口,創(chuàng)建AddRequiredHeaderParameter類,添加一個(gè)header類型的參數(shù),并且Header Name為Magicodes-Type如下所示:
public class AddRequiredHeaderParameter : IOperationFilter{public void Apply(OpenApiOperation operation, OperationFilterContext context){if (operation.Parameters == null){operation.Parameters = new List<OpenApiParameter>();}operation.Parameters.Add(new OpenApiParameter{Name = "Magicodes-Type",In = ParameterLocation.Header,Required = false,Description = "根據(jù)HttpContentMediaType添加指定的header值,導(dǎo)出不同格式的文件。"});}}然后轉(zhuǎn)到ConfigureServices()方法中,在AddSwaggerGen方法中添加如下內(nèi)容:
c.OperationFilter<AddRequiredHeaderParameter>();Reference
https://github.com/dotnetcore/Magicodes.IE
轉(zhuǎn)載是一種動(dòng)力 分享是一種美德
如果喜歡作者的文章,請(qǐng)關(guān)注【麥扣聊技術(shù)】訂閱號(hào)以便第一時(shí)間獲得最新內(nèi)容。本文版權(quán)歸作者和湖南心萊信息科技有限公司共有,歡迎轉(zhuǎn)載,但未經(jīng)作者同意必須保留此段聲明,且在文章頁(yè)面明顯位置給出原文連接,否則保留追究法律責(zé)任的權(quán)利。
文檔官網(wǎng):docs.xin-lai.com
原文作者:【HueiFeng】
QQ群:
編程交流群<85318032>?
產(chǎn)品交流群<897857351>
總結(jié)
以上是生活随笔為你收集整理的Magicodes.IE.AspNetCore之一行代码多格式导出的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 等待十年,史上第一个 64 位版 Vis
- 下一篇: NET问答: 如何避免在 EmptyEn