动态生成 表格笔记

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data;
using System.Reflection;
using System.Collections;

namespace Rhythmk.Cnblogs
{
    //  http://t.qq.com/wangkyx   rhythmk  cnblogs

    public partial class Index : System.Web.UI.Page
    {
        public string HTMLTB = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            List<EExamanalysisreport> list = new List<EExamanalysisreport>();
            EExamanalysisreport entity;
            entity = new EExamanalysisreport() { Level = 1, PointCode1 = "0101", PointCode1Name = "0101Name", PointCode2 = "010101", PointCode2Name = "010101Name", PointCode3 = "01010101", PointCode3Name = "01010101Name", PointCode4 = "0101010101", PointCode4Name = "0101010101Name" };
            list.Add(entity);

            entity = new EExamanalysisreport() { Level = 4, PointCode1 = "0101", PointCode1Name = "0101Name", PointCode2 = "010101", PointCode2Name = "010101Name", PointCode3 = "01010101", PointCode3Name = "01010101Name", PointCode4 = "0101010102", PointCode4Name = "0101010102Name" };
            list.Add(entity);
            entity = new EExamanalysisreport() { Level = 4, PointCode1 = "0101", PointCode1Name = "0101Name", PointCode2 = "010101", PointCode2Name = "010101Name", PointCode3 = "01010102", PointCode3Name = "01010102Name", PointCode4 = "0101010204", PointCode4Name = "0101010204Name" };
            list.Add(entity);
            entity = new EExamanalysisreport() { Level = 4, PointCode1 = "0101", PointCode1Name = "0101Name", PointCode2 = "010101", PointCode2Name = "010202Name", PointCode3 = "01020203", PointCode3Name = "01020203Name", PointCode4 = "0102020304", PointCode4Name = "0102020304Name" };
            list.Add(entity);
            entity = new EExamanalysisreport() { Level = 4, PointCode1 = "0101", PointCode1Name = "0101Name", PointCode2 = "010102", PointCode2Name = "010102Name", PointCode3 = "01010205", PointCode3Name = "01010205Name", PointCode4 = "0101020501", PointCode4Name = "0101020501Name" };
            list.Add(entity);
            entity = new EExamanalysisreport() { Level = 4, PointCode1 = "0201", PointCode1Name = "0201Name", PointCode2 = "020101", PointCode2Name = "020101Name", PointCode3 = "02010101", PointCode3Name = "02010101Name", PointCode4 = "0201010101", PointCode4Name = "02010101Name" };
            list.Add(entity);
            TableEngine tb = new TableEngine(list);
           HTMLTB=tb.Show().ToString();

        }
    }
    //  http://t.qq.com/wangkyx   rhythmk  cnblogs
    public class TableEngine
    {
        private List<EExamanalysisreport> _dataList;
        private StringBuilder sb;
        private DataTable dt;
        /// <summary>
        /// 单元格
        /// </summary>
        private List<TCell> _celllist;

        /// <summary>
        /// 行大小
        /// </summary>
        private int _Max_X; //  行大小

        /// <summary>
        /// 列大小
        /// </summary>
        private int _Max_Y;


        public TableEngine(List<EExamanalysisreport> datalist)
        {
            this._dataList = datalist.Distinct().ToList();
            _celllist = new List<TCell>();
            sb = new StringBuilder();
            dt = new DataTable();
        }

        //  http://t.qq.com/wangkyx   rhythmk  cnblogs
        /// <summary>
        /// 初始化数据
        /// </summary>
        private void Init()
        {
            dt = ToDataTable(_dataList);
            _Max_X = _dataList.Max(x => x.Level);
            _Max_Y = _dataList.Count();
            List<Tuple<string, string, int>> pointArray = new List<Tuple<string, string, int>>();
            pointArray.AddRange(_dataList.Select(X => new Tuple<string, string, int>(X.PointCode1, X.PointCode1+"Name", 1)));
            pointArray.AddRange(_dataList.Select(X => new Tuple<string, string, int>(X.PointCode2, X.PointCode2+"Name", 2)));
            pointArray.AddRange(_dataList.Select(X => new Tuple<string, string, int>(X.PointCode3, X.PointCode3+"Name", 3)));
            pointArray.AddRange(_dataList.Select(X => new Tuple<string, string, int>(X.PointCode4, X.PointCode4+"Name", 4)));
            pointArray = pointArray.OrderBy(x => x.Item1).Distinct().ToList();
            TCell cell;
            for (int i = 0; i <  _Max_X ; i++)
            {
                var PointList = pointArray.Where(x => x.Item3 == (i + 1)).Distinct().OrderBy(x => x.Item1).ToList();

                foreach (var p in PointList)
                {
                    int rowCount = ChildItemCount(i + 1, p.Item1);
                    for (int j = 0; j < rowCount; j++)
                    {
                        cell = new TCell();
                        cell.X = i;// 知识点级别
                        cell.Y = _celllist.Where(x => x.X == i).Count() ;
                        cell.PointCode = p.Item1;
                        cell.PointName = p.Item2;
                        cell.YItem = 0;
                        if (j == 0) cell.YItem = rowCount;
                        _celllist.Add(cell);
                    }
                }
            }
        }


        private int ChildItemCount(int level, string pointcode)
        {
            return dt.Select(string.Format("PointCode{0}='{1}'", level, pointcode)).Length;
        }


        public StringBuilder Show()
        {
            if (_dataList == null || _dataList.Count < 1)
                return sb;
            Init();
            sb.Append("<table class=\"auto-style3\" border=\"1\">");
            StringBuilder sbRow;
            for (int i = 0; i < _Max_Y; i++)
            {
                sbRow = new StringBuilder();

                for (int j = 0; j < _Max_X; j++)
                {
                    TCell tcell = _celllist.Where(x => x.X == j && x.Y == i).FirstOrDefault();
                    if (tcell != null)
                    {
                        int minY = _celllist.Where(x =>x.PointCode==tcell.PointCode).Min(x=>x.Y);
                        if (minY!=tcell.Y)
                            continue;
                       
                        sbRow.Append("<td  x=\""+j.ToString()+"\" y=\""+i.ToString()+"\"");
                        if (tcell.YItem > 1)
                        {
                            sbRow.Append(" rowspan=\"" + tcell.YItem.ToString() + "\" ");

                        }
                        sbRow.Append(">" + tcell.PointName);
                        sbRow.Append("</td>");
                    }
                }

                if (sbRow.Length > 1)
                {
                    sb.Append("<tr>" + sbRow.ToString() + "</tr>");
                }

            }
            sb.Append("</table>");

            return sb;
        }


        /// <summary>
        /// 将集合类转换成DataTable
        /// </summary>
        /// <param name="list">集合</param>
        /// <returns></returns>
        private static DataTable ToDataTable(IList list)
        {
            DataTable result = new DataTable();
            if (list.Count > 0)
            {
                PropertyInfo[] propertys = list[0].GetType().GetProperties();
                foreach (PropertyInfo pi in propertys)
                {
                    result.Columns.Add(pi.Name, pi.PropertyType);
                }

                for (int i = 0; i < list.Count; i++)
                {
                    ArrayList tempList = new ArrayList();
                    foreach (PropertyInfo pi in propertys)
                    {
                        object obj = pi.GetValue(list[i], null);
                        tempList.Add(obj);
                    }
                    object[] array = tempList.ToArray();
                    result.LoadDataRow(array, true);
                }
            }
            return result;
        }

        public class TCell
        {
            public string PointCode { get; set; }
            public string PointName { get; set; }
            public int Level { get; set; }
            public int X { get; set; }
            public int Y { get; set; }
            public int YItem { get; set; }
        }
    }


    public class EExamanalysisreport
    {
        /// <summary>
        /// 获取或设置Id
        /// </summary>
        public long Id { get; set; }

        /// <summary>
        /// 获取或设置ExaminationID
        /// </summary>
        public long ExaminationID { get; set; }

        /// <summary>
        /// 获取或设置PointCode1
        /// </summary>
        public string PointCode1 { get; set; }

        /// <summary>
        /// 获取或设置PointCode1Name
        /// </summary>
        public string PointCode1Name { get; set; }

        /// <summary>
        /// 获取或设置PointCode2
        /// </summary>
        public string PointCode2 { get; set; }

        /// <summary>
        /// 获取或设置PointCode2Name
        /// </summary>
        public string PointCode2Name { get; set; }

        /// <summary>
        /// 获取或设置PointCode3
        /// </summary>
        public string PointCode3 { get; set; }

        /// <summary>
        /// 获取或设置PointCode3Name
        /// </summary>
        public string PointCode3Name { get; set; }

        /// <summary>
        /// 获取或设置PointCode4
        /// </summary>
        public string PointCode4 { get; set; }

        /// <summary>
        /// 获取或设置PointCode4Name
        /// </summary>
        public string PointCode4Name { get; set; }

        /// <summary>
        /// 获取或设置Score
        /// </summary>
        public int Score { get; set; }

        /// <summary>
        /// 获取或设置Percent
        /// </summary>
        public double Percent { get; set; }

        /// <summary>
        /// 等级
        /// </summary>
        public int Level { get; set; }

    }

}
posted @ 2012-11-22 22:39  Rhythmk  阅读(225)  评论(0编辑  收藏  举报
Rhythmk 个人笔记