1

我正在尝试使用 LinqToExcel 从 excel 导入数据。我在模型类中几乎没有只读属性。当我尝试将它们与 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.
            {
            }
      }

我该如何克服呢?

4

0 回答 0