source: Common/Table.cs

Last change on this file was 14, checked in by chronos, 21 months ago
  • Modified: Various improvements.
File size: 3.8 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Windows.Forms;
5using System.Text;
6
7namespace Common
8{
9 public enum OutputFormat
10 {
11 Excel,
12 Plain,
13 Csv,
14 Html,
15 ListView
16 };
17
18 public class Row
19 {
20 public List<string> Cells = new List<string>();
21
22 public void AddCell(string text)
23 {
24 Cells.Add(text);
25 }
26 }
27
28 public class Table
29 {
30 public List<Row> Rows = new List<Row>();
31
32 public void Clear()
33 {
34 Rows.Clear();
35 }
36
37 public Row AddRow()
38 {
39 Row row = new Row();
40 Rows.Add(row);
41 return row;
42 }
43
44 public string GetOutputTabs()
45 {
46 StringBuilder output = new StringBuilder();
47 foreach (var row in Rows)
48 {
49 output.AppendLine(string.Join("\t", row.Cells));
50 }
51 return output.ToString();
52 }
53
54 public string GetOutputPlain()
55 {
56 StringBuilder output = new StringBuilder();
57 foreach (var row in Rows)
58 {
59 output.AppendLine(string.Join(Environment.NewLine, row.Cells));
60 output.AppendLine();
61 output.AppendLine("===========================");
62 output.AppendLine();
63 }
64 return output.ToString();
65 }
66
67 public string GetOutputCsv()
68 {
69 StringBuilder output = new StringBuilder();
70 foreach (var row in Rows)
71 {
72 output.AppendLine(string.Join(",", row.Cells.Select(x => "\"" + x + "\"")));
73 }
74 return output.ToString();
75 }
76
77 public string GetOutputHtml()
78 {
79 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 }
85 output.AppendLine("</table>");
86 return output.ToString();
87 }
88
89 public void GetOutputListView(ListView listView)
90 {
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
99 {
100 while (listView.Columns.Count < row.Cells.Count)
101 listView.Columns.Add("");
102 ListViewItem item = new ListViewItem();
103 int i = 0;
104 foreach (var cell in row.Cells)
105 {
106 if (i == 0) item.Text = cell;
107 else item.SubItems.Add(cell);
108 i++;
109 }
110 listView.Items.Add(item);
111 }
112 }
113
114 foreach (ColumnHeader column in listView.Columns)
115 {
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 "";
127 }
128 }
129}
Note: See TracBrowser for help on using the repository browser.