简体   繁体   English

ASP.Net MVC将WebGrid导出为PDF

[英]ASP.Net MVC Exporting WebGrid to PDF

I am trying to export a WebGrid to PDF and I have the following code in my controller: 我正在尝试将WebGrid导出为PDF并且我的控制器中包含以下代码:

public FileStreamResult CreatePdf()
{
    List<CertificationListViewModel> all = new List<CertificationListViewModel>();

    using (DBEntities db = new DBEntities())
    {
        all = db.Certifications.ToList();
    }

    WebGrid grid = new WebGrid(source: all, canPage: false, canSort: false);

    string gridHtml = grid.GetHtml(
        columns: grid.Columns(
            grid.Column("Name", "Nome"),
            grid.Column("Classification", "Classificação"),
            grid.Column("Date", "Data de Realização") 
        )
    ).ToString();
    string exportData = String.Format("<html><head>{0}</head><body>{1}</body></html>", "<style>table{ border-spacing: 10px; border-collapse: separate; }</style>", gridHtml);
    var bytes = System.Text.Encoding.UTF8.GetBytes(exportData);
    using (var input = new MemoryStream(bytes))
    {
        var output = new MemoryStream();
        var document = new iTextSharp.text.Document(PageSize.A4, 50, 50, 50, 50);
        var writer = PdfWriter.GetInstance(document, output);
        writer.CloseStream = false;
        document.Open();

        var xmlWorker = iTextSharp.tool.xml.XMLWorkerHelper.GetInstance();
        xmlWorker.ParseXHtml(writer, document, input, System.Text.Encoding.UTF8);
        document.Close();
        output.Position = 0;
        return new FileStreamResult(output, "application/pdf");
    }
}

The code is giving me a error in this line: 代码在这一行给我一个错误:

all = db.Certifications.ToList();

This is the error that shows up: 这是显示的错误:

Cannot implicitly convert type 'System.Collections.Generic.List<HIQTraining.Model.Certification>' to 'System.Collections.Generic.List<HIQTrainingSite.ViewModel.CertificationListViewModel>' 无法将类型'System.Collections.Generic.List <HIQTraining.Model.Certification>'隐式转换为'System.Collections.Generic.List <HIQTrainingSite.ViewModel.CertificationListViewModel>'

I am not sure what the error is if someone has an ideia of what it could be I would appreciate ! 我不确定如果有人对它可能有什么想法有什么错误,我将不胜感激!

PS : I have the following iTextSharp libraries exported in the project: PS:我在项目中导出了以下iTextSharp库:

using iTextSharp.text;
using iTextSharp.text.pdf;

The error is telling you that your values are of different types. 错误告诉您您的值属于不同类型。 (Which makes sense, I wouldn't expect "view" models to be stored in the database.) So you'll need to in some way convert from one to the other. (这很有意义,我不希望将“视图”模型存储在数据库中。)因此,您需要以某种方式从一种转换为另一种。

For example, if your view model can be simply built with an object initializer, then you can construct it in a .Select() projection: 例如,如果可以使用对象初始化程序简单地构建视图模型,则可以在.Select()投影中构造它:

all = db.Certifications.ToList()
      .Select(c => new CertificationListViewModel {
          SomeProperty = c.SomeProperty,
          AnotherProperty = c.AnotherProperty
      });

If there are many properties, libraries like AutoMapper can help with that. 如果有许多属性,则诸如AutoMapper之类的库可以帮助您解决此问题。

Conversely, if you actually wanted the list of database models, then you'd need to change the type of your variable to match that: 相反,如果您实际上想要数据库模型列表,则需要更改变量的类型以使其匹配:

List<Certification> all = new List<Certification>();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM