Last active
January 2, 2025 14:10
-
-
Save jsmarble/d24fe77446c246ec2401f5c62962c72d to your computer and use it in GitHub Desktop.
Extension method to turn a DataTable into a Markdown table
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static StringBuilder AsMarkdownTable(this DataTable source) | |
{ | |
StringBuilder sb = new StringBuilder(); | |
foreach (DataColumn column in source.Columns) | |
sb.Append("|").Append(column.ColumnName.Trim()); | |
sb.AppendLine("|"); | |
for (int i = 0; i < source.Columns.Count; i++) | |
sb.Append("|----"); | |
sb.AppendLine("|"); | |
foreach (DataRow row in source.Rows) | |
{ | |
foreach (object val in row.ItemArray) | |
sb.Append("|").Append(Convert.ToString(val).Trim()); | |
sb.AppendLine("|"); | |
} | |
return sb; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment