Exemple #1
0
 public async Task ExportReceipt_Type_Test()
 {
     var tplPath = Path.Combine(Directory.GetCurrentDirectory(), "TestFiles", "ExportTemplates",
         "receipt.cshtml");
     var tpl = File.ReadAllText(tplPath);
     var exporter = new HtmlExporter();
     var filePath = Path.Combine(Directory.GetCurrentDirectory(), nameof(ExportReceipt_Test) + ".html");
     if (File.Exists(filePath)) File.Delete(filePath);
     //此处使用默认模板导出
     var result = await exporter.ExportBytesByTemplate(
         new ReceiptInfo
         {
             Amount = 22939.43M,
             Grade = "2019秋",
             IdNo = "43062619890622xxxx",
             Name = "张三",
             Payee = "湖南心莱信息科技有限公司",
             PaymentMethod = "微信支付",
             Profession = "运动训练",
             Remark = "学费",
             TradeStatus = "已完成",
             TradeTime = DateTime.Now,
             UppercaseAmount = "贰万贰仟玖佰叁拾玖圆肆角叁分",
             Code = "19071800001"
         }, tpl, typeof(ReceiptInfo));
     result.ShouldNotBeNull();
     using (var file = File.OpenWrite(filePath))
     {
         file.Write(result, 0, result.Length);
     }
     File.Exists(filePath).ShouldBeTrue();
 }
Exemple #2
0
        public async Task <bool> HandleSuccessfulReqeustAsync(HttpContext context, object body, Type type, string tplPath)
        {
            var    contentType = "";
            string filename    = DateTime.Now.ToString("yyyyMMddHHmmss");

            byte[] result = null;
            switch (context.Request.Headers["Magicodes-Type"])
            {
            case HttpContentMediaType.XLSXHttpContentMediaType:
                filename += ".xlsx";
                var dt = ToDataTable(body?.ToString());
                contentType = HttpContentMediaType.XLSXHttpContentMediaType;
                var exporter = new ExcelExporter();
                result = await exporter.ExportAsByteArray(dt, type);

                break;

            case HttpContentMediaType.PDFHttpContentMediaType:
                filename   += ".pdf";
                contentType = HttpContentMediaType.PDFHttpContentMediaType;
                IExportFileByTemplate pdfexporter = new PdfExporter();
                var tpl = await File.ReadAllTextAsync(tplPath);

                var obj = JsonConvert.DeserializeObject(body.ToString(), type);
                result = await pdfexporter.ExportBytesByTemplate(obj, tpl, type);

                break;

            case HttpContentMediaType.HTMLHttpContentMediaType:
                filename   += ".html";
                contentType = HttpContentMediaType.HTMLHttpContentMediaType;
                IExportFileByTemplate htmlexporter = new HtmlExporter();
                result = await htmlexporter.ExportBytesByTemplate(JsonConvert.DeserializeObject(body.ToString(), type), await File.ReadAllTextAsync(tplPath), type);

                break;

            case HttpContentMediaType.DOCXHttpContentMediaType:
                filename   += ".docx";
                contentType = HttpContentMediaType.DOCXHttpContentMediaType;
                IExportFileByTemplate docxexporter = new WordExporter();
                result = await docxexporter.ExportBytesByTemplate(JsonConvert.DeserializeObject(body.ToString(), type), await File.ReadAllTextAsync(tplPath), type);

                break;
            }
            if (contentType != "")
            {
                context.Response.Headers.Add("Content-Disposition", $"attachment;filename={filename}");
                context.Response.ContentType = contentType;
                if (result != null)
                {
                    await context.Response.Body.WriteAsync(result, 0, result.Length);
                }
            }
            else
            {
                return(false);
            }
            return(true);
        }
Exemple #3
0
        public async Task ExportHtmlBytesByTemplate_Test()
        {
            var tplPath  = Path.Combine(Directory.GetCurrentDirectory(), "TestFiles", "ExportTemplates", "tpl1.cshtml");
            var tpl      = File.ReadAllText(tplPath);
            var exporter = new HtmlExporter();
            var filePath = Path.Combine(Directory.GetCurrentDirectory(), nameof(ExportHtmlBytesByTemplate_Test) + ".html");

            if (File.Exists(filePath))
            {
                File.Delete(filePath);
            }
            //此处使用默认模板导出
            var result = await exporter.ExportBytesByTemplate(
                GenFu.GenFu.ListOf <ExportTestData>(), tpl);

            result.ShouldNotBeNull();
            using var file = File.OpenWrite(filePath);
            file.Write(result, 0, result.Length);
            File.Exists(filePath).ShouldBeTrue();
        }
