Epplus:使用LoadFromCollection时,导出的Excel文件中的布尔列为空

时间:2015-12-14 18:41:21

标签: c# excel epplus

在我的MVC应用程序中,我有一个ActionResult,它加载IEnumerable并将其导出到Excel文件。除非我有一个布尔列,否则那部分效果很好。在这种情况下,布尔列的值为空。

LoadFromCollection不支持布尔列吗?

要解决此问题,我可以在模型中创建一个单独的字段,将布尔值转换为字符串值。但是,我希望我可以在ExcelPackage()块中添加一个声明来完成工作,而无需调整模型。这是可能的,还是重写模型唯一可能的解决方案?

public ActionResult ExportToExcel(string _searchString, string _setScale)
{                
   // This is the query result set the user wishes to export to file
   IEnumerable<CreditsAllsExport> exportQuery;
   /* ... irrelevant code redacted ... */
   exportQuery = exportQuery.AsEnumerable();

   byte[] response;
   using (var excelFile = new ExcelPackage())
   {
      /* ... irrelevant code redacted ... */
      worksheet.Cells["A5"].LoadFromCollection(Collection: exportQuery, PrintHeaders: true);

      /* TRANSLATE BOOLEAN TO Yes or No */

      response = excelFile.GetAsByteArray();
  }

  /* ... irrelevant code redacted ... */
}

2 个答案:

答案 0 :(得分:1)

这完全正常......

xmlTags:8[NoResponseOK]  
xmlTags:9[NoResponseOK, TestIndicator]  
xmlTags:10[NoResponseOK, TestIndicator, PrimaryObjectType]  
xmlTags:11[NoResponseOK, TestIndicator, PrimaryObjectType, ChangeSubType]  
xmlTags:12[NoResponseOK, TestIndicator, PrimaryObjectType, ChangeSubType, ChangeTC]  
xmlTags:13[NoResponseOK, TestIndicator, PrimaryObjectType, ChangeSubType, ChangeTC, TranContentCode]  
xmlTags:14[NoResponseOK, TestIndicator, PrimaryObjectType, ChangeSubType, ChangeTC, TranContentCode, ChangeSubType]  
xmlTags:15[NoResponseOK, TestIndicator, PrimaryObjectType, ChangeSubType, ChangeTC, TranContentCode, ChangeSubType, ChangeTC]  
xmlTags:16[NoResponseOK, TestIndicator, PrimaryObjectType, ChangeSubType, ChangeTC, TranContentCode, ChangeSubType, ChangeTC, TranContentCode]

答案 1 :(得分:0)

这很令人尴尬,但事实证明我的专栏没有出现在结果中,因为我没有将它包含在我的选择陈述中。

正如@Izydrmr所示,LoadFromCollection支持布尔值 。因此,主要观点是如果布尔列没有出现,那么请检查select语句。

IEnumerable<CreditsAllsExport> exportQuery =
   from s in db.CreditsAlls
   select new CreditsAllsExport
   {
      FiscalYear = s.FiscalYear,
      Campus = s.Campus,
      StudentName = s.StudentName,
      CourseID = s.CourseID,
      CourseTitle = s.CourseTitle,
      CreditEarned = s.CreditEarned,
      DateEarned = s.DateEarned,
      Department = s.Department,
      School = s.School,
      Teacher = s.Teacher,
      /* I had forgotten this column*/ Transfer=s.Transfer,  
      CampusSchoolMatch = s.CampusSchoolMatch
  };