小计的总和而不是使用excel互操作小计计数记录

时间:2013-04-11 15:07:23

标签: c# excel

所以我有一个持有数据的类,一个字典可以提取所有7列信息并将其放入excel中。我的问题是,当我使用我的工作簿调用小计函数时,它会计算数据的行/实例,而不是总计列,特别是我创建的实际小计。

class:
public class DataRow
    {
        public string ItemID { get; set; }
        public string ItemDes { get; set; }
        public string UOM { get; set; }
        public DateTime? DateRec { get; set; }
        public string RecBy { get; set; }
        public Decimal Cost { get; set; }
        public int? Qty { get; set; }

        public DataRow(string item, string itemd, string uom, DateTime? dateRec, string recBy, Decimal cost, int? qty)
        {
            this.ItemID = item;
            this.ItemDes = itemd;
            this.UOM = uom;
            this.DateRec = dateRec;
            this.RecBy = recBy;
            this.Cost = cost;
            this.Qty = qty;
        }

    }

var rec = ...
    join...
    join...
    where...


Dictionary<string, List<DataRow>> data = new Dictionary<string, List<DataRow>>();


foreach (var r in rec){
    string itemid = r.POItem.Items.itm_ext_id.Trim();
    DataRow dr = new DataRow(itemid, r.POItem.Items.itm_ds.Trim(),  r.POItem.UnitOfMeasure.cod_dtl_ds.Trim(), r.pch_rcv_ts, r.lst_mod_id.Trim(),   r.POItem.ord_prc_at, r.pch_rcv_qy);  

    if (data.ContainsKey(itemid))
    {
        data[itemid].Add(dr);
    }
    else
    {
        data.Add(itemid, new List<DataRow> { dr });
    }

}

int i = 1;
foreach (KeyValuePair<string,List<DataRow>> kvp in data.OrderBy(kvp => kvp.Key))
{

    foreach(DataRow dr in kvp.Value)
    {
        workbook.AddCell(i, 0, kvp.Key);
        workbook.AddCell(i, 1, dr.ItemDes);
        workbook.AddCell(i, 2, dr.UOM);

        if(dr.DateRec.HasValue)
            workbook.AddCell(i, 3, String.Format("{0:MM/dd/yyyy}", dr.DateRec));

        workbook.AddCell(i, 4, dr.RecBy);
        workbook.AddCell(i, 5, dr.Cost);

        if(dr.Qty > 0)
            workbook.AddCell(i++, 6, dr.Qty);
    }

}

int[] grpColumns = { 7 };
workbook.SubTotal(1, 7, 1, grpColumns);

0 个答案:

没有答案