Exemple #4
0
        public async Task HandleSuccessfulReqeustAsync(HttpContext context, object body, Type type, string tplPath)
        {
            var    contentType = "";
            string filename    = DateTime.Now.ToString("yyyyMMddHHmmss");

            byte[] result = null;
            switch (context.Request.ContentType)
            {
            case HttpContentMediaType.XLSXHttpContentMediaType:
                filename += ".xlsx";
                var dt = ToDataTable(body?.ToString());
                contentType = HttpContentMediaType.XLSXHttpContentMediaType;
                var exporter = new ExcelExporter();
                result = await exporter.ExportAsByteArray(dt, type);

                break;

            case HttpContentMediaType.PDFHttpContentMediaType:
                filename   += ".pdf";
                contentType = HttpContentMediaType.PDFHttpContentMediaType;
                IExportFileByTemplate pdfexporter = new PdfExporter();
                tplPath = Path.Combine(Directory.GetCurrentDirectory(), "ExportTemplates",
                                       "batchReceipt.cshtml");
                var tpl = File.ReadAllText(tplPath);
                var obj = JsonConvert.DeserializeObject(body.ToString(), type);
                result = await pdfexporter.ExportBytesByTemplate(obj, tpl, type);

                break;

            case HttpContentMediaType.HTMLHttpContentMediaType:
                filename   += ".html";
                contentType = HttpContentMediaType.HTMLHttpContentMediaType;
                IExportFileByTemplate htmlexporter = new HtmlExporter();
                result = await htmlexporter.ExportBytesByTemplate(JsonConvert.DeserializeObject(body.ToString(), type), File.ReadAllText(Path.Combine(Directory.GetCurrentDirectory(), "ExportTemplates",
                                                                                                                                                      "receipt.cshtml")), type);

                break;

            case HttpContentMediaType.DOCXHttpContentMediaType:
                filename += ".docx";
                IExportFileByTemplate docxexporter = new WordExporter();
                result = await docxexporter.ExportBytesByTemplate(JsonConvert.DeserializeObject(body.ToString(), type), File.ReadAllText(Path.Combine(Directory.GetCurrentDirectory(), "ExportTemplates",
                                                                                                                                                      "receipt.cshtml")), type);

                break;

            default:
                break;
            }

            #region  excel
            //var dt = ToDataTable(body?.ToString());
            ////Excel
            //IExporter exporter = new ExcelExporter();
            //var result = await exporter.ExportAsByteArray(dt, type);
            //context.Response.Headers.Add("Content-Disposition", "attachment;filename=test.xlsx");
            //context.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet; charset=UTF-8";
            #endregion

            #region PDF
            //IExportFileByTemplate exporter = new PdfExporter();
            //var tplPath = Path.Combine(Directory.GetCurrentDirectory(), "ExportTemplates",
            //    "batchReceipt.cshtml");
            //var tpl = File.ReadAllText(tplPath);
            //var obj = JsonConvert.DeserializeObject(body.ToString(), type);
            //var result = await exporter.ExportBytesByTemplate(obj, tpl, type);
            //context.Response.Headers.Add("Content-Disposition", "attachment;filename=test.pdf");
            //context.Response.ContentType = "application/pdf; charset=UTF-8";
            #endregion

            #region HTML
            //IExportFileByTemplate exporter = new HtmlExporter();
            //var tplPath = Path.Combine(Directory.GetCurrentDirectory(), "ExportTemplates",
            //    "receipt.cshtml");
            //var tpl = File.ReadAllText(tplPath);
            //var obj = JsonConvert.DeserializeObject(body.ToString(), type);
            //var result = await exporter.ExportBytesByTemplate(obj, tpl, type);
            //context.Response.Headers.Add("Content-Disposition", "attachment;filename=test.html");
            //context.Response.ContentType = "text/html; charset=UTF-8";
            #endregion

            #region Word
            //IExportFileByTemplate exporter = new WordExporter();
            //var tplPath = Path.Combine(Directory.GetCurrentDirectory(), "ExportTemplates",
            //    "receipt.cshtml");
            //var tpl = File.ReadAllText(tplPath);
            //var obj = JsonConvert.DeserializeObject(body.ToString(), type);
            //var result = await exporter.ExportBytesByTemplate(obj, tpl, type);
            //context.Response.Headers.Add("Content-Disposition", "attachment;filename=test.docx");
            //context.Response.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";

            #endregion

            context.Response.Headers.Add("Content-Disposition", $"attachment;filename={filename}");
            context.Response.ContentType = contentType;
            await context.Response.Body.WriteAsync(result, 0, result.Length);
        }