/// <summary> /// 根据模板导出列表 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="fileName"></param> /// <param name="dataItems"></param> /// <param name="htmlTemplate"></param> /// <returns></returns> public async Task <ExportFileInfo> ExportListByTemplate <T>(string fileName, ICollection <T> dataItems, string htmlTemplate = null) where T : class { if (string.IsNullOrWhiteSpace(fileName)) { throw new ArgumentException(Resource.FileNameMustBeFilled, nameof(fileName)); } var exporter = new HtmlExporter(); var htmlString = await exporter.ExportListByTemplate(dataItems, htmlTemplate); using (var generatedDocument = new MemoryStream()) { using (var package = WordprocessingDocument.Create(generatedDocument, WordprocessingDocumentType.Document)) { var mainPart = package.MainDocumentPart; if (mainPart == null) { mainPart = package.AddMainDocumentPart(); new Document(new Body()).Save(mainPart); } var converter = new HtmlConverter(mainPart); converter.ParseHtml(htmlString); mainPart.Document.Save(); } File.WriteAllBytes(fileName, generatedDocument.ToArray()); var fileInfo = new ExportFileInfo(fileName, "application/vnd.openxmlformats-officedocument.wordprocessingml.document"); return(fileInfo); } }
/// <summary> /// 导出Pdf /// </summary> /// <param name="data"></param> /// <param name="pdfExporterAttribute"></param> /// <param name="template"></param> /// <returns></returns> public async Task <byte[]> ExportListBytesByTemplate <T>(ICollection <T> data, PdfExporterAttribute pdfExporterAttribute, string template) where T : class { var exporter = new HtmlExporter(); var htmlString = await exporter.ExportListByTemplate(data, template); return(await ExportPdf(pdfExporterAttribute, htmlString)); }
/// <summary> /// 根据模板导出bytes /// </summary> /// <typeparam name="T"></typeparam> /// <param name="data"></param> /// <param name="template"></param> /// <returns></returns> public async Task <byte[]> ExportListBytesByTemplate <T>(ICollection <T> data, string template) where T : class { var exporter = new HtmlExporter(); var htmlString = await exporter.ExportListByTemplate(data, template); using (var generatedDocument = new MemoryStream()) { using (var package = WordprocessingDocument.Create(generatedDocument, WordprocessingDocumentType.Document)) { var mainPart = package.MainDocumentPart; if (mainPart == null) { mainPart = package.AddMainDocumentPart(); new Document(new Body()).Save(mainPart); } var converter = new HtmlConverter(mainPart); converter.ParseHtml(htmlString); mainPart.Document.Save(); byte[] byteArray = Encoding.UTF8.GetBytes(htmlString); using (var stream = new MemoryStream(byteArray)) { mainPart.FeedData(stream); } } return(generatedDocument.ToArray()); } }
public async Task ExportHtml_Test() { var exporter = new HtmlExporter(); var filePath = Path.Combine(Directory.GetCurrentDirectory(), nameof(ExportHtml_Test) + ".html"); if (File.Exists(filePath)) File.Delete(filePath); //此处使用默认模板导出 var result = await exporter.ExportListByTemplate(filePath, GenFu.GenFu.ListOf<ExportTestData>()); result.ShouldNotBeNull(); File.Exists(filePath).ShouldBeTrue(); }
public async Task ExportHtmlByTemplate_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(ExportHtmlByTemplate_Test) + ".html"); if (File.Exists(filePath)) { File.Delete(filePath); } //此处使用默认模板导出 var result = await exporter.ExportListByTemplate(filePath, GenFu.GenFu.ListOf <ExportTestData>(), tpl); result.ShouldNotBeNull(); File.Exists(filePath).ShouldBeTrue(); }
/// <summary> /// 根据模板导出列表 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="fileName"></param> /// <param name="dataItems"></param> /// <param name="htmlTemplate"></param> /// <returns></returns> public async Task <ExportFileInfo> ExportListByTemplate <T>(string fileName, ICollection <T> dataItems, string htmlTemplate = null) where T : class { if (string.IsNullOrWhiteSpace(fileName)) { throw new ArgumentException("文件名必须填写!", nameof(fileName)); } var exporterAttribute = GetExporterAttribute <T>(); var exporter = new HtmlExporter(); var htmlString = await exporter.ExportListByTemplate(dataItems, htmlTemplate); if (exporterAttribute.IsWriteHtml) { File.WriteAllText(fileName + ".html", htmlString); } return(await ExportPdf(fileName, exporterAttribute, htmlString)); }
/// <summary> /// 根据模板导出列表 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="fileName"></param> /// <param name="dataItems"></param> /// <param name="htmlTemplate"></param> /// <returns></returns> public async Task <TemplateFileInfo> ExportListByTemplate <T>(string fileName, ICollection <T> dataItems, string htmlTemplate = null) where T : class { if (string.IsNullOrWhiteSpace(fileName)) { throw new ArgumentException("文件名必须填写!", nameof(fileName)); } var exporter = new HtmlExporter(); var htmlString = await exporter.ExportListByTemplate(dataItems, htmlTemplate); var converter = new BasicConverter(new PdfTools()); var doc = new HtmlToPdfDocument { GlobalSettings = { ColorMode = ColorMode.Color, Orientation = Orientation.Landscape, PaperSize = PaperKind.A4, Out = fileName, }, Objects = { new ObjectSettings { PagesCount = true, HtmlContent = htmlString, WebSettings = { DefaultEncoding = "utf-8" }, HeaderSettings ={ FontSize =9, Right = "[page]/[toPage]", Line = true, Spacing = 2.812 }, Encoding = System.Text.Encoding.UTF8 } } }; converter.Convert(doc); var fileInfo = new TemplateFileInfo(fileName, "application/pdf"); return(fileInfo); }
/// <summary> /// 根据模板导出列表 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="fileName"></param> /// <param name="dataItems"></param> /// <param name="htmlTemplate"></param> /// <returns></returns> public async Task <TemplateFileInfo> ExportListByTemplate <T>(string fileName, ICollection <T> dataItems, string htmlTemplate = null) where T : class { if (string.IsNullOrWhiteSpace(fileName)) { throw new ArgumentException("文件名必须填写!", nameof(fileName)); } var exporterAttribute = GetExporterAttribute <T>(); var exporter = new HtmlExporter(); var htmlString = await exporter.ExportListByTemplate(dataItems, htmlTemplate); if (exporterAttribute.IsWriteHtml) { File.WriteAllText(fileName + ".html", htmlString); } var doc = GetHtmlToPdfDocumentByExporterAttribute(fileName, exporterAttribute, htmlString); PdfConverter.Convert(doc); var fileInfo = new TemplateFileInfo(fileName, "application/pdf"); return(fileInfo); }