通过TableItem(类结构表格)打印输出HTML格式的表格

2017-07-17 15:34:38  访问(1422) 赞(0) 踩(0)


  • 有几个代码:
    1、几个cs文件,定义Table、Row和Cell
    2、css样式
    3、调用实现方法

  • a.DefaultHref {
        color: #0000FF;
        text-decoration: none;
    }
    
    a.DefaultHref:link {
        color: #0000FF;
        text-decoration: none;
    }
    
    a.DefaultHref:hover {
        color: #0000FF;
        text-decoration: underline;
    }
    
    a.DefaultHref:visited {
        color: #0000FF;
        text-decoration: none;
    }
    
    
    a.DefaultRedHref {
        color: #FF0000;
        text-decoration: none;
    }
    
        a.DefaultRedHref:link {
            color: #FF0000;
            text-decoration: none;
        }
    
        a.DefaultRedHref:hover {
            color: #FF0000;
            text-decoration: underline;
        }
    
        a.DefaultRedHref:visited {
            color: #FF0000;
            text-decoration: none;
        }
    
    /* 默认的表格画线效果 */
    table.DL {
        border-collapse: collapse;
        border-spacing: 0;
        border-left: 1px solid #888;
        border-top: 1px solid #888;
        background: #efefef;
        width: 100%;
    }
    
    th.DL, td.DL, td.DLA {
        border-right: 1px solid #888;
        border-bottom: 1px solid #888;
        padding: 5px 15px;
        cursor: pointer;
    }
    
    th.DL {
        font-weight: bold;
        background: #ccc;
    }
    
    td.DLA {
        background: #ffffff;
    }
    
    table.tableItem {
        background-color: #CDCDCD;
        font-family: 仿宋;
    }
    
    tr.trItem {
        background-color: #FFFFFF;
    }
    
    td.tdTitleItem {
        background-color: #FFFFFF;
        font-size: 18px;
        font-weight: bold;
        font-family: 宋体;
        line-height: 20pt;
        vertical-align: middle;
        line-height: 20pt;
        text-align: center;
    }
    
    td.tdLeftTitleItem {
        background-color: #FFFFFF;
        font-size: 18px;
        font-weight: bold;
        font-family: 宋体;
        line-height: 20pt;
        vertical-align: middle;
        line-height: 20pt;
        text-align: left;
        padding-left: 5px;
    }
    
    td.tdMainItem {
        background-color: #FFFFFF;
        font-size: 14px;
        font-family: 宋体;
        line-height: 20pt;
        vertical-align: middle;
        line-height: 20pt;
        padding-left: 5px;
    }
    
    td.tdMainRedItem {
        background-color: #FFFFFF;
        font-size: 14px;
        font-family: 宋体;
        line-height: 20pt;
        vertical-align: middle;
        line-height: 20pt;
        padding-left: 5px;
        color: Red;
    }
    
    td.tdMainLeftItem {
        background-color: #FFFFFF;
        font-size: 14px;
        font-family: 宋体;
        line-height: 20pt;
        vertical-align: middle;
        line-height: 20pt;
        padding-left: 5px;
        text-align: left;
    }
    
    td.tdMainTitle {
        background-color: #FFFFFF;
        font-size: 14px;
        font-family: 宋体;
        line-height: 20pt;
        vertical-align: middle;
        line-height: 20pt;
        padding-left: 5px;
        font-weight: bold;
    }
    
    
  • using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace eKing.EkPageCreate.Classes.TableClasses
    {
        /// <summary>
        /// 
        /// </summary>
        [Serializable]
        public class TableItem
        {
            /// <summary>
            /// 
            /// </summary>
            public TableItem()
            {
    
            }
    
            /// <summary>
            /// 
            /// </summary>
            protected int m_ColNumber = 0;
    
            /// <summary>
            /// 
            /// </summary>
            public int ColNumber
            {
                get
                {
                    return m_ColNumber;
                }
                set
                {
                    m_ColNumber = value;
                }
            }
    
            /// <summary>
            /// 行记录
            /// </summary>
            protected List<RowItem> m_ListItem = null;
    
            /// <summary>
            /// 
            /// </summary>
            public List<RowItem> ListItem
            {
                get
                {
                    return m_ListItem;
                }
                set
                {
                    m_ListItem = value;
                }
            }
    
            /// <summary>
            /// 
            /// </summary>
            /// <returns></returns>
            public string ToHtml()
            {
    
                StringBuilder theResult = new StringBuilder();
    
                List<RowItem> rowList = ListItem;
                List<CellItem> cellList = null;
                string cssClassName = null;
                string addHtml = "";
                int idx = 0;
                foreach (RowItem row in rowList)
                {
                    if (row == null)
                        continue;
    
                    idx = 0;
                    theResult.Append(@"<tr class=""trItem"">");
    
                    cellList = row.ListItem;
    
                    foreach (CellItem cell in cellList)
                    {
                        switch (cell.EmCellStyleX)
                        {
                            case CellItem.EmCellStyle.内容:
                            default:
                                cssClassName = "tdMainItem";
                                break;
                            case CellItem.EmCellStyle.标题_居左:
                                cssClassName = "tdLeftTitleItem";
                                break;
                            case CellItem.EmCellStyle.标题_居中:
                                cssClassName = "tdTitleItem";
                                break;
                            case CellItem.EmCellStyle.子标题_居中:
                                cssClassName = "tdMainTitle";
                                break;
                            case CellItem.EmCellStyle.内容_红:
                                cssClassName = "tdMainRedItem";
                                break;
                            case CellItem.EmCellStyle.内容_居左:
                                cssClassName = "tdMainLeftItem";
                                break;
                        }
     
    
                        ++idx;
    
                        if (cell.ColSpan == 1)
                            addHtml = "";
                        else if (cell.ColSpan == 0)
                        {
                            addHtml = " colspan=\"" + (ColNumber - idx + 1).ToString() + "\"";
                        }
                        else
                        {
                            addHtml = " colspan=\"" + cell.ColSpan.ToString() + "\"";
                        }
    
                        theResult.Append(@"<td" + addHtml + @" class=""" + cssClassName + @""" align=""center"" height=""30px"">
                        " + cell.TheText + @"
                    </td>");
    
                    }
    
                    theResult.Append("</tr>");
                }
                 
    
                return @"
    <!DOCTYPE html PUBLIC ""-//W3C//DTD XHTML 1.0 Transitional//EN"" ""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"">
    <html xmlns=""http://www.w3.org/1999/xhtml"">
    <head><title>
    
    </title>
        <style>
            table.tableItem
            {
                background-color: #CDCDCD;
                font-family: 仿宋;
            }
            tr.trItem
            {
                background-color: #FFFFFF;
            }
            td.tdTitleItem
            {
                background-color: #FFFFFF;
                font-size: 18px;
                font-weight: bold;
                font-family: 宋体;
                line-height: 20pt;
                vertical-align: middle;
                line-height: 20pt;
                text-align: center;
            }
            td.tdLeftTitleItem
            {
                background-color: #FFFFFF;
                font-size: 18px;
                font-weight: bold;
                font-family: 宋体;
                line-height: 20pt;
                vertical-align: middle;
                line-height: 20pt; 
                text-align: left;
                padding-left:5px;
            }
            td.tdMainItem
            {
                background-color: #FFFFFF;
                font-size: 14px;
                font-family: 宋体;
                line-height: 20pt;
                vertical-align: middle;
                line-height: 20pt;
                padding-left: 5px;
            }
            td.tdMainRedItem
            {
                background-color: #FFFFFF;
                font-size: 14px;
                font-family: 宋体;
                line-height: 20pt;
                vertical-align: middle;
                line-height: 20pt;
                padding-left: 5px;
                color:Red;
            }
            td.tdMainLeftItem
            {
                background-color: #FFFFFF;
                font-size: 14px;
                font-family: 宋体;
                line-height: 20pt;
                vertical-align: middle;
                line-height: 20pt;
                padding-left: 5px; 
                text-align: left;
            }
            td.tdMainTitle
            {
                background-color: #FFFFFF;
                font-size: 14px;
                font-family: 宋体;
                line-height: 20pt;
                vertical-align: middle;
                line-height: 20pt;
                padding-left: 5px;
                font-weight:bold;
            }
        </style>
    </head>
    <body>
        <table class=""tableItem"" width=""98%"" align=""center"" cellpadding=""1"" cellspacing=""1"">
            <tbody>
                " + theResult.ToString() + @" 
            </tbody>
        </table>
    </body>
    </html>
    ";
            }
    
    
            /// <summary>
            /// 转成HTML(DL样式的)
            /// </summary>
            /// <returns></returns>
            public string ToDLHtml()
            {
    
                StringBuilder theResult = new StringBuilder();
    
                List<RowItem> rowList = ListItem;
                List<CellItem> cellList = null;
                string cssClassName = null;
                string addHtml = "";
                int idx = 0;
                int rowIndex = 0;
                string tdName = "td";
    
                foreach (RowItem row in rowList)
                {
                    if (row == null)
                        continue;
    
                    ++rowIndex;
    
                    if (rowIndex == 1)
                    {
                        tdName = "th";
                    }
                    else
                    {
                        tdName = "td";
                    }
    
                    idx = 0;
                    theResult.Append(@"<tr class=""DL"">");
    
                    cellList = row.ListItem;
    
                    if (rowIndex % 2 == 1)
                        cssClassName = "DL";
                    else
                        cssClassName = "DLA";
    
                    foreach (CellItem cell in cellList)
                    {
    
    
                        ++idx;
    
                        if (cell.ColSpan == 1)
                        {
                            addHtml = "";
                        }
                        else if (cell.ColSpan == 0)
                        {
                            addHtml = " colspan=\"" + (ColNumber - idx + 1).ToString() + "\"";
                        }
                        else
                        {
                            addHtml = " colspan=\"" + cell.ColSpan.ToString() + "\"";
                        }
    
                        if (cell.RowSpan != 1)
                        {
                            addHtml += " rowspan=\"" + cell.RowSpan.ToString() + "\"";
                        }
    
                        if (cell.CellWidth != 0)
                        {
                            addHtml += " width=\"" + cell.CellWidth.ToString() + "px\"";
                        }
    
                        theResult.Append(@"<" + tdName + addHtml + @" class=""" + cssClassName + @""" align=""center"" height=""30px"">
                        " + cell.TheText + @"
                    </" + tdName + ">");
    
                    }
    
                    theResult.Append("</tr>");
                }
    
                string strTableHtml = @"<table class=""DL""><tbody>
                " + theResult.ToString() + @" 
            </tbody>
        </table>";
    
                return strTableHtml;
            }
    
        }
    }
    
    
  • using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Reflection;
    
    namespace eKing.EkPageCreate.Classes.TableClasses
    {
        /// <summary>
        /// 行记录项
        /// </summary>
        [Serializable]
        public class RowItem 
        {
            /// <summary>
            /// 行记录项
            /// </summary>
            public RowItem()
            {
            }
    
            /// <summary>
            /// 单元格记录项
            /// </summary>
            protected List<CellItem> m_ListItem = null;
    
            /// <summary>
            /// 单元格记录项
            /// </summary>
            public List<CellItem> ListItem
            {
                get
                {
                    return m_ListItem;
                }
                set
                {
                    m_ListItem = value;
                }
            }
    
            public void AddCellItem(int idx, CellItem cell)
            {
                if (idx < 0)
                {
                    throw new Exception
                        (
                            "方法:"
                            + MethodBase.GetCurrentMethod().ReflectedType.FullName
                            + " "
                            + MethodBase.GetCurrentMethod().ToString()
                            + " 发生异常:idx(" + idx.ToString() + ") < 0"
                        );
                }
                if (cell == null)
                    return;
    
                if (ListItem == null)
                    ListItem = new List<CellItem>();
    
                if (idx >= ListItem.Count)
                    ListItem.Add(cell);
                else
                    ListItem.Insert(idx, cell);
            }
    
            /// <summary>
            /// 
            /// </summary>
            /// <param name="theText"></param>
            /// <returns></returns>
            public static RowItem RowItemBuildFullRowTitle(string theText)
            {
                RowItem theResult = new RowItem();
                CellItem cell = null;
                List<CellItem> cellList = new List<CellItem>();
                theResult.ListItem = cellList;
    
    
                cell = new CellItem();
                cell.EmCellStyleX = CellItem.EmCellStyle.标题_居左;
                cell.ColSpan = 0;
                cell.TheText = theText;
    
                cellList.Add(cell);
    
                return theResult;
            }
    
            /// <summary>
            /// 
            /// </summary>
            /// <param name="p"></param>
            /// <returns></returns>
            public static RowItem RowItemBuildTitle(params string[] p)
            {
    
                if (p == null)
                    return null;
    
                RowItem theResult = new RowItem();
                CellItem cell = null;
                List<CellItem> cellList = new List<CellItem>();
                theResult.ListItem = cellList;
    
                foreach (string s in p)
                {
                    cell = new CellItem();
                    cell.EmCellStyleX = CellItem.EmCellStyle.标题_居中;
                    cell.TheText = s;
    
                    cellList.Add(cell);
                }
    
                return theResult;
            }
    
            /// <summary>
            /// 
            /// </summary>
            /// <param name="p"></param>
            /// <returns></returns>
            public static RowItem RowItemBuildTitleByDataColumnCollection
                (
                    DataColumnCollection dcc
                )
            {
                RowItem theResult = new RowItem();
                CellItem cell = null;
                List<CellItem> cellList = new List<CellItem>();
                theResult.ListItem = cellList;
    
                foreach (DataColumn dc in dcc)
                {
                    cell = new CellItem();
                    cell.EmCellStyleX = CellItem.EmCellStyle.标题_居中;
                    cell.TheText = dc.ColumnName;
    
                    cellList.Add(cell);
                }
    
                return theResult;
            }
    
    
            /// <summary>
            /// 
            /// </summary>
            /// <param name="p"></param>
            /// <returns></returns>
            public static RowItem RowItemBuildTitleByDataRow
                (
                    DataColumnCollection dcc,
                    DataRow dr
                )
            {
                RowItem theResult = new RowItem();
                CellItem cell = null;
                List<CellItem> cellList = new List<CellItem>();
                theResult.ListItem = cellList;
                object oValue = null;
    
                foreach (DataColumn dc in dcc)
                {
                    cell = new CellItem();
    
                    oValue = dr[dc.ColumnName];
    
                    if (oValue == null || oValue == DBNull.Value)
                        cell.TheText = "";
                    else
                    {
                        if (dc.DataType == typeof(DateTime))
                        {
                            cell.TheText = DateTimeConvertShow(oValue);
                        }
                        else
                            cell.TheText = oValue.ToString();
                    }
                    cellList.Add(cell);
                }
    
                return theResult;
            }
    
            /// <summary>
            /// object转成时间的判断显示
            /// </summary>
            /// <param name="oValue"></param>
            /// <returns></returns>
            private static string DateTimeConvertShow(object oValue)
            {
                if (oValue == null)
                    return "";
    
                string str = oValue.ToString();
    
                if (str.Length == 0)
                    return "";
    
                DateTime dt = DateTime.MaxValue;
    
                if (!DateTime.TryParse(str, out dt))
                    return str;
    
                if (dt == dt.Date)
                    return dt.ToString("yyyy-MM-dd");
                else
                    return dt.ToString("yyyy-MM-dd HH:mm:ss");
            }
        }
    }
    
    
  • using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace eKing.EkPageCreate.Classes.TableClasses
    {
        /// <summary>
        /// 单元格
        /// </summary>
        [Serializable]
        public class CellItem
        {
            /// <summary>
            /// 单元格样式
            /// </summary>
            public enum EmCellStyle
            {
                /// <summary>
                /// 内容
                /// </summary>
                内容 = 1,
                /// <summary>
                /// 标题_居左
                /// </summary>
                标题_居左,
                /// <summary>
                /// 标题_居中
                /// </summary>
                标题_居中,
                /// <summary>
                /// 子标题_居中
                /// </summary>
                子标题_居中,
                /// <summary>
                /// 内容_红
                /// </summary>
                内容_红,
                /// <summary>
                /// 内容_居左
                /// </summary>
                内容_居左
            }
    
            /// <summary>
            /// 单元格
            /// </summary>
            public CellItem()
            {
    
            }
    
            /// <summary>
            /// 
            /// </summary>
            /// <param name="_theText"></param>
            public CellItem(string _theText)
            {
                m_TheText = _theText;
            }
    
    
            /// <summary>
            /// 
            /// </summary>
            /// <param name="_EmCellStyleX"></param>
            /// <param name="_theText"></param>
            public CellItem(EmCellStyle _EmCellStyleX, string _theText)
            {
                m_EmCellStyleX = _EmCellStyleX;
                m_TheText = _theText;
            }
    
            /// <summary>
            /// 
            /// </summary>
            protected int m_ColSpan = 1;
    
            /// <summary>
            /// 
            /// </summary>
            public int ColSpan
            {
                get
                {
                    return m_ColSpan;
                }
                set
                {
                    m_ColSpan = value;
                }
            }
    
    
            #region RowSpan ~ 行项
    
            /// <summary>
            /// RowSpan ~ 行项
            /// </summary>
            protected int m_RowSpan = 1;
    
            /// <summary>
            /// RowSpan ~ 行项
            /// </summary>
            public int RowSpan
            {
                get
                {
                    return m_RowSpan;
                }
                set
                {
                    m_RowSpan = value;
                }
            }
    
            #endregion RowSpan ~ 行项
    
    
            #region CellWidth ~ 宽度
    
            /// <summary>
            /// CellWidth ~ 宽度
            /// </summary>
            protected int m_CellWidth = 0;
    
            /// <summary>
            /// CellWidth ~ 宽度
            /// </summary>
            public int CellWidth
            {
                get
                {
                    return m_CellWidth;
                }
                set
                {
                    m_CellWidth = value;
                }
            }
    
            #endregion CellWidth ~ 宽度
    
    
    
            /// <summary>
            /// 
            /// </summary>
            protected EmCellStyle m_EmCellStyleX = EmCellStyle.内容;
    
            /// <summary>
            /// 
            /// </summary>
            public EmCellStyle EmCellStyleX
            {
                get
                {
                    return m_EmCellStyleX;
                }
                set
                {
                    m_EmCellStyleX = value;
                }
            }
    
            /// <summary>
            /// 
            /// </summary>
            protected string m_TheText = "";
    
            /// <summary>
            /// 
            /// </summary>
            public string TheText
            {
                get
                {
                    return m_TheText;
                }
                set
                {
                    m_TheText = value;
                }
            }
        }
    }
    
    
  • 
            /// <summary>
            /// 构建Title
            /// </summary>
            /// <param name="checkSEQ"></param>
            /// <returns></returns>
            protected RowItem CheckDBTableBuildRowItemTitle(bool checkSEQ)
            {
                RowItem theResult = new RowItem();
                CellItem cell = null;
                List<CellItem> cellList = new List<CellItem>();
                theResult.ListItem = cellList;
    
                cell = new CellItem("序号");
                cellList.Add(cell);
    
                cell = new CellItem("表名");
                cellList.Add(cell);
    
                cell = new CellItem("关键字");
                cellList.Add(cell);
    
                cell = new CellItem("注释");
                cellList.Add(cell);
    
                cell = new CellItem("生产库多出来的字段");
                cellList.Add(cell);
    
                cell = new CellItem("生产库缺失字段");
                cellList.Add(cell);
    
                cell = new CellItem("表校验异常");
                cellList.Add(cell);
    
                if(checkSEQ)
                {
    
                    cell = new CellItem("序列号");
                    cellList.Add(cell);
    
                    cell = new CellItem("序列号当前值");
                    cellList.Add(cell);
    
                    cell = new CellItem("关键字最大值");
                    cellList.Add(cell);
    
                    cell = new CellItem("序列号校验异常");
                    cellList.Add(cell);
                }
    
                return theResult;
            }
    
    
            /// <summary>
            /// 构建Title
            /// </summary>
            /// <param name="checkSEQ"></param>
            /// <returns></returns>
            protected RowItem CheckDBTableBuildRowItem
                (
                    bool checkSEQ,
                    int rowNum,
                    DbTableCheckResult item
                )
            {
                RowItem theResult = new RowItem();
                CellItem cell = null;
                List<CellItem> cellList = new List<CellItem>();
                theResult.ListItem = cellList;
    
                cell = new CellItem(rowNum.ToString());
                cellList.Add(cell);
    
                cell = new CellItem(item.TableName);
                cellList.Add(cell);
    
                cell = new CellItem(item.PkName);
                cellList.Add(cell);
    
                cell = new CellItem(item.TableComment);
                cellList.Add(cell);
    
                cell = new CellItem(item.MoreDataColumn);
                cellList.Add(cell);
    
                cell = new CellItem(item.MissDataColumn);
                cellList.Add(cell);
    
                cell = new CellItem(item.ErrMsg);
                cellList.Add(cell);
    
                if (checkSEQ)
                {
    
                    cell = new CellItem(item.SequenceName);
                    cellList.Add(cell);
    
                    cell = new CellItem(item.CurSeqValue);
                    cellList.Add(cell);
    
                    cell = new CellItem(item.MaxID);
                    cellList.Add(cell);
    
                    cell = new CellItem(item.CheckSeqErrMsg);
                    cellList.Add(cell);
                }
    
                return theResult;
            }
    
    
            /// <summary>
            /// 通过逻辑类和数据库表比对 - 校验数据表
            /// </summary>
            /// <param name="checkSEQ">校验序列号</param>
            /// <param name="strTableName">表名</param>
            /// <param name="xdbHelper">数据库链接串</param>
            /// <returns></returns>
            public string CheckDBTableToHTML
                (
                    bool checkSEQ,
                    string strTableName, 
                    DBHelper xdbHelper
                )
            {
                List<DbTableCheckResult> theList
                    = 
                    CheckDBTable(checkSEQ,strTableName, xdbHelper);
    
                TableItem tb = new TableItem();
    
                if (checkSEQ)
                {
                    tb.ColNumber = 11;
                }
                else
                {
                    tb.ColNumber = 7;
                }
    
                List<RowItem> rowList = new List<RowItem>();
                tb.ListItem = rowList;
    
                RowItem row = null;
    
                row = CheckDBTableBuildRowItemTitle(checkSEQ);
                rowList.Add(row);
    
                
    
                int idx = 0;
                foreach (DbTableCheckResult item in theList)
                {
                    if (checkSEQ)
                    {
                        if (item.IsOK && item.CheckSeqIsOK)
                            continue;
                    }
                    else
                    {
                        if (item.IsOK)
                            continue;
                    }
    
                    ++idx;
    
                    // item.ToTrHtml
                    row = CheckDBTableBuildRowItem(checkSEQ, idx, item);
                    rowList.Add(row);
    
                    // theResult.AppendLine(item.ToTrHtml());
                }
    
              
    
                return tb.ToDLHtml();
            }
    

上一条:

下一条:


 

相关评论

评论加载中……
 

发表评论

类型:
内容:
  (Alt+Enter)