将数据表打印到.NET中的textbox/textfile
是否有预定义的或“简单”的方法将数据表写入文本文件或文本框控件(使用单空格字体),例如datatable.Print(): 第1列|第2列| --------|--------| v1 | v2| v3 | v4| v5 | v6| 编辑 这是一个初始版本(vb.net)-如果有人感兴趣或想构建自己的版本:将数据表打印到.NET中的textbox/textfile,.net,printing,datatable,writing,.net,Printing,Datatable,Writing,是否有预定义的或“简单”的方法将数据表写入文本文件或文本框控件(使用单空格字体),例如datatable.Print(): 第1列|第2列| --------|--------| v1 | v2| v3 | v4| v5 | v6| 编辑 这是一个初始版本(vb.net)-如果有人感兴趣或想构建自己的版本: Public Function BuildTable(ByVal dt As DataTable) As String Dim result As New StringBuilde
Public Function BuildTable(ByVal dt As DataTable) As String
Dim result As New StringBuilder
Dim widths As New List(Of Integer)
Const ColumnSeparator As Char = "|"c
Const HeadingUnderline As Char = "-"c
' determine width of each column based on widest of either column heading or values in that column
For Each col As DataColumn In dt.Columns
Dim colWidth As Integer = Integer.MinValue
For Each row As DataRow In dt.Rows
Dim len As Integer = row(col.ColumnName).ToString.Length
If len > colWidth Then
colWidth = len
End If
Next
widths.Add(CInt(IIf(colWidth < col.ColumnName.Length, col.ColumnName.Length + 1, colWidth + 1)))
Next
' write column headers
For Each col As DataColumn In dt.Columns
result.Append(col.ColumnName.PadLeft(widths(col.Ordinal)))
result.Append(ColumnSeparator)
Next
result.AppendLine()
' write heading underline
For Each col As DataColumn In dt.Columns
Dim horizontal As String = New String(HeadingUnderline, widths(col.Ordinal))
result.Append(horizontal.PadLeft(widths(col.Ordinal)))
result.Append(ColumnSeparator)
Next
result.AppendLine()
' write each row
For Each row As DataRow In dt.Rows
For Each col As DataColumn In dt.Columns
result.Append(row(col.ColumnName).ToString.PadLeft(widths(col.Ordinal)))
result.Append(ColumnSeparator)
Next
result.AppendLine()
Next
Return result.ToString()
End Function
Public Function BuildTable(ByVal dt作为DataTable)作为字符串
将结果变暗为新的StringBuilder
作为新列表的尺寸宽度(整数)
常量列分隔符为Char=“|”c
Const HeadingUnderline作为Char=“-”c
'根据列标题或列中值的最宽值确定每列的宽度
对于每个列,作为dt.列中的DataColumn
Dim COLWITH As Integer=Integer.MinValue
对于dt.行中作为DataRow的每一行
Dim len As Integer=行(col.ColumnName).ToString.Length
如果len>colWidth,则
colWidth=len
如果结束
下一个
宽度.Add(CInt(IIf(colWidth 不,没有。您必须自己进行格式化,或者找到第三方库自己进行格式化。您可以使用:
公共静态字符串打印表(此数据表dt)
{
DataTableReader dtReader=dt.CreateDataReader();
StringBuilder结果=新建StringBuilder();
while(dtReader.Read())
{
对于(int i=0;i
我建议你看看我的文章。
MyDataTableFormatter类包含用于将数据表格式化为基于字符的表(您的需求)、HTML表或流文档表的方法
您可以从我的网站下载包含该类的项目,但为了方便起见,我将在这里发布代码
/// <summary>
/// Gets a string representation of the <see cref="System.Data.DataTable" />.
/// </summary>
/// <remarks>The string representation should be displayed with a monospaced font.</remarks>
public static string GetStringRepresentation(DataTable dataTable)
{
if (dataTable == null)
throw new ArgumentNullException("'dataTable' cannot be null.");
StringWriter representationWriter = new StringWriter();
// First, set the width of every column to the length of its largest element.
int[] columnWidths = new int[dataTable.Columns.Count];
for (int columnIndex = 0; columnIndex < dataTable.Columns.Count; columnIndex++)
{
int headerWidth = dataTable.Columns[columnIndex].ColumnName.Length;
int longestElementWidth = dataTable.AsEnumerable()
.Select((row) => row[columnIndex].ToString().Length)
.Max();
columnWidths[columnIndex] = Math.Max(headerWidth, longestElementWidth);
}
// Next, write the table
// Write a horizontal line.
representationWriter.Write("+-");
for (int columnIndex = 0; columnIndex < dataTable.Columns.Count; columnIndex++)
{
for (int i = 0; i < columnWidths[columnIndex]; i++)
representationWriter.Write("-");
representationWriter.Write("-+");
if (columnIndex != dataTable.Columns.Count - 1)
representationWriter.Write("-");
}
representationWriter.WriteLine(" ");
// Print the headers
representationWriter.Write("| ");
for (int columnIndex = 0; columnIndex < dataTable.Columns.Count; columnIndex++)
{
string header = dataTable.Columns[columnIndex].ColumnName;
representationWriter.Write(header);
for (int blanks = columnWidths[columnIndex] - header.Length; blanks > 0; blanks--)
representationWriter.Write(" ");
representationWriter.Write(" | ");
}
representationWriter.WriteLine();
// Print another horizontal line.
representationWriter.Write("+-");
for (int columnIndex = 0; columnIndex < dataTable.Columns.Count; columnIndex++)
{
for (int i = 0; i < columnWidths[columnIndex]; i++)
representationWriter.Write("-");
representationWriter.Write("-+");
if (columnIndex != dataTable.Columns.Count - 1)
representationWriter.Write("-");
}
representationWriter.WriteLine(" ");
// Print the contents of the table.
for (int row = 0; row < dataTable.Rows.Count; row++)
{
representationWriter.Write("| ");
for (int column = 0; column < dataTable.Columns.Count; column++)
{
representationWriter.Write(dataTable.Rows[row][column]);
for (int blanks = columnWidths[column] - dataTable.Rows[row][column].ToString().Length;
blanks > 0; blanks--)
representationWriter.Write(" ");
representationWriter.Write(" | ");
}
representationWriter.WriteLine();
}
// Print a final horizontal line.
representationWriter.Write("+-");
for (int column = 0; column < dataTable.Columns.Count; column++)
{
for (int i = 0; i < columnWidths[column]; i++)
representationWriter.Write("-");
representationWriter.Write("-+");
if (column != dataTable.Columns.Count - 1)
representationWriter.Write("-");
}
representationWriter.WriteLine(" ");
return representationWriter.ToString();
}
//
///获取的字符串表示形式。
///
///字符串表示应以单间距字体显示。
公共静态字符串GetStringRepresentation(DataTable DataTable)
{
if(dataTable==null)
抛出新ArgumentNullException(“'dataTable'不能为null”);
StringWriter representationWriter=新建StringWriter();
//首先,将每列的宽度设置为其最大元素的长度。
int[]columnWidths=新int[dataTable.Columns.Count];
对于(int columnIndex=0;columnIndex行[columnIndex].ToString().Length)
.Max();
columnWidths[columnIndex]=数学最大值(headerWidth,longestElementWidth);
}
//接下来,写下表格
//写一条水平线。
representationWriter.Write(“+-”);
对于(int columnIndex=0;columnIndex0;blanks--)
代表作家。写(“”);
代表作家。写(“|”);
}
representationWriter.WriteLine();
//打印另一条水平线。
representationWriter.Write(“+-”);
对于(int columnIndex=0;columnIndex0;空白--)
代表作家。写(“”);
代表作家。写(“|”);
}
representationWriter.WriteLine();
}
//打印最后一条水平线。
representationWriter.Write(“+-”)
public static string PrintTable(this DataTable dt)
{
DataTableReader dtReader = dt.CreateDataReader();
StringBuilder result = new StringBuilder();
while (dtReader.Read())
{
for (int i = 0; i < dtReader.FieldCount; i++)
{
result.AppendFormat("{0} = {1}",
dtReader.GetName(i).Trim(),
dtReader.GetValue(i).ToString().Trim());
}
result.AppendLine();
}
dtReader.Close();
return result.ToString();
}
/// <summary>
/// Gets a string representation of the <see cref="System.Data.DataTable" />.
/// </summary>
/// <remarks>The string representation should be displayed with a monospaced font.</remarks>
public static string GetStringRepresentation(DataTable dataTable)
{
if (dataTable == null)
throw new ArgumentNullException("'dataTable' cannot be null.");
StringWriter representationWriter = new StringWriter();
// First, set the width of every column to the length of its largest element.
int[] columnWidths = new int[dataTable.Columns.Count];
for (int columnIndex = 0; columnIndex < dataTable.Columns.Count; columnIndex++)
{
int headerWidth = dataTable.Columns[columnIndex].ColumnName.Length;
int longestElementWidth = dataTable.AsEnumerable()
.Select((row) => row[columnIndex].ToString().Length)
.Max();
columnWidths[columnIndex] = Math.Max(headerWidth, longestElementWidth);
}
// Next, write the table
// Write a horizontal line.
representationWriter.Write("+-");
for (int columnIndex = 0; columnIndex < dataTable.Columns.Count; columnIndex++)
{
for (int i = 0; i < columnWidths[columnIndex]; i++)
representationWriter.Write("-");
representationWriter.Write("-+");
if (columnIndex != dataTable.Columns.Count - 1)
representationWriter.Write("-");
}
representationWriter.WriteLine(" ");
// Print the headers
representationWriter.Write("| ");
for (int columnIndex = 0; columnIndex < dataTable.Columns.Count; columnIndex++)
{
string header = dataTable.Columns[columnIndex].ColumnName;
representationWriter.Write(header);
for (int blanks = columnWidths[columnIndex] - header.Length; blanks > 0; blanks--)
representationWriter.Write(" ");
representationWriter.Write(" | ");
}
representationWriter.WriteLine();
// Print another horizontal line.
representationWriter.Write("+-");
for (int columnIndex = 0; columnIndex < dataTable.Columns.Count; columnIndex++)
{
for (int i = 0; i < columnWidths[columnIndex]; i++)
representationWriter.Write("-");
representationWriter.Write("-+");
if (columnIndex != dataTable.Columns.Count - 1)
representationWriter.Write("-");
}
representationWriter.WriteLine(" ");
// Print the contents of the table.
for (int row = 0; row < dataTable.Rows.Count; row++)
{
representationWriter.Write("| ");
for (int column = 0; column < dataTable.Columns.Count; column++)
{
representationWriter.Write(dataTable.Rows[row][column]);
for (int blanks = columnWidths[column] - dataTable.Rows[row][column].ToString().Length;
blanks > 0; blanks--)
representationWriter.Write(" ");
representationWriter.Write(" | ");
}
representationWriter.WriteLine();
}
// Print a final horizontal line.
representationWriter.Write("+-");
for (int column = 0; column < dataTable.Columns.Count; column++)
{
for (int i = 0; i < columnWidths[column]; i++)
representationWriter.Write("-");
representationWriter.Write("-+");
if (column != dataTable.Columns.Count - 1)
representationWriter.Write("-");
}
representationWriter.WriteLine(" ");
return representationWriter.ToString();
}
+------------------+----------------+-------------------+
| Item | Units in Stock | Unit Price |
+------------------+----------------+-------------------+
| Drilling machine | 1000 | $1,000,000 |
| Backpack | 320 | $24 |
| Chocolate bar | 100000 | $2.00000000000000 |
+------------------+----------------+-------------------+