专栏首页跟着阿笨一起玩NET[C#] 常用工具类——直接在浏览器输出数据

[C#] 常用工具类——直接在浏览器输出数据

/// <summary>
    /// <para> </para>
    ///  常用工具类——直接在浏览器输出数据
    /// <para> -------------------------------------------------------------</para>
    /// <para> DumpDataTable:接在浏览器输出数据DataTable</para>
    /// <para> DumpListItemILIST:直接在浏览器输出数据ILIST<ListItem></para>
    /// <para> DumpDataTableILIST:直接在浏览器输出数据DataTableILIST</para>
    /// <para> DumpIntArrILIST:直接在浏览器输出数据整型数组ILIST<int[]></para>
    /// <para> DumpStrArrILIST:直接在浏览器输出数据字符串数组的ILIST<string[]></para>
    /// <para> DumpDataSet:直接在浏览器输出数据DataSet对象</para>
    /// <para> DumpObjectArr2:直接在浏览器中输出二维数组</para>
    /// </summary>
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI.WebControls;
using System.Resources;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
 
 
namespace Utils
{
    /// <summary>
    /// <para> </para>
    ///  常用工具类——直接在浏览器输出数据
    /// <para> -------------------------------------------------------------</para>
    /// <para> DumpDataTable:接在浏览器输出数据DataTable</para>
    /// <para> DumpListItemILIST:直接在浏览器输出数据ILIST<ListItem></para>
    /// <para> DumpDataTableILIST:直接在浏览器输出数据DataTableILIST</para>
    /// <para> DumpIntArrILIST:直接在浏览器输出数据整型数组ILIST<int[]></para>
    /// <para> DumpStrArrILIST:直接在浏览器输出数据字符串数组的ILIST<string[]></para>
    /// <para> DumpDataSet:直接在浏览器输出数据DataSet对象</para>
    /// <para> DumpObjectArr2:直接在浏览器中输出二维数组</para>
    /// </summary>
 
 
    public class DumpHelper
    {
        #region 构造方法设置
        /// <summary>
        /// 数据类型颜色
        /// </summary>
        protected static string ColorDataType = "red";
        /// <summary>
        /// 行数颜色
        /// </summary>
        protected static string ColorRowCount = "red";
        /// <summary>
        /// 列数颜色
        /// </summary>
        protected static string ColorColumnsCount = "red";
        /// <summary>
        /// 无数据时颜色
        /// </summary>
        protected static string ColorNoData = "red";
        /// <summary>
        /// 序号颜色
        /// </summary>
        protected static string ColorSerial = "black";
        /// <summary>
        /// 字段名颜色
        /// </summary>
        protected static string ColorFieldName = "blue";
        /// <summary>
        /// 字段类型颜色
        /// </summary>
        protected static string ColorFieldType = "#cc9999";
        /// <summary>
        /// 字段值颜色
        /// </summary>
        protected static string ColorFieldValue = "#9933ff";
        /// <summary>
        /// 最终输出字符串
        /// </summary>
        protected static string DumpStr = string.Empty;
        /// <summary>
        /// 输出字符串DIV开始
        /// </summary>
        protected static string DivRef = "<div style='font-size:14px;line-height:20px;margin:5px 0 0 5px;'>";
        #endregion
 
        #region  直接在浏览器输出数据DataTable
        /// <summary>
        /// 直接在浏览器输出数据DataTable
        /// </summary>
        /// <param name="Dt">DataTable表格</param>
        public static void DumpDataTable(DataTable Dt)
        {
            if (Dt != null)
            {
                int DtRowsCount = Dt.Rows.Count;
                int DtColumnsCount = Dt.Columns.Count;
                DumpStr = DivRef + "数据类型:<font color=" + ColorDataType + ">TableDate</font> [ 表名:" + Dt.TableName + " 行数:<font color=" + ColorRowCount + ">" + DtRowsCount + "</font>,列数:<font color=" + ColorColumnsCount + ">" + DtColumnsCount + "</font> ] <hr>";
                if (DtRowsCount < 1) DumpStr += " <font color=" + ColorNoData + ">无表格行数据!</font>";
                else
                {
                    for (int i = 0; i < DtRowsCount; i++)
                    {
                        DumpStr += "( Row:<font color=" + ColorSerial + ">" + i + "</font> ) => ";
                        for (int j = 0; j < DtColumnsCount; j++)
                        {
                            string ColumnName = Dt.Columns[j].ColumnName.ToString();
                            string ColumnType = Dt.Columns[j].DataType.ToString();
                            DumpStr += "  [" + j + "] <font color=" + ColorFieldName + ">" + ColumnName + "</font> ( <font color=" + ColorFieldType + ">" + ColumnType + "</font> ) => \"<font color=" + ColorFieldValue + ">" + Dt.Rows[i][ColumnName].ToString() + "</font>\"";
                        }
                        DumpStr += "<p>";
                    }
                }
            }
            HttpContext.Current.Response.Write(DumpStr + "</div>");
        }
        #endregion
 
        #region 直接在浏览器输出数据ILIST<ListItem>
        /// <summary>
        /// 直接在浏览器输出数据ILIST<ListItem>
        /// </summary>
        /// <param name="ListItems">ILIST<ListItem>泛型数据</param>
        public static void DumpListItemILIST(IList<ListItem> ListItems)
        {
            if (ListItems != null)
            {
                int ListCount = ListItems.Count;
                DumpStr = DivRef + "数据类型:<font color=" + ColorDataType + ">IList<ListItem></font> [ 项目数:<font color=" + ColorRowCount + ">" + ListCount + "</font> ] <hr>";
                if (ListCount < 1) DumpStr += " <font color=" + ColorNoData + ">泛型中无数据!</font>";
                else
                {
                    for (int i = 0; i < ListCount; i++)
                    {
                        DumpStr += "( " + i + " ) => ";
                        string Texts = ListItems[i].Text.ToString();
                        string Values = ListItems[i].Value.ToString();
                        DumpStr += "  <font color=" + ColorFieldName + ">[ Text ]</font> => \"<font color=" + ColorFieldValue + ">" + Texts + "</font>\"";
                        DumpStr += "  <font color=" + ColorFieldName + ">[ Value ]</font> => \"<font color=" + ColorFieldValue + ">" + Values + "</font>\"";
                    }
                }
            }
            HttpContext.Current.Response.Write(DumpStr + "</div>");
        }
        #endregion
 
        #region  直接在浏览器输出数据DataTableILIST
        /// <summary>
        /// 直接在浏览器输出数据DataTableILIST
        /// </summary>
        /// <param name="ListDataTable">ILIST<ListDataTable>泛型数据</param>
        public static void DumpDataTableILIST(IList<DataTable> ListDataTable)
        {
            if (ListDataTable != null)
            {
                int ListCount = ListDataTable.Count;
                DumpStr = DivRef + "数据类型:<font color=" + ColorDataType + ">IList<DataTable></font> [ DataTable数:<font color=" + ColorRowCount + ">" + ListCount + "</font> ] <hr>";
                if (ListCount < 1) DumpStr += " <font color=" + ColorNoData + ">泛型中无数据!</font>";
                else
                {
                    for (int i = 0; i < ListCount; i++)
                    {
                        if (i > 0) DumpStr += "<hr>";
                        DataTable Dt = ListDataTable[i];
                        int DtRowsCount = Dt.Rows.Count;
                        int DtColumnsCount = Dt.Columns.Count;
                        DumpStr += "( 表序号:" + i + " 表名:" + Dt.TableName + " ) => [ 行数:<font color=" + ColorRowCount + ">" + DtRowsCount + "</font>,列数:<font color=" + ColorColumnsCount + ">" + DtColumnsCount + "</font> ]";
                        if (DtRowsCount < 1) DumpStr += " <font color=" + ColorNoData + ">表格中无数据!</font><p>";
                        else
                        {
                            for (int k = 0; k < DtRowsCount; k++)
                            {
                                DumpStr += "  ( Row:<font color=" + ColorSerial + ">" + k + "</font> ) => ";
                                for (int j = 0; j < DtRowsCount; j++)
                                {
                                    string ColumnName = Dt.Columns[j].ColumnName.ToString();
                                    string ColumnType = Dt.Columns[j].DataType.ToString();
                                    DumpStr += "    [" + j + "] <font color=" + ColorFieldName + ">" + ColumnName + "</font> ( <font color=" + ColorFieldType + ">" + ColumnType + "</font> ) => \"<font color=" + ColorFieldValue + ">" + Dt.Rows[k][ColumnName].ToString() + "</font>\"";
                                }
                                DumpStr += "<p>";
                            }
                        }
                    }
                }
            }
            HttpContext.Current.Response.Write(DumpStr + "</div>");
        }
        #endregion
 
        #region 直接在浏览器输出数据整型数组ILIST<int[]>
        /// <summary>
        ///  直接在浏览器输出数据整型数组ILIST<int[]>
        /// </summary>
        /// <param name="IntList">ILIST<Int[]>泛型数据</param>
        public static void DumpIntArrILIST(IList<int[]> IntList)
        {
            if (IntList != null)
            {
                int ListCount = IntList.Count;
                DumpStr = DivRef + "数据类型:<font color=" + ColorDataType + ">IList<Int[]></font> [ Int[]个数:<font color=" + ColorRowCount + ">" + ListCount + "</font> ] <hr>";
                if (ListCount < 1) DumpStr += " <font color=" + ColorNoData + ">泛型中无数据!</font>";
                else
                {
                    for (int i = 0; i < ListCount; i++)
                    {
                        int[] IntArr = IntList[i];
                        int IntArrCount = IntArr.Length;
                        DumpStr += "( Int[]:<font color=" + ColorSerial + ">" + i + "</font> ) => 数组元素个数:<font color=" + ColorSerial + ">" + IntArrCount + "</font>";
                        if (IntArrCount < 1) DumpStr += "  <font color=" + ColorNoData + ">数组中无数据</font><p>";
                        else
                        {
                            for (int j = 0; j < IntArrCount; j++)
                            {
                                DumpStr += "  [" + j + "]  ( <font color=" + ColorFieldType + ">Int</font> ) => \"<font color=" + ColorFieldValue + ">" + IntArr[j].ToString() + "</font>\"";
                            }
                        }
                    }
                }
            }
            HttpContext.Current.Response.Write(DumpStr + "</div>");
        }
        #endregion
 
        #region 直接在浏览器输出数据字符串数组的ILIST<string[]>
        /// <summary>
        /// 直接在浏览器输出数据字符串数组的ILIST<string[]>
        /// </summary>
        /// <param name="StrList">ILIST<String[]>泛型数据</param>
        public static void DumpStrArrILIST(IList<string[]> StrList)
        {
            if (StrList != null)
            {
                int ListCount = StrList.Count;
                DumpStr = DivRef + "数据类型:<font color=" + ColorDataType + ">IList<String[]></font> [ String[]个数:<font color=" + ColorRowCount + ">" + ListCount + "</font> ] <hr>";
                if (ListCount < 1) DumpStr += " <font color=" + ColorNoData + ">泛型中无数据!</font>";
                else
                {
                    for (int i = 0; i < ListCount; i++)
                    {
                        string[] StrArr = StrList[i];
                        int StrArrCount = StrArr.Length;
                        DumpStr += "( String[]:<font color=" + ColorSerial + ">" + i + "</font> ) => 数组元素个数:<font color=" + ColorSerial + ">" + StrArrCount + "</font>";
                        if (StrArrCount < 1) DumpStr += "  <font color=" + ColorNoData + ">数组中无数据</font><p>";
                        else
                        {
                            for (int j = 0; j < StrArrCount; j++)
                            {
                                DumpStr += "  [" + j + "]  ( <font color=" + ColorFieldType + ">Int</font> ) => \"<font color=" + ColorFieldValue + ">" + StrArr[j].ToString() + "</font>\"";
                            }
                        }
                    }
                }
            }
            HttpContext.Current.Response.Write(DumpStr + "</div>");
        }
        #endregion
 
        #region 直接在浏览器输出数据DataSet对象
        /// <summary>
        /// 直接在浏览器输出数据DataSet对象
        /// </summary>
        /// <param name="DS">DataSet对象</param>
        public static void DumpDataSet(DataSet DS)
        {
            if (DS != null)
            {
                int DtCount = DS.Tables.Count;
                DumpStr = DivRef + "数据类型:<font color=" + ColorDataType + ">DataSet对象</font> [ DataSet中表格个数:<font color=" + ColorRowCount + ">" + DtCount + "</font> ] <hr>";
                if (DtCount < 1) DumpStr += " <font color=" + ColorNoData + ">DataSet对象中无表格!</font>";
                else
                {
                    for (int i = 0; i < DtCount; i++)
                    {
                        if (i > 0) DumpStr += "<hr>";
                        DataTable Dt = DS.Tables[i];
                        int DtRowsCount = Dt.Rows.Count;
                        int DtColumnsCount = Dt.Columns.Count;
                        DumpStr += "( 表序号:" + i + " 表名:" + Dt.TableName + " ) => [ 行数:<font color=" + ColorRowCount + ">" + DtRowsCount + "</font>,列数:<font color=" + ColorColumnsCount + ">" + DtColumnsCount + "</font> ]";
                        if (DtRowsCount < 1) DumpStr += " <font color=" + ColorNoData + ">表格中无数据!</font><p>";
                        else
                        {
                            for (int k = 0; k < DtRowsCount; k++)
                            {
                                DumpStr += "  ( Row:<font color=" + ColorSerial + ">" + k + "</font> ) => ";
                                for (int j = 0; j < DtColumnsCount; j++)
                                {
                                    string ColumnName = Dt.Columns[j].ColumnName.ToString();
                                    string ColumnType = Dt.Columns[j].DataType.ToString();
                                    DumpStr += "    [" + j + "] <font color=" + ColorFieldName + ">" + ColumnName + "</font> ( <font color=" + ColorFieldType + ">" + ColumnType + "</font> ) => \"<font color=" + ColorFieldValue + ">" + Dt.Rows[k][ColumnName].ToString() + "</font>\"";
                                }
                                DumpStr += "<p>";
                            }
                        }
                    }
                }
            }
            HttpContext.Current.Response.Write(DumpStr + "</div>");
        }
        #endregion
 
        #region 直接在浏览器中输出二维数组
        /// <summary>
        /// 直接在浏览器中输出二维数组
        /// </summary>
        /// <param name="ObjArr">二维数组</param>
        public static void DumpObjectArr2(object[] ObjArr)
        {
            if (ObjArr != null)
            {
                int ObjLength = ObjArr.Length;
                DumpStr = DivRef + "数据类型:<font color=" + ColorDataType + ">Object数组</font> [ 数组中元素个数:<font color=" + ColorRowCount + ">" + ObjLength + "</font> ] <hr>";
                for (int i = 0; i < ObjLength; i++)
                {
                    DumpStr += "  ( <font color=" + ColorSerial + ">" + i + "</font> ) ";
                    object Arri = ObjArr[i];
                    DumpStr += " ( ";
                    DumpStr += "<font color=" + ColorFieldType + ">" + Arri.GetType().ToString() + "</font>";
                    DumpStr += " ) ";
                    DumpStr += " => <font color=" + ColorFieldValue + ">\"" + Arri + "\"</font>";
                }
            }
            HttpContext.Current.Response.Write(DumpStr + "</div>");
        }
        #endregion
 
        public static void DumpObjectArr(object[] ObjArr)
        {
            if (ObjArr != null)
            {
                HttpContext.Current.Response.Write(ObjArr.GetType());
            }
        }
    }
}

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • C# 使用 NPOI 库读写 Excel 文件

    NPOI 是开源的 POI 项目的.NET版,可以用来读写Excel,Word,PPT文件。在处理Excel文件上,NPOI 可以同时兼容 xls 和 xlsx...

    跟着阿笨一起玩NET
  • 关于DevExpress的XtraTreeList使用方法总结

    跟着阿笨一起玩NET
  • C#将数据以XML格式写入Excel

    本文转载:http://www.cnblogs.com/eflylab/archive/2008/09/21/1295580.html

    跟着阿笨一起玩NET
  • CSS3 font 学习笔记

    LRainner
  • @font-face 属性

    @font-face 允许网页开发者为其网页指定在线字体。 通过这种作者自备字体的方式,@font-face 可以消除对用户电脑字体的依赖。 @font-f...

    Html5知典
  • HTML5基础——文字常用标签(上)

    一、微文概览 标题 字体与字号 颜色 二、详细介绍 1. 标题 我们在HTML文件中写一篇文字时,总会希望将标题的文字设置的大一些,黑一些,粗一些,那么怎样在H...

    用户1667431
  • 让你的网站用上炫酷的中文字体

    随着当前 Web 技术的日新月异,网页界面内容越来越丰富,让人眼花缭乱,其中就包括了网页中的各种自定义字体。

    米开朗基杨
  • django2用模板代码图标字体丢失报404 cJZKeOuBrn4kERxqtaUH3T8E0i7KZn-EPnyo3HZu7kw.woff

    玩蛇的胖纸
  • Android开发之 全局替换字体

    第一反应是好简单,换个TypeFace不就完了,但是不可能每个控件去设置一个吧?不存在的。

    Xiaolei123
  • iconfont关于content值的坑

    font-carrier是一个功能强大的字体操作库,使用它你可以随心所欲的操作字体。让你可以在svg的维度改造字体的展现形状。 //可以设置某个字对应的形状,当...

    IMWeb前端团队

扫码关注云+社区

领取腾讯云代金券