stevenjin 2025-01-24 16:50 采纳率: 96.8%
浏览 5
已结题

用Magicodes.IE.Pdf导出数据列表

用Magicodes.IE.Pdf导出数据列表,原代码不是一个集合,被我改为list之后报错,求正确方法。
报错如下:



Microsoft.CSharp.RuntimeBinder.RuntimeBinderException:“Cannot implicitly convert type 'Magicodes.ExporterAndImporter.Core.Models.ExportDocumentInfo<System.Collections.Generic.List<WebApplication1.Person>>' to 'System.Collections.IEnumerable'. An explicit conversion exists (are you missing a cast?)”
 



 public async Task<FileResult> Export()
 {
     try
     {   
         Person resumeDescription1 = 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"
         };
 
         Person resumeDescription2 = 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"
         };
         List<Person> people = new List<Person>();
         people.Add(resumeDescription1);
         people.Add(resumeDescription2);
         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,
          people, 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");
     }
 }
 
 
@using WebApplication1
 
<!DOCTYPE html>
<html>
@model List<Person>
<head>
    <meta charset='UTF-8'>
    <title></title>
    <style>
        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;
            }
    </style>
</head>
<body>
    <table class='m_table'>
        <tr>
            <th colspan='8' style='text-align:left;font-size:18px;'>个人信息</th>
        </tr>
        @foreach(var person in Model)
        {
            <tr>
                <td> 姓名:</td>
                <td> @person.Name </td>
                <td> 性别: </td>
                <td> @person.Gender </td>
                <!-- 其他属性类似 -->
            </tr>
        }              
    </table>
</body>
</html>
 
    [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; } 
    }

  • 写回答

4条回答 默认 最新

  • 檀越@新空间 2025-01-24 16:56
    关注

    下午好🌅🌅🌅
    本答案参考通义千问

    从错误信息来看,问题在于 ExportByTemplate 方法期望一个 IEnumerable 类型作为参数,而你传入的是 ExportDocumentInfo<List<Person>> 类型。为了解决这个问题,你需要确保传递给 ExportByTemplate 的参数是符合其签名要求的类型。

    根据你的代码和描述,你已经将 Person 实例放入了一个 List<Person> 中。接下来,我们需要确保在调用 ExportByTemplate 方法时,传入的参数类型是正确的。以下是修正后的代码:

    修改后的代码

    public async Task<FileResult> Export()
    {
        try
        {
            Person resumeDescription1 = 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"
            };
    
            Person resumeDescription2 = new Person
            {
                Id = 2,
                Name = "李四",
                Gender = "女",
                Birthday = Convert.ToDateTime("1996-06-23"),
                Mobile = "19999999999",
                University = "北京大学",
                Education = "博士研究生",
                Major = "物理",
                Nation = "汉族",
                MarriageStaus = "未婚",
                Occupation = "实验室研究员",
                FreshStudent = "否",
                VisitDate = Convert.ToDateTime("2018-08-02"),
                CurrentCompanyName = "中国科学院",
                Resignation = "想换个更有挑战的工作",
                Profile = "擅长实验设计与数据分析",
                Evaluate = "Python是世界上最强大的脚本语言",
                FurImg = $"{_webHostEnvironment.WebRootPath}\\Images\\001.jpg",
                AnalysisReport = $"{_webHostEnvironment.WebRootPath}\\Images\\002.jpg"
            };
    
            List<Person> people = new List<Person>();
            people.Add(resumeDescription1);
            people.Add(resumeDescription2);
    
            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 = $"{Directory.GetCurrentDirectory()}\\{fileName}";
    
            var result = await exporter.ExportByTemplate(downloadName, people, 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");
        }
    }
    

    关键点解释

    1. 创建 Person 对象:确保每个 Person 对象具有唯一的 Id,以便它们可以区分。
    2. 创建 List<Person> 集合:将多个 Person 对象添加到 List<Person> 中。
    3. 传递 List<Person>ExportByTemplate 方法:确保你传递的是一个集合(List<Person>),而不是单个对象或者其他的类型。

    问题解决步骤

    1. 检查 ExportByTemplate 方法签名:确保你传递的参数类型是该方法期望的类型。
    2. 确保 peopleIEnumerable 类型:在你的代码中,people 已经是 List<Person> 类型,而 List<T> 实现了 IEnumerable<T> 接口,所以这一步是自动满足的。
    3. 调试和验证:运行代码并验证是否解决了错误。

    希望这些修改能帮助你解决问题。如果还有其他问题,请随时告诉我。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 5月17日
  • 已采纳回答 5月9日
  • 修改了问题 1月24日
  • 创建了问题 1月24日