Changeset 15 for Common/Table.cs
- Timestamp:
- Jun 18, 2024, 12:15:33 PM (5 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
Common/Table.cs
r14 r15 9 9 public enum OutputFormat 10 10 { 11 [FieldDisplayName("Excel (Tab separated)")] 11 12 Excel, 13 [FieldDisplayName("Plain text")] 12 14 Plain, 15 [FieldDisplayName("CSV")] 13 16 Csv, 17 [FieldDisplayName("HTML")] 14 18 Html, 15 ListView 19 [FieldDisplayName("ListView")] 20 ListView, 21 [FieldDisplayName("MediaWiki")] 22 MediaWiki, 23 [FieldDisplayName("JSON")] 24 Json, 25 [FieldDisplayName("XML")] 26 Xml 16 27 }; 17 28 18 public class Row29 public class TableRow 19 30 { 20 31 public List<string> Cells = new List<string>(); … … 26 37 } 27 38 39 public class TableColumn 40 { 41 public string Name; 42 public ColumnDataType DataType; 43 } 44 28 45 public class Table 29 46 { 30 public List<Row> Rows = new List<Row>(); 47 public string Title; 48 public List<TableRow> Rows = new List<TableRow>(); 49 public List<TableColumn> Columns = new List<TableColumn>(); 31 50 32 51 public void Clear() 33 52 { 53 Columns.Clear(); 34 54 Rows.Clear(); 35 55 } 36 56 37 public Row AddRow() 38 { 39 Row row = new Row(); 57 public void AddColumn(string name, ColumnDataType dataType = ColumnDataType.String) 58 { 59 Columns.Add(new TableColumn() { DataType = dataType, Name = name }); 60 } 61 62 public TableRow AddRow() 63 { 64 TableRow row = new TableRow(); 40 65 Rows.Add(row); 41 66 return row; … … 45 70 { 46 71 StringBuilder output = new StringBuilder(); 72 output.AppendLine(string.Join("\t", Columns.Select(x => x.Name))); 47 73 foreach (var row in Rows) 48 74 { … … 55 81 { 56 82 StringBuilder output = new StringBuilder(); 83 output.AppendLine(string.Join(Environment.NewLine, Columns.Select(x => x.Name))); 84 output.AppendLine(); 85 output.AppendLine("==========================="); 86 output.AppendLine(); 57 87 foreach (var row in Rows) 58 88 { … … 68 98 { 69 99 StringBuilder output = new StringBuilder(); 70 foreach (var row in Rows) 71 { 72 output.AppendLine(string.Join(",", row.Cells.Select(x => "\"" + x + "\""))); 100 output.AppendLine(string.Join(",", Columns.Select(x => "\"" + x.Name + "\""))); 101 foreach (var row in Rows) 102 { 103 output.AppendLine(string.Join(",", row.Cells.Select(x => "\"" + x.Replace("\"", "\"\"") + "\""))); 73 104 } 74 105 return output.ToString(); … … 78 109 { 79 110 StringBuilder output = new StringBuilder(); 80 output.AppendLine("<table border=\"1\">"); 81 foreach (var row in Rows) 82 { 83 output.AppendLine("<tr><td>" + string.Join("</td><td>", row.Cells.Select(x => x.Replace(Environment.NewLine, "<br/>"))) + "</td></tr>"); 84 } 111 output.AppendLine("<html>"); 112 if (!string.IsNullOrEmpty(Title)) 113 { 114 output.AppendLine(" <head>"); 115 output.AppendLine(" <title>" + Title + "</title>"); 116 output.AppendLine(" </head>"); 117 } 118 output.AppendLine(" <body>"); 119 output.AppendLine(" <table border=\"1\">"); 120 output.AppendLine(" <tr><th>" + string.Join("</th><th>", Columns.Select(x => x.Name.Replace(Environment.NewLine, "<br/>"))) + "</th></tr>"); 121 foreach (var row in Rows) 122 { 123 output.AppendLine(" <tr><td>" + string.Join("</td><td>", row.Cells.Select(x => x.Replace(Environment.NewLine, "<br/>"))) + "</td></tr>"); 124 } 125 output.AppendLine(" </table>"); 126 output.AppendLine(" </body>"); 127 output.AppendLine("</html>"); 128 return output.ToString(); 129 } 130 131 public string GetOutputXml() 132 { 133 StringBuilder output = new StringBuilder(); 134 output.AppendLine("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); 135 output.AppendLine("<table>"); 136 if (!string.IsNullOrEmpty(Title)) output.AppendLine(" <title>" + Title + "</title>"); 137 output.AppendLine(" <rows>"); 138 foreach (var row in Rows) 139 { 140 output.AppendLine(" <row>"); 141 int x = 0; 142 foreach (var cell in row.Cells) 143 { 144 string columnName = ""; 145 if (x < Columns.Count) columnName = Columns[x].Name.Replace("\"", "\\\""); 146 output.AppendLine(" <cell name=\"" + columnName + "\">" + cell + "</cell>"); 147 x++; 148 } 149 output.AppendLine(" </row>"); 150 } 151 output.AppendLine(" </rows>"); 85 152 output.AppendLine("</table>"); 86 153 return output.ToString(); 87 154 } 155 156 public string GetOutputMediaWiki() 157 { 158 StringBuilder output = new StringBuilder(); 159 output.AppendLine("{| class=\"wikitable sortable\""); 160 output.AppendLine("! " + string.Join(" !! ", Columns.Select(x => x.Name.Replace(Environment.NewLine, "<br/>")))); 161 if (!string.IsNullOrEmpty(Title)) output.AppendLine("|+ " + Title); 162 foreach (var row in Rows) 163 { 164 output.AppendLine("|-"); 165 output.AppendLine("| " + string.Join(" || ", row.Cells.Select(x => x.Replace(Environment.NewLine, "<br/>")))); 166 } 167 output.AppendLine("|}"); 168 return output.ToString(); 169 } 170 171 public string GetOutputJson() 172 { 173 StringBuilder output = new StringBuilder(); 174 output.AppendLine("["); 175 for (int y = 0; y < Rows.Count; y++) 176 { 177 output.AppendLine(" {"); 178 for (int x = 0; x < Rows[y].Cells.Count; x++) 179 { 180 string columnName = ""; 181 if (x < Columns.Count) columnName = Columns[x].Name.Replace("\"", "\\\""); 182 output.Append(" \"" + columnName + "\": \"" + Rows[y].Cells[x].Replace("\"", "\\\"") + "\""); 183 if (x < Rows[y].Cells.Count - 1) 184 { 185 output.Append(","); 186 } 187 output.AppendLine(); 188 } 189 output.Append(" }"); 190 if (y < Rows.Count - 1) 191 { 192 output.Append(","); 193 } 194 output.AppendLine(); 195 } 196 output.AppendLine("]"); 197 return output.ToString(); 198 } 88 199 89 200 public void GetOutputListView(ListView listView) 90 201 { 91 foreach (var row in Rows) 92 { 93 if (listView.Columns.Count == 0 && row.Cells.Count > 0) 94 { 95 while (listView.Columns.Count < row.Cells.Count) 96 listView.Columns.Add(row.Cells[listView.Columns.Count]); 97 } 98 else 202 listView.BeginUpdate(); 203 try 204 { 205 List<ListViewItem> listViewItems = new List<ListViewItem> 206 { 207 Capacity = Rows.Count 208 }; 209 210 while (listView.Columns.Count < Columns.Count) 211 listView.Columns.Add(Columns[listView.Columns.Count].Name); 212 213 for (int i = 0; i < Columns.Count; i++) 214 { 215 listView.Columns[i].Tag = i; 216 } 217 ListViewManager listViewManager = new ListViewManager(listView); 218 for (int i = 0; i < Columns.Count; i++) 219 { 220 listViewManager.SetColumnDataType(i, Columns[i].DataType); 221 } 222 223 foreach (var row in Rows) 99 224 { 100 225 while (listView.Columns.Count < row.Cells.Count) 101 226 listView.Columns.Add(""); 227 102 228 ListViewItem item = new ListViewItem(); 103 229 int i = 0; 104 foreach ( varcell in row.Cells)230 foreach (string cell in row.Cells) 105 231 { 106 232 if (i == 0) item.Text = cell; 107 233 else item.SubItems.Add(cell); 108 234 i++; 109 235 } 110 listView.Items.Add(item); 111 } 112 } 113 236 237 listViewItems.Add(item); 238 } 239 240 // Add prepared list view items into visible list view at once 241 listView.Items.Clear(); 242 listView.Items.AddRange(listViewItems.ToArray()); 243 244 // Update columns width 245 foreach (ColumnHeader column in listView.Columns) 246 { 247 column.Width = listView.Width / listView.Columns.Count; 248 } 249 } 250 finally 251 { 252 listView.EndUpdate(); 253 } 254 } 255 256 public string GetOutput(OutputFormat outputFormat) 257 { 258 switch (outputFormat) 259 { 260 case OutputFormat.Excel: 261 return GetOutputTabs(); 262 case OutputFormat.Plain: 263 return GetOutputPlain(); 264 case OutputFormat.Csv: 265 return GetOutputCsv(); 266 case OutputFormat.Html: 267 return GetOutputHtml(); 268 case OutputFormat.MediaWiki: 269 return GetOutputMediaWiki(); 270 case OutputFormat.Json: 271 return GetOutputJson(); 272 case OutputFormat.Xml: 273 return GetOutputXml(); 274 default: 275 return ""; 276 } 277 } 278 279 public static string GetFileExt(OutputFormat outputFormat) 280 { 281 string[] outputFormatFileExt = new string[] { ".txt", ".txt", ".csv", ".htm", ".txt", ".txt", ".json", ".xml"}; 282 283 return outputFormatFileExt[(int)outputFormat]; 284 } 285 286 public void LoadFromListView(ListView listView) 287 { 288 Clear(); 289 Columns.Capacity = listView.Columns.Count; 114 290 foreach (ColumnHeader column in listView.Columns) 115 291 { 116 column.Width = listView.Width / listView.Columns.Count; 117 } 118 } 119 120 public string GetOutput(OutputFormat outputFormat) 121 { 122 if (outputFormat == OutputFormat.Excel) return GetOutputTabs(); 123 else if (outputFormat == OutputFormat.Plain) return GetOutputPlain(); 124 else if (outputFormat == OutputFormat.Csv) return GetOutputCsv(); 125 else if (outputFormat == OutputFormat.Html) return GetOutputHtml(); 126 else return ""; 292 AddColumn(column.Text); 293 } 294 295 Rows.Capacity = listView.Items.Count; 296 for (int i = 0; i < listView.Items.Count; i++) 297 { 298 TableRow row = AddRow(); 299 row.Cells.Capacity = listView.Items[i].SubItems.Count; 300 foreach (ListViewItem.ListViewSubItem subItem in listView.Items[i].SubItems) 301 { 302 row.AddCell(subItem.Text); 303 } 304 } 127 305 } 128 306 }
Note:
See TracChangeset
for help on using the changeset viewer.