如何在.Net Core MVC中将字符串数据导出为pdf格式

时间:2017-01-23 12:26:49

标签: asp.net

如何在.Net Core MVC中将字符串数据导出为pdf格式。请任何人帮助我在.net核心中以pdf格式导出数据。

1 个答案:

答案 0 :(得分:0)

您可以在OnResultExecuting期间使用Response并将Filter属性替换为在MemoryStream中存储结果HTML的内容。然后,您可以在OnResultExecuted期间清除响应,并将其替换为PDF转换的结果。我不确定这会比仅从URL获取HTML更好。

例如=

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WebApplication2.Models
{
    public class Person
    {
        public string Name { get; set; }
        public string Email { get; set; }
        public int Age { get; set; }
    }
}



 public ActionResult GetPersons()
        {
            List<Person> persons = new List<Person>();
            persons.Add(new Person() { Age = 29, Name = "Rami1", Email = "Rami1@abc.com" });
            persons.Add(new Person() { Age = 28, Name = "Rami2", Email = "Rami2@abc.com" });
            return View(persons);
        }

@model IEnumerable<WebApplication2.Models.Person>

@{
    ViewBag.Title = "GetPersons";
}

<h2>GetPersons</h2>

<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Name)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Email)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Age)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Name)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Email)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Age)
        </td>
    </tr>
}

</table>