static string toDisplay(object p, TableFormatOptions tf) { if (p == null) { return(string.Empty); } Type t = p.GetType(); if (t == typeof(decimal) || t == typeof(decimal?)) { p = string.Format("{0:##0.00}", p); } else if (t == typeof(double) || t == typeof(double?) || t == typeof(float) || t == typeof(float?)) { p = string.Format("{0:G}", p); } else if (t == typeof(bool) || t == typeof(bool?)) { p = string.Format("{0}", p); } else if (t == typeof(char) || t == typeof(char?)) { p = new string((char)p, 1); } else if (t.IsPrimitive) { p = string.Format("{0}", p); } string ret = Utils.TransformStr(Utils.To <string>(p), TransformRules.RemoveControl) ?? string.Empty; if ((tf & TableFormatOptions.MaxColWidthMask) != 0) { ret = Utils.FitWidth(ret, (int)(tf & TableFormatOptions.MaxColWidthMask), ((tf & TableFormatOptions.EllipsisStart) != 0) ? FitWidthOption.EllipsisStart : FitWidthOption.EllipsisEnd); } return(ret); }
static string toDisplay(object p, TableFormatOptions tf) { if (p == null) return string.Empty; Type t = p.GetType(); if (t == typeof(decimal) || t == typeof(decimal?)) p = string.Format("{0:##0.00}", p); else if (t == typeof(double) || t == typeof(double?) || t == typeof(float) || t == typeof(float?)) p = string.Format("{0:G}", p); else if (t == typeof(bool) || t == typeof(bool?)) p = string.Format("{0}", p); else if (t == typeof(char) || t == typeof(char?)) p = new string((char)p, 1); else if (t.IsPrimitive) p = string.Format("{0}", p); string ret = Utils.TransformStr(Utils.To<string>(p), TransformRules.RemoveControl) ?? string.Empty; if ((tf & TableFormatOptions.MaxColWidthMask) != 0) ret = Utils.FitWidth(ret, (int)(tf & TableFormatOptions.MaxColWidthMask), ((tf & TableFormatOptions.EllipsisStart) != 0) ? FitWidthOption.EllipsisStart : FitWidthOption.EllipsisEnd); return ret; }
/// Format some rows of the data table using the specified formatting options public static string ToTextTable(DataTable dt, IEnumerable datarows, TableFormatOptions options) { StringBuilder output = new StringBuilder(); if (dt.Columns.Count == 0) return string.Empty; int[] colMaxWidth = new int[dt.Columns.Count]; string[] colName = new string[dt.Columns.Count]; bool[] rightAlign = new bool[dt.Columns.Count]; int i = 0; for (i = 0; i < dt.Columns.Count; ++i) { colName[i] = dt.Columns[i].Caption ?? dt.Columns[i].ColumnName; colMaxWidth[i] = colName[i].Length + 2; } if ((options & TableFormatOptions.Records) != 0) { DataTable dt1 = new DataTable(dt.TableName + " (records)"); dt1.Columns.Add("Field"); dt1.Columns.Add("Value", typeof(object)); foreach (DataRow row in datarows) { dt1.Rows.Clear(); for (i = 0; i < colMaxWidth.Length; ++i) dt1.Rows.Add(colName[i], row[i]); output.AppendLine(ToTextTable(dt1, (options & ~TableFormatOptions.Records))); } return output.ToString(); } List<string[]> rows = new List<string[]>(); foreach (DataRow row in datarows) { string[] sv = new string[colMaxWidth.Length]; for (i = 0; i < colMaxWidth.Length; ++i) { var val = toDisplay(row[i], options); if ((options & TableFormatOptions.NumbersRight) != 0 && !rightAlign[i] && row[i] != null) { Type t = row[i].GetType(); if (t.IsPrimitive || t == typeof(decimal)) rightAlign[i] = true; } sv[i] = val; if (val.Length + 2 > colMaxWidth[i]) colMaxWidth[i] = val.Length + 2; } rows.Add(sv); } bool withLines = ((options & TableFormatOptions.Lines) != 0); bool withHeader = ((options & TableFormatOptions.Header) != 0); bool html = ((options & TableFormatOptions.Html) != 0); // Print header StringBuilder brk = new StringBuilder(); int totalWidth = 0; foreach (var col in colMaxWidth) { totalWidth += col + ((brk.Length == 0) ? 0 : 1); brk.Append(withLines ? "+" : (brk.Length == 0) ? "" : " "); brk.Append(new string('-', col)); } if (withLines) brk.Append('+'); brk.AppendLine(); if (html) output.Append(withLines ? "<table border='1' class='xshTable'>" : "<table class='xshTable'>"); else if (withLines) output.Append(brk.ToString()); if (withHeader) { if (html) output.Append("<thead><tr>"); // Print column names for (i = 0; i < colName.Length; ++i) { if (html) { output.Append("<th>"); output.Append(EscapeHtml(colName[i])); output.Append("</th>"); } else { string s = " " + colName[i] + " "; output.Append(withLines ? "|" : (i == 0) ? "" : " "); if (rightAlign[i]) output.Append(s.PadLeft(colMaxWidth[i])); else output.Append(s.PadRight(colMaxWidth[i])); } } if (html) output.AppendLine("</tr></thead>"); else { output.Append(withLines ? "|" : ""); output.AppendLine(); // Print separator again output.Append(brk.ToString()); } } // Print rows if (html) output.AppendLine("<tbody>"); foreach (var r in rows) { if (html) output.Append("<tr>"); for (i = 0; i < colName.Length; ++i) { string rw = r[i] ?? string.Empty; if (html) { output.Append(rightAlign[i] ? "<td style='text-align:right;'>" : "<td>"); output.Append(EscapeHtml(rw)); output.Append("</td>"); } else { output.Append(withLines ? "|" : (i == 0) ? "" : " "); output.Append(" "); if (rightAlign[i]) output.Append(rw.PadLeft(colMaxWidth[i] - 2)); else output.Append(rw.PadRight(colMaxWidth[i] - 2)); output.Append(" "); } } if (html) output.Append("</tr>"); else output.Append(withLines ? "|" : ""); output.AppendLine(); } if (html) output.AppendLine("</tbody>"); // Print footer - separator again bool withCount = ((options & TableFormatOptions.Count) != 0); if (withCount) { if (html) { output.AppendFormat("<tfoot><tr><th colspan='{0}'>{1} rows</th></tr></tfoot>", colMaxWidth.Length, rows.Count); output.AppendLine(); } else { if (withLines || withHeader) output.Append(brk.ToString()); output.Append(withLines ? "|" : ""); output.Append((" " + rows.Count + " rows").PadRight(totalWidth, ' ')); output.Append(withLines ? "|" : ""); if (withLines) output.AppendLine(); } } if (html) output.AppendLine("</table>"); else if (withLines) output.Append(brk.ToString()); return output.ToString(); }
/// Select and sort some rows from the data table, then format them with the specified formatting options public static string ToTextTable(DataTable dt, string select, string sort, TableFormatOptions options) { return ToTextTable(dt, dt.Select(select, sort), options); }
/// Convert DataTable to a text table with specified formatting options public static string ToTextTable(DataTable dt, TableFormatOptions options) { return ToTextTable(dt, dt.Rows, options); }
/// Convert all public properties to a text table with specified formatting options public static string ToTextTable(IEnumerable enumerable, TableFormatOptions options) { return ToTextTable(ToDataTable(enumerable), options); }
/// Convert rowset to a text table with the specified formating options public string ToTextTable(TableFormatOptions options, string columns, string where, string sort) { using (var dt = ToDataTable(columns, where, sort)) return(Utils.ToTextTable(dt, options)); }
/// Convert rowset to a text table with the specified formating options public string ToTextTable(TableFormatOptions options) { using (var dt = ToDataTable()) return(Utils.ToTextTable(dt, options)); }
/// Format some rows of the data table using the specified formatting options public static string ToTextTable(DataTable dt, IEnumerable datarows, TableFormatOptions options) { StringBuilder output = new StringBuilder(); if (dt.Columns.Count == 0) { return(string.Empty); } int[] colMaxWidth = new int[dt.Columns.Count]; string[] colName = new string[dt.Columns.Count]; bool[] rightAlign = new bool[dt.Columns.Count]; int i = 0; for (i = 0; i < dt.Columns.Count; ++i) { colName[i] = dt.Columns[i].Caption ?? dt.Columns[i].ColumnName; colMaxWidth[i] = colName[i].Length + 2; } if ((options & TableFormatOptions.Records) != 0) { DataTable dt1 = new DataTable(dt.TableName + " (records)"); dt1.Columns.Add("Field"); dt1.Columns.Add("Value", typeof(object)); foreach (DataRow row in datarows) { dt1.Rows.Clear(); for (i = 0; i < colMaxWidth.Length; ++i) { dt1.Rows.Add(colName[i], row[i]); } output.AppendLine(ToTextTable(dt1, (options & ~TableFormatOptions.Records))); } return(output.ToString()); } List <string[]> rows = new List <string[]>(); foreach (DataRow row in datarows) { string[] sv = new string[colMaxWidth.Length]; for (i = 0; i < colMaxWidth.Length; ++i) { var val = toDisplay(row[i], options); if ((options & TableFormatOptions.NumbersRight) != 0 && !rightAlign[i] && row[i] != null) { Type t = row[i].GetType(); if (t.IsPrimitive || t == typeof(decimal)) { rightAlign[i] = true; } } sv[i] = val; if (val.Length + 2 > colMaxWidth[i]) { colMaxWidth[i] = val.Length + 2; } } rows.Add(sv); } bool withLines = ((options & TableFormatOptions.Lines) != 0); bool withHeader = ((options & TableFormatOptions.Header) != 0); bool html = ((options & TableFormatOptions.Html) != 0); // Print header StringBuilder brk = new StringBuilder(); int totalWidth = 0; foreach (var col in colMaxWidth) { totalWidth += col + ((brk.Length == 0) ? 0 : 1); brk.Append(withLines ? "+" : (brk.Length == 0) ? "" : " "); brk.Append(new string('-', col)); } if (withLines) { brk.Append('+'); } brk.AppendLine(); if (html) { output.Append(withLines ? "<table border='1' class='xshTable'>" : "<table class='xshTable'>"); } else if (withLines) { output.Append(brk.ToString()); } if (withHeader) { if (html) { output.Append("<thead><tr>"); } // Print column names for (i = 0; i < colName.Length; ++i) { if (html) { output.Append("<th>"); output.Append(EscapeHtml(colName[i])); output.Append("</th>"); } else { string s = " " + colName[i] + " "; output.Append(withLines ? "|" : (i == 0) ? "" : " "); if (rightAlign[i]) { output.Append(s.PadLeft(colMaxWidth[i])); } else { output.Append(s.PadRight(colMaxWidth[i])); } } } if (html) { output.AppendLine("</tr></thead>"); } else { output.Append(withLines ? "|" : ""); output.AppendLine(); // Print separator again output.Append(brk.ToString()); } } // Print rows if (html) { output.AppendLine("<tbody>"); } foreach (var r in rows) { if (html) { output.Append("<tr>"); } for (i = 0; i < colName.Length; ++i) { string rw = r[i] ?? string.Empty; if (html) { output.Append(rightAlign[i] ? "<td style='text-align:right;'>" : "<td>"); output.Append(EscapeHtml(rw)); output.Append("</td>"); } else { output.Append(withLines ? "|" : (i == 0) ? "" : " "); output.Append(" "); if (rightAlign[i]) { output.Append(rw.PadLeft(colMaxWidth[i] - 2)); } else { output.Append(rw.PadRight(colMaxWidth[i] - 2)); } output.Append(" "); } } if (html) { output.Append("</tr>"); } else { output.Append(withLines ? "|" : ""); } output.AppendLine(); } if (html) { output.AppendLine("</tbody>"); } // Print footer - separator again bool withCount = ((options & TableFormatOptions.Count) != 0); if (withCount) { if (html) { output.AppendFormat("<tfoot><tr><th colspan='{0}'>{1} rows</th></tr></tfoot>", colMaxWidth.Length, rows.Count); output.AppendLine(); } else { if (withLines || withHeader) { output.Append(brk.ToString()); } output.Append(withLines ? "|" : ""); output.Append((" " + rows.Count + " rows").PadRight(totalWidth, ' ')); output.Append(withLines ? "|" : ""); if (withLines) { output.AppendLine(); } } } if (html) { output.AppendLine("</table>"); } else if (withLines) { output.Append(brk.ToString()); } return(output.ToString()); }
/// Select and sort some rows from the data table, then format them with the specified formatting options public static string ToTextTable(DataTable dt, string select, string sort, TableFormatOptions options) { return(ToTextTable(dt, dt.Select(select, sort), options)); }
/// Convert DataTable to a text table with specified formatting options public static string ToTextTable(DataTable dt, TableFormatOptions options) { return(ToTextTable(dt, dt.Rows, options)); }
/// Convert all public properties to a text table with specified formatting options public static string ToTextTable(IEnumerable enumerable, TableFormatOptions options) { return(ToTextTable(ToDataTable(enumerable), options)); }