DataTable 列转行

接上篇

JSON to DataTable_Free Labor 的专栏-CSDN博客1. Json fileappsettings.json "UploadData": [ { "item_a": "", "item_b": "", "item_c": "", "d1": "Jan-22", "d2": "Jan-22", "d3": "Jan-22", "d4": "Jan-22", "d5": "Jan-22", "d6": "Feb-22", https://blog.csdn.net/wish366/article/details/123202278?spm=1001.2014.3001.5501已经获得一个DataTable.

 由于列数不定,并且有规律,每个月下都有W1-W5,需要按月把列转为行。

1. Class RecruitPlan and RecruitPlanCosts

    public class RecruitPlan
    {
        public string site { get; set; }
        public string plant { get; set; }
        public string batchid { get; set; }
        public string item_a { get; set; }
        public string item_b { get; set; }
        public string item_c { get; set; }
        public string period { get; set; }
        public string w1 { get; set; }
        public string w2 { get; set; }
        public string w3 { get; set; }
        public string w4 { get; set; }
        public string w5 { get; set; }
        public string uuser { get; set; }
        public DateTime udate { get; set; }
        public string chkrst { get; set; }
    }

    public class RecruitPlanCosts
    {
        public string site { get; set; }
        public string plant { get; set; }
        public string type { get; set; }
        public string item_a { get; set; }
        public string item_b { get; set; }
        public string item_c { get; set; }
        public string period_type { get; set; }
        public string period { get; set; }
        public decimal value { get; set; }
        public string period_from { get; set; }
        public string period_to { get; set; }
        public string month { get; set; }
        public string week { get; set; }
        public string cuser { get; set; }
        public DateTime cdate { get; set; }
        public string muser { get; set; }
        public DateTime mdate { get; set; }
    }

2. 列转行

string site = "*";
            string plant = "*";
            string batchid = Guid.NewGuid().ToString();
            List<RecruitPlan> r3Plans = new List<RecruitPlan>();
            List<RecruitPlanCosts> r3PlanCosts = new List<RecruitPlanCosts>();

            for (int i = 2; i < dt.Rows.Count; i++)
            {
                var row = dt.Rows[i];

                for (int j = 0; j < (dt.Columns.Count - 3) / 5 + (dt.Columns.Count - 3) % 5; j++)
                {
                    string month = DateTime.Parse(dt.Rows[0]["d" + (j * 5 + 1)].ToString()).ToString("yyyyMM");

                    RecruitPlan r3Plan = new RecruitPlan();
                    r3Plan.site = site;
                    r3Plan.plant = plant;
                    r3Plan.batchid = batchid;
                    r3Plan.item_a = row["item_a"].ToString();
                    r3Plan.item_b = row["item_b"].ToString();
                    r3Plan.item_c = row["item_c"].ToString();
                    r3Plan.period = month;
                    r3Plan.w1 = row["d" + (j * 5 + 1)].ToString();
                    r3Plan.w2 = row["d" + (j * 5 + 2)].ToString();
                    r3Plan.w3 = row["d" + (j * 5 + 3)].ToString();
                    r3Plan.w4 = row["d" + (j * 5 + 4)].ToString();
                    r3Plan.w5 = row["d" + (j * 5 + 5)].ToString();
                    r3Plan.uuser = "";
                    // use DB, r3Plan.udate = DateTime.Now;
                    r3Plan.chkrst = "";
                    r3Plans.Add(r3Plan);
                }

                for (int j = 3; j < dt.Columns.Count; j++)
                {
                    string date = dt.Rows[0]["d" + (j - 2)].ToString();
                    string year = DateTime.Parse(date).ToString("yyyy");
                    string month = DateTime.Parse(date).ToString("MM");
                    string week = dt.Rows[1]["d" + (j-2)].ToString().Trim();

                    RecruitPlanCosts r3PlanCost = new RecruitPlanCosts();
                    r3PlanCost.site = site;
                    r3PlanCost.plant = plant;
                    r3PlanCost.type = "plan";
                    r3PlanCost.item_a = row["item_a"].ToString();
                    r3PlanCost.item_b = row["item_b"].ToString();
                    r3PlanCost.item_c = row["item_c"].ToString();
                    r3PlanCost.period_type = "weekly";
                    r3PlanCost.period = year + month + "-" + week;
                    r3PlanCost.value = Convert.ToDecimal(row[j]);
                    r3PlanCost.period_from = "TBD";
                    r3PlanCost.period_to = "TBD";
                    r3PlanCost.month = month;
                    r3PlanCost.week = week;
                    r3PlanCost.cuser = "";
                    r3PlanCost.muser = "";

                    r3PlanCosts.Add(r3PlanCost);
                }
            }

猜你喜欢

转载自blog.csdn.net/wish366/article/details/123202676
今日推荐