工欲善其事必先利其器,一个好的方法能促使我们快速的完成一项工作。废话不多说,继续上干货,今天的编码是用.NET Core 导出一个PDF文件,我们这里借用的是【张三的简历】(张三:你们能不能别拿我举例了),将【张三的简历】导出,网页预览样式如下:
可以看出今天的张三同学也是一个程序员,而且是知名的C#程序员,个人简历也是幽默和谐又不失文雅
-------------------------------------------------分割线-正文开始----------------------------------------------------------
1、创建预览页
新建一个.NET Core MVC项目,如图所示:
在model文件夹新建Person类,代码如下;
using Magicodes.ExporterAndimporter.Core;
using System;
namespace WebMVC.Models
{
[Exporter(Name = "个人简历")]
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public string Gender { get; set; }
public DateTime Birthday { get; set; }
public string Mobile { get; set; }
public string University { get; set; }
public string Education { get; set; }
public string Major { get; set; }
public string Nation { get; set; }
public string MarriageStaus { get; set; }
public string Occupation { get; set; }
public string FreshStudent { get; set; }
public DateTime VisitDate { get; set; }
public string CurrentCompanyName { get; set; }
public string Resignation { get; set; }
public string Profile { get; set; }
public string evaluate { get; set; }
public string FurImg { get; set; }
public string AnalysisReport { get; set; }
}
}
HomeController控制器下index方法添加如下代码,设置默认数据:
public IActionResult Index()
{
Person resumeDescription = new Person();
resumeDescription.Id = 1;
resumeDescription.Name = "张三";
resumeDescription.Gender = "男";
resumeDescription.Birthday = Convert.ToDateTime("1995-05-22");
resumeDescription.Mobile = "18888888888";
resumeDescription.University = "华中科技大学";
resumeDescription.Education = "硕士研究生";
resumeDescription.Major = "计算机科学技术";
resumeDescription.Nation = "汉族";
resumeDescription.MarriageStaus = "已婚";
resumeDescription.Occupation = "砖厂搬砖";
resumeDescription.FreshStudent = "否";
resumeDescription.VisitDate = Convert.ToDateTime("2017-07-01");
resumeDescription.CurrentCompanyName = "王多鱼谁有梦想投资有限公司";
resumeDescription.Resignation = "梦想都实现了,现想找一份端茶调水双休五险一金不是996的软件公司";
resumeDescription.Profile = "本人精通Word、Excel、Access、Power Point、Outlook Express等软件的安装与卸载 熟练掌握VB、C++、FoxPro、SQL、java 等单词的拼写熟悉 Win7、WindowsPhone、Linux、Mac、Android、IOS、Blackberry等操作系统的开关机!";
resumeDescription.evaluate = "C#是世界上最好的语言";
resumeDescription.FurImg = $"\Images\001.jpg";
resumeDescription.AnalysisReport = $"\Images\002.jpg";
return View(resumeDescription);
}
View视图Home/Index.cshtml编写如下代码:
@{
Layout = null;
}
@model WebMVC.Models.ResumeDescription
body {
margin: 0;
padding: 0;
min-width: 1080px;
}
.m_table {
border-collapse: separate;
margin: 20px auto 0;
min-width: 1900px;
text-align: center;
font: 500 17px '微软雅黑';
border-spacing: 0;
border: 1px solid #EBEEF5;
}
.m_table th {
background-color: #F7F3F7;
}
.m_table th,
.m_table td {
border-right: 1px solid #EBEEF5;
border-bottom: 1px solid #EBEEF5;
padding: 5px;
height: 60px;
width: 60px;
}
1、导出PDF
HomeController控制器下Export方法添加如下代码,进行文件下载:
public async TaskExport() { try { Person resumeDescription = new Person { Id = 1, Name = "张三", Gender = "男", Birthday = Convert.ToDateTime("1995-05-22"), Mobile = "18888888888", University = "华中科技大学", Education = "硕士研究生", Major = "计算机科学技术", Nation = "汉族", MarriageStaus = "已婚", Occupation = "砖厂搬砖", FreshStudent = "否", VisitDate = Convert.ToDateTime("2017-07-01"), CurrentCompanyName = "王多鱼谁有梦想投资有限公司", Resignation = "梦想都实现了,现想找一份端茶调水双休五险一金不是996的软件公司", Profile = "本人精通Word、Excel、Access、Power Point、Outlook Express等软件的安装与卸载 熟练掌握VB、C++、FoxPro、SQL、java 等单词的拼写熟悉 Win7、WindowsPhone、Linux、Mac、Android、IOS、Blackberry等操作系统的开关机!", evaluate = "C#是世界上最好的语言", FurImg = $"{_webHostEnvironment.WebRootPath}\Images\001.jpg", AnalysisReport =$"{_webHostEnvironment.WebRootPath}\Images\002.jpg" }; var tplPath = Path.Combine(Directory.GetCurrentDirectory(), "Template", "PdfTemplate.cshtml"); var tpl = System.IO.File.ReadAllText(tplPath); var exporter = new PdfExporter(); string fileName = $"{Guid.NewGuid().ToString()}.pdf"; string downloadName = $"{Path.Combine(Directory.GetCurrentDirectory())}\{fileName}"; var result = await exporter.ExportByTemplate(downloadName, resumeDescription, tpl); //因为生成的模板默认在当前项目下,需要移动到wwwroot System.IO.File.Move(result.FileName, $"{_webHostEnvironment.WebRootPath}\Download\{fileName}"); string addrUrl = $"{_webHostEnvironment.WebRootPath}\Download\{fileName}"; var stream = System.IO.File.OpenRead(addrUrl); return File(stream, result.FileType, Path.GetFileName(addrUrl)); } catch (Exception ex) { var stream = System.IO.File.OpenRead(null); return File(stream, "application/vnd.android.package-archive"); } }
当点击预览页导出按钮时,自动导出一个PDF文件。
附件下载地址请点击:C#.NETCoreMVC导出PDF-C#文档类资源-CSDN文库



