使用LinqToExcel导入数据 - 只读属性

时间:2014-10-25 16:39:27

标签: c# excel asp.net-mvc-4 linq-to-excel

我正在尝试使用LinqToExcel从excel导入数据。我在模型类中几乎没有readonly属性。当我尝试使用excel列映射它们时,它们会因以下错误而失败。此外,当我从excel中删除这些列时,它没有值就可以正常工作。

  

找不到方法“总计”。

型号:预算

    [Required]
    [Display(Name = "Room Type")]
    public int RoomTypeID { get; set; }
    [ForeignKey("RoomTypeID")]
    public virtual RoomType RoomType { get; set; }

    public decimal Pair { get; set; }
    [ExcelColumn("Cost per Room*")]
    public decimal CostPerRoom { get; set; }

    [NotMapped]
    [ExcelColumn("Total")]
    [Display(Name = "Total")]
    public decimal Total
    {
        get
        {
            if (this.RoomType != null)
            {
                return this.CostPerRoom * this.RoomType.RoomTypeQty * this.Pair;
            }
            else
            {
                return 0;
            }

        }
    }

预算控制器:

    public ActionResult ReadFromExcel()
    {
        var file = Request.Files[0];
        if (file != null && file.ContentLength > 0)
        {
            var fileName = Path.GetFileName(file.FileName);
            var path = Path.Combine(Server.MapPath("~/Uploads/"), fileName);
            file.SaveAs(path);


            var excel = new ExcelQueryFactory(path);
            excel.DatabaseEngine = DatabaseEngine.Ace;

            excel.TrimSpaces = LinqToExcel.Query.TrimSpacesType.Both;

            var budgets = from c in excel.Worksheet<Budget>("WorksheeName") select c;
            foreach (var item in budgets) // This is where it generates the error.
            {
            }
      }

我如何克服这个问题?

0 个答案:

没有答案