以前看到一个朋友的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++)
...{
myObjs2[j] = myTable.Columns[j].ColumnName;
}
AddLine(myObjs2);
for (int i = 0; i < myTable.Rows.Count; i++)
...{
object[] myObjs = 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
}
}
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++)
...{
myObjs2[j] = myTable.Columns[j].ColumnName;
}
AddLine(myObjs2);
for (int i = 0; i < myTable.Rows.Count; i++)
...{
object[] myObjs = 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
}
}