[C#] 常用工具類——直接在瀏覽器輸出資料

來源:互聯網
上載者:User

標籤:style   blog   http   color   os   io   資料   for   

/// <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());            }        }    }}

  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.