C#行轉列

        /// <summary>
        /// 行轉列
        /// </summary>
        /// <param name="_outDataSource">數據源</param>
        /// <param name="categoryname">列名:行業</param>
        /// <param name="date">列名:日期(該列爲:行轉列)</param>
        /// <param name="zongshu">列名:條數</param>
        /// <returns></returns>
        private DataTable RCC(DataTable _outDataSource, string categoryname, string date, string zongshu)
        {
            //從DataTable中讀取不重複的日期行,用來構造新DataTable的列
            DataTable distinct_date = _outDataSource.DefaultView.ToTable(true, date);

            DataTable new_DataTable = new DataTable();

            //將行業列添加到新表中
            DataColumn new_d_col = new DataColumn();
            new_d_col.ColumnName = categoryname;
            new_d_col.Caption = "";
            new_DataTable.Columns.Add(new_d_col);

            StringBuilder str_sum = new StringBuilder();

            //開始在新表中構造日期列
            foreach (DataRow dr in distinct_date.Rows)
            {
                new_d_col = new DataColumn();
                new_d_col.DataType = typeof(decimal);
                new_d_col.ColumnName = dr[date].ToString();
                new_d_col.Caption = dr[date].ToString();
                new_d_col.DefaultValue = 0;
                new_DataTable.Columns.Add(new_d_col);

                //這個的目的是爲合計列構造expression   註釋 爲 可算合計
                // str_sum.Append("+[").Append(dr[date].ToString()).Append("]");

            }

            ////將合計列添加到新表中   註釋 爲 可算合計
            //new_d_col = new DataColumn();
            //new_d_col.DataType = typeof(decimal);
            //new_d_col.ColumnName = "Sum";
            //new_d_col.Caption = "合計";
            //new_d_col.DefaultValue = 0;
            //new_d_col.Expression = str_sum.ToString().Substring(1);
            //new_DataTable.Columns.Add(new_d_col);

            /*到此新表已經構建完畢,下面開始爲新表添加數據*/

            //從原DataTable中讀出不重複的行業,以行業爲關鍵字來構造新表的行
            DataTable distinct_object = _outDataSource.DefaultView.ToTable(true, categoryname);
            DataRow[] drs;
            DataRow new_dr;
            foreach (DataRow dr in distinct_object.Rows)
            {
                new_dr = new_DataTable.NewRow();
                new_dr[categoryname] = dr[categoryname].ToString();

                foreach (DataRow _dr in distinct_date.Rows)
                {
                    drs = _outDataSource.Select(categoryname + "='" + dr[categoryname].ToString() + "' and " + date + "='" + _dr[date].ToString() + "'");
                    if (drs.Length != 0)
                    {
                        new_dr[_dr[date].ToString()] = Math.Round(Convert.ToDecimal(drs[0][zongshu]), 2);
                    }
                }
                new_DataTable.Rows.Add(new_dr);
            }

            return new_DataTable;
        }

 

調用方法:express

  DataTable newTable = new DataTable();
            newTable.Columns.Add("行業", typeof(string));     //行業名稱
            newTable.Columns.Add("年份", typeof(string));             //年份
            newTable.Columns.Add("條數", typeof(string));    //條數


            int years = Convert.ToInt32(DateTime.Now.ToString("yyyy"));

            if (!string.IsNullOrEmpty(currentyear))
            {
                years = Convert.ToInt32(currentyear);
            }
            for (var i = years; i >= years - 9; i--)
            {
                DataTable dt = GetGroupIndustryByYear(i.ToString());

                if (dt != null && dt.Rows.Count > 0)
                {
                    foreach (DataRow dr in dt.Rows)
                    {
                        DataRow n_dr = newTable.NewRow();
                        n_dr["行業"] = Convert.ToString(dr["INDUSTRYCATEGORYNAME"]);
                        n_dr["年份"] = Convert.ToString(i);
                        n_dr["條數"] = Convert.ToString(dr["count"]);

                        newTable.Rows.Add(n_dr);
                    }
                }

            }


            DataTable newDT = RCC(newTable);
相關文章
相關標籤/搜索