对象启动时的减法

时间:2014-02-17 15:52:50

标签: c# export-to-excel

我有一个ViewModel,它代表一个电子表格,如下所示。

我循环遍历dataList,逐行/逐列填充其值。

如您所见,前六列是直接从数据列表中分配相应的值。

其余的,即Col1,Col2,Col3,Col3 ......等,如下所示计算:

foreach (var stockMaterial in materialDataList)
{
     var initialQuantity = stockMaterial.CurrentQuantity;
     var subtractedValues = GetSubtractedValues(stockMaterial.CurrentQuantity, stockMaterial.AvarageUsage,
     stockMaterial.QuantityReserved);


     exportedFile.Rows.Add(new StockForecastReportExport
     {
          MaterialName = stockMaterial.MaterialName,
          CurrentStockLevel = initialQuantity,
          OrderThreshold = stockMaterial.OrderThreshold,
          AvarageUsage = stockMaterial.AvarageUsage,
          QuantityReserved = stockMaterial.QuantityReserved,
          Col1 = stockMaterial.CurrentQuantity - subtractedValues,
          Col2 = stockMaterial.CurrentQuantity - subtractedValues - subtractedValues,
          Col3 = stockMaterial.CurrentQuantity - subtractedValues - subtractedValues - subtractedValues,
     });
}

当您看到特定于Material(一行)的currentQuantity时,对于下一个每列都会减少一个数字。至少有20列。

我想知道我能做些什么才能有效而整洁地做到这一点。当然,上述情况绝对不可接受!

请帮助提出想法。例如,可能循环遍历Col(x)属性并分配值。

这是我提到的视图模型:

 public class StockForecastReportExport
    {
        [ExcelCol("A")]
        public string MaterialName { get; set; }

        [ExcelCol("B")]
        public int CurrentStockLevel { get; set; }

        [ExcelCol("C")]
        public int OrderThreshold { get; set; }

        [ExcelCol("D")]
        public int AvarageUsage { get; set; }

        [ExcelCol("E")]
        public int QuantityReserved { get; set; }

        [ExcelCol("F")]
        public decimal Col1 { get; set; }

        [ExcelCol("G")]
        public decimal Col2 { get; set; }

        [ExcelCol("H")]
        public decimal Col3 { get; set; }

        [ExcelCol("I")]
        public decimal Col4 { get; set; }

        [ExcelCol("J")]
        public decimal Col5 { get; set; }

        [ExcelCol("K")]
        public decimal Col6 { get; set; }

        [ExcelCol("L")]
        public decimal Col7 { get; set; }

        [ExcelCol("M")]
        public decimal Col8 { get; set; }

        [ExcelCol("N")]
        public decimal Col9 { get; set; }

        [ExcelCol("O")]
        public decimal Col10 { get; set; }

        [ExcelCol("P")]
        public decimal Col11 { get; set; }

        [ExcelCol("Q")]
        public decimal Col12 { get; set; }

        [ExcelCol("R")]
        public decimal Col13 { get; set; }

        [ExcelCol("S")]
        public decimal Col14 { get; set; }

        [ExcelCol("T")]
        public decimal Col15 { get; set; }

        [ExcelCol("U")]
        public decimal Col16 { get; set; }

        [ExcelCol("V")]
        public decimal Col17 { get; set; }

        [ExcelCol("W")]
        public decimal Col18 { get; set; }

        [ExcelCol("X")]
        public decimal Col19 { get; set; }

        [ExcelCol("Y")]
        public decimal Col20 { get; set; }

    }

非常感谢提前。

2 个答案:

答案 0 :(得分:2)

如果您拥有视图模型的代码,我会更改它,以便ColX属性只有一个返回CurrentQuantity - (X * subtractedValues)的getter,其中subtractedValues是构造函数的必需值。

这样,您就不必设置所有列值。它们会自动计算。

它看起来像这样:

class StockForecastReportExport
{
    private decimal subtractedValue;

    public StockForecastReportExport(decimal subtractedValue)
    {
        this.subtractedValue = subtractedValue; 
    }

    [ExcelCol("F")]
    public decimal Col1 { get { return CurrentQuantity - (1 * subtractedValue); } }

    [ExcelCol("G")]
    public decimal Col2 { get { return CurrentQuantity - (2 * subtractedValue); } }

    [ExcelCol("H")]
    public decimal Col3 { get { return CurrentQuantity - (3 * subtractedValue); } }

    public decimal CurrentQuantity { get; set; }
}

当然,你可以坚持反思,但我不认为在这种情况下需要这样做。

答案 1 :(得分:1)

我建议在课堂内混淆计算。 添加constructor并传递SubtractedValues和CurrentQuantity,并使用乘法函数设置列:

请注意,此解决方案仍然只进行一次计算,并在实例化时将结果设置为所有ColN属性。

public class StockForecastReportExport
{
    public StockForecastReportExport(
        string materialName, 
        int currentStockLevel, 
        int orderThreshold, 
        int averageUsage, 
        int quantityReserved, 
        int currentQuantity, 
        int subtractedValues)
    {
        MaterialName = materialName;
        CurrentStockLevel = currentStockLevel;
        OrderThreshold = orderThreshold;
        AvarageUsage = averageUsage;
        QuantityReserved = quantityReserved;

        Func<int, int> multiplyFunc = mf => 
            currentQuantity - (subtractedValues * mf);

        // Calculate the rest
        Col1 = multiplyFunc(1);
        Col2 = multiplyFunc(2);
        Col3 = multiplyFunc(3);
        ...
    }

    [ExcelCol("A")]
    public string MaterialName { get; set; }

    ...
}

然后您的创作代码将更加优雅:

exportedFile.Rows.Add(
    new StockForecastReportExport
    (
        materialName: stockMaterial.MaterialName,
        currentStockLevel: initialQuantity,
        orderThreshold: stockMaterial.OrderThreshold,
        averageUsage: stockMaterial.AvarageUsage,
        quantityReserved: stockMaterial.QuantityReserved,
        currentQuantity: stockMaterial.CurrentQuantity,
        subtractedValues: subtractedValues
    );
);