以前看到一個朋友的C/S界面程序裡,把數據表用網格的形式輸出到TextBox,覺得挺好用的,所以我也做了一個C#版的類。
下面是效果:
原碼:
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Collections;
namespace CommonLibrary
...{
/**//// <summary>
/// PaintTable 給制表格
/// 把數據以表格的形式呈現出來。
/// </summary>
public class PaintTable
...{
ArrayList myDataList;
char[] lines = new char[] ...{ ''┌'', ''─'', ''┬'', ''┐'', ''│'', ''│'', ''├'', ''─'', ''┼'', ''┤'', ''│'', ''├'', ''─'', ''┼'', ''┤'', ''└'', ''─'', ''┴'', ''┘'' };
int maxRowCount;
/**//// <summary>
/// 構造函數
/// </summary>
public PaintTable()
...{
myDataList = new ArrayList();
maxRowCount = 0;
}
/**//// <summary>
/// 構造函數
/// </summary>
/// <param name="myStyle">表格樣式</param>
public PaintTable(PaintLineStyle myStyle)
...{
myDataList = new ArrayList();
maxRowCount = 0;
if (myStyle == PaintLineStyle.DoubleLine)
...{
lines = new char[] ...{ ''╔'', ''═'', ''╤'', ''╗'', ''║'', ''│'', ''╠'', ''═'', ''╪'', ''╣'', ''│'', ''╟'', ''─'', ''┼'', ''╢'', ''╚'', ''═'', ''╧'', ''╝'' };
}
else if (myStyle == PaintLineStyle.DishedLine)
...{
lines = new char[] ...{ ''┏'', ''━'', ''┳'', ''┓'', ''┃'', ''┃'', ''┣'', ''━'', ''╇'', ''┫'', ''│'', ''┠'', ''─'', ''┼'', ''┨'', ''┗'', ''━'', ''┷'', ''┛'' };
}
}
/**//// <summary>
/// 從數據表添加數據
/// </summary>
/// <param name="myTable">數據表</param>
public void AddTable(DataTable myTable)
...{
if (myTable != null)
...{
object[] myObJS2 = new object[myTable.Columns.Count];
for (int j = 0; j < myTable.Columns.Count; j++)
...{
myObJS[j] = myTable.Rows[i][j];
}
AddLine(myObJS);
}
}
}
/**//// <summary>
/// 清空現數據
/// </summary>
public void Clear()
...{
this.myDataList.Clear();
this.maxRowCount = 0;
}
/**//// <summary>
/// 添加行
/// </summary>
/// <param name="inputValues">行</param>
public void AddLine(params object[] inputValues)
...{
myDataList.Add(inputValues);
if (inputValues.Length > maxRowCount) maxRowCount = inputValues.Length;
}
/**//// <summary>
/// 計算占位長度
/// </summary>
private ArrayList CountColLength()
...{
ArrayList myLengthList = new ArrayList();
for (int i = 0; i < maxRowCount; i++)
...{
int nowRowLength = 0;
for (int j = 0; j < myDataList.Count; j++)
...{
object[] nowValues = (object[])myDataList[j];
if (i < nowValues.Length)
...{
int valueLength = GetStringSize(GetValue(nowValues[i]));
if (valueLength > nowRowLength) nowRowLength = valueLength;
if (nowRowLength % 2 == 1) nowRowLength++;
}
}
myLengthList.Add(nowRowLength);
}
return myLengthList;
}
/**//// <summary>
/// 計算字符串的占位長度
/// </summary>
private int GetStringSize(string inputStr)
...{
int length = Encoding.Default.GetByteCount(inputStr);
return length;
}
/**//// <summary>
/// 轉為字符串
/// </summary>
/// <param name="inputObj">對象</param>
/// <returns>返回字符串</returns>
private string GetValue(object inputObj)
...{
if (inputObj != null && inputObj != DBNull.Value)
...{
return inputObj.ToString();
}
else
...{
return "";
}
}
/**//// <summary>
/// 補上空格
/// </summary>
/// <param name="inputStr">源字符串</param>
/// <param name="maxLength">長度</param>
/// <returns>返回字符串</returns>
private string GetFullString(string inputStr, int maxLength)
...{
int inputLength = GetStringSize(inputStr);
if (inputLength < maxLength)
...{
int addCount = maxLength - inputLength;
for (int i = 0; i < addCount; i++)
...{
inputStr += " ";
}
}
return inputStr.Replace(" ", " ").Replace(" ", " ");
}
/**//// <summary>
/// 計算最大長度
/// </summary>
/// <param name="inputList">集合</param>
/// <returns>長度</returns>
private int GetAllRowLength(ArrayList inputList)
...{
int allRowLength = 0;
for (int i = 0; i < inputList.Count; i++)
...{
allRowLength += (int)inputList[i];
}
return allRowLength;
}
/**//// <summary>
/// 畫線
/// </summary>
/// <param name="inputChar">樣式</param>
/// <param name="length">長度</param>
/// <returns>返回字符串</returns>
private string PaintLine(char inputChar, int length)
...{
string reStr = "";
for (int i = 0; i < length; i++)
...{
reStr += inputChar;
}
return reStr;
}
/**//// <summary>
/// 畫整行
/// </summary>
/// <param name="inputStr"></param>
/// <param name="inputMid"></param>
/// <param name="myLengthList"></param>
/// <returns></returns>
private string PaintAllLine(char inputStr, char inputMid, ArrayList myLengthList)
...{
string reStr = "";
for (int i = 0; i < myLengthList.Count; i++)
...{
reStr += PaintLine(inputStr, ((int)myLengthList[i]) / 2);
if (i < myLengthList.Count - 1)
...{
reStr += inputMid;
}
}
return reStr;
}
/**//// <summary>
/// 畫表
/// </summary>
/// <returns>返回表格</returns>
public string Paint()
...{
StringBuilder myBuilder = new StringBuilder();
ArrayList myLengthList = CountColLength();
int allRowLength = GetAllRowLength(myLengthList);
myBuilder.Append(lines[0]);
myBuilder.Append(PaintAllLine(lines[1], lines[2], myLengthList));
myBuilder.Append(lines[3]);
myBuilder.Append(" ");
for (int j = 0; j < myDataList.Count; j++)
...{
myBuilder.Append(lines[4]);
object[] nowValues = (object[])myDataList[j];
for (int i = 0; i < maxRowCount; i++)
...{
int maxLength = (int)myLengthList[i];
if (i < nowValues.Length)
...{
myBuilder.Append(GetFullString(GetValue(nowValues[i]), maxLength));
}
else
...{
myBuilder.Append(GetFullString("", maxLength));
}
if (j == 0 && i < maxRowCount - 1)
...{
myBuilder.Append(lines[5]);
}
else if (i < maxRowCount - 1)
...{
myBuilder.Append(lines[10]);
}
}
myBuilder.Append(lines[4] + " ");
if (j == 0)
...{
myBuilder.Append(lines[6]);
myBuilder.Append(PaintAllLine(lines[7], lines[8], myLengthList));
myBuilder.Append(lines[9]);
myBuilder.Append(" ");
}
else if (j < myDataList.Count - 1)
...{
myBuilder.Append(lines[11]);
myBuilder.Append(PaintAllLine(lines[12], lines[13], myLengthList));
myBuilder.Append(lines[14]);
myBuilder.Append(" ");
}
}
myBuilder.Append(lines[15]);
myBuilder.Append(PaintAllLine(lines[16], lines[17], myLengthList));
myBuilder.Append(lines[18]);
myBuilder.Append(" ");
myLengthList.Clear();
return myBuilder.ToString();
}
}
/**//// <summary>
/// 表格樣式
/// </summary>
public enum PaintLineStyle
...{
DoubleLine = 0,
SingleLine = 1,
DishedLine = 2
}
}