把数据表用网格的形式输出
2008-01-08 22:48
316 查看
以前看到一个朋友的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;
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
}
}
相关文章推荐
- 人工智能homework1_____c++ 回溯法解决数据(以命令行形式读入txt,输出在txt)
- php以excel的形式将数据输出
- ajax请求返回Json格式数据如何循环输出成table形式
- 如何将ajax请求返回的Json格式数据循环输出成table形式
- 如何将ajax请求返回的Json格式数据循环输出成table形式
- opencv学习(七)Mat函数(数据储存结构 /多种初始化创建方式/ 输出矩阵内容),,其他数据结构存放形式
- C语言---程序的一般形式、数据类型、常量变量、运算符、表达式、格式化输入输出
- 将数据以二进制形式输出
- 如何使用QString把不同类型的数据如何转换成字符串形式输出
- orcal 游标查询数据形式输出到控制台
- 处理tiff格式dem数据的方法——输出txt形式高程信息
- 三级次数据(一):在数据库使用存储过程输出为自联表形式
- 数据存储字典形式,并以字符串输出
- python中json格式数据输出实现方式(无unicode编码输出形式)
- 例子&nbsp;取数据&nbsp;以表格形式&nbsp;输出
- 在网页中以table形式动态输出数据库中数据的方式(1)html与php混写版
- C语言 将int型数据以二进制形式输出
- 将数据以二进制形式输出
- 输出一个整型数据的二进制形式
- ajax请求返回Json格式数据如何循环输出成table形式