加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 如何使用[Display(Name =“”)]作为LoadFromCollection的

发布时间:2020-12-15 18:12:29 所属栏目:百科 来源:网络整理
导读:在我的MVC 5站点中,我有一个稳定的导出到Excel功能,但用户要求我使列标题“用户友好”. 在我的模型中,我使用[Display(Name =“Friendly Column Name”)]注释,它们在View页面上按预期显示,但是当用户触发导出功能时它们不会转移到导出的Excel文件.相反,出现真
在我的MVC 5站点中,我有一个稳定的导出到Excel功能,但用户要求我使列标题“用户友好”.

在我的模型中,我使用[Display(Name =“Friendly Column Name”)]注释,它们在View页面上按预期显示,但是当用户触发导出功能时它们不会转移到导出的Excel文件.相反,出现真实姓名.

我知道我可以通过除LoadFromCollection之外的其他方法加载工作表单元格,但我想知道是否仍然可以使用LoadFromCollection并使用模型的Display(Name())注释.

这就是我目前拥有的:

public ActionResult ExportToExcel(string _selectedCampus)
    {
        // This is the query result set the user wishes to export to file.
        IEnumerable<Mia1aSec1FayExport> exportQuery = unitOfWork
            .Mia1aSec1FayRepository.Get().Select(n => new Mia1aSec1FayExport
        {
            Campus = n.Campus,StudentName = n.StudentName,CreditsBeforeSchoolYear = n.CreditsBeforeSchoolYear,CreditsDuringSemester1 = n.CreditsDuringSemester1,EligibleFayCount = n.EligibleFayCount,EligibleFayMeetingGoal = n.EligibleFayMeetingGoal
        }).Where(n => n.Campus == _selectedCampus).OrderBy(n => n.StudentName)
        .AsEnumerable();

        // The current iteration saves the table contents,but without the   
        // [Display{Name"xxx)] annotation from the model.

        byte[] response;   
        using (var excelFile = new ExcelPackage())
        {
            excelFile.Workbook.Properties.Title = "MIA 1A (i) Eligible FAY Students";                
            var worksheet = excelFile.Workbook.Worksheets.Add("Sheet1");                   
            worksheet.Cells["A1"]
                .LoadFromCollection(Collection: exportQuery,PrintHeaders: true);
            response = excelFile.GetAsByteArray();
        }

        // Save dialog appears through browser for user to save file as desired.
        return File(response,"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet","Mia1aSec1Fay.xlsx");
    }

一种解决方法是覆盖标题单元格,但这是不可取的,原因有两个:(1)我已经在模型中将列名称写为注释,这意味着我现在在两个不同的地方写了相同的信息,并且(2) )我必须手动保持信息同步,以防用户需要更多更改,我可能会忘记这样做.

// This loads teh table as seen by the user in the index view.
            worksheet.Cells["A1"].LoadFromCollection(Collection: exportQuery,PrintHeaders: true);

            // Workaround overwrite header cells,as I'm not able to use Display Name annotation.
            worksheet.Cells[1,1].Value = "Campus";
            worksheet.Cells[1,2].Value = "Student Name";
            worksheet.Cells[1,3].Value = "Credits Before SY";
            worksheet.Cells[1,4].Value = "Credits During Semester 1";
            worksheet.Cells[1,5].Value = "Legible Population";
            worksheet.Cells[1,6].Value = "Eligible Students Earning 2+ Credits";

在写这篇文章时,我真的希望这不是唯一的选择.必须有一种方法可以在标题中写入显示名称值而不是真实名称.

解决方法

LoadFromCollection仅响应DisplayName或Description属性,而不响应Display属性.

因此,您可以尝试将其中一个属性添加到当前属性中.

[DisplayName("Friendly Column Name")]
[Display(Name = "Friendly Column Name")]
public string StudentName { get; set; }

还要确保在调用LoadFromCollection时将PrintHeaders参数设置为true,但是您说真实的名称会出现,因此可能已经很好了.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读