将 DataTable 打印到 .NET 中的文本框/文本文件
是否有预定义或“简单”的方法将数据表写入文本文件或 TextBox 控件(使用等宽字体),例如 DataTable.Print():
Column1| Column2| --------|--------| v1| v2| v3| v4| v5| v6|
编辑
这是初始版本 (vb.net) - 如果有人有兴趣或想要建立自己的:
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
Is there a predefined or "easy" method of writing a datatable to a text file or TextBox Control (With monospace font) such as DataTable.Print():
Column1| Column2| --------|--------| v1| v2| v3| v4| v5| v6|
Edit
Here's an initial version (vb.net) - in case anyone is interested or wants to build their own:
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我建议您查看我的文章DataTable Formatter。
我的 DataTableFormatter 类包含将 DataTable 格式化为基于字符的表(您的要求)、HTML 表或流文档表的方法。
您可以从我的网站下载包含该类的项目,但为了方便起见,我将在此处发布代码。
GetStringRepresentation(DataTable) 方法给出了这样的结果
我应该承认这个表样式是从 MySQL 命令行客户端欺骗的。
I recommend you check out my article DataTable Formatter.
My DataTableFormatter class contains methods for formatting DataTables as character-based tables (your requirement), HTML tables, or flow document tables.
You can download the project containing the class from my website, but for convenience I will post the code here.
The method GetStringRepresentation(DataTable) gives results such as this
I should admit that this table style is cheated from the MySQL command line client.
您可以使用 DataTableReader:
You could use a DataTableReader:
不,没有。 您必须自己进行格式化,或者找到可以自己完成此操作的第三方库。
No, there isn't. You will have to do the formatting yourself, or find a third-party library that will do this yourself.