Changeset 14 for Common/Table.cs


Ignore:
Timestamp:
Aug 2, 2022, 11:46:25 AM (22 months ago)
Author:
chronos
Message:
  • Modified: Various improvements.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • Common/Table.cs

    r13 r14  
    11using System;
    22using System.Collections.Generic;
    3 using System.Diagnostics.Eventing.Reader;
    43using System.Linq;
     4using System.Windows.Forms;
    55using System.Text;
    6 using System.Threading.Tasks;
    76
    87namespace Common
    98{
    10     public class Row
    11     {
    12         public List<string> cells = new List<string>();
    13 
    14         public void AddCell(string text)
    15         {
    16             cells.Add(text);
    17         }
    18     }
    19 
    209    public enum OutputFormat
    2110    {
    2211        Excel,
    2312        Plain,
    24         Csv
     13        Csv,
     14        Html,
     15        ListView
    2516    };
     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    }
    2627
    2728    public class Table
    2829    {
    29         public List<Row> rows = new List<Row>();
     30        public List<Row> Rows = new List<Row>();
    3031
    3132        public void Clear()
    3233        {
    33             rows.Clear();
     34            Rows.Clear();
    3435        }
    3536
     
    3738        {
    3839            Row row = new Row();
    39             rows.Add(row);
     40            Rows.Add(row);
    4041            return row;
    4142        }
     
    4445        {
    4546            StringBuilder output = new StringBuilder();
    46             foreach (var row in rows)
     47            foreach (var row in Rows)
    4748            {
    48                 output.AppendLine(string.Join("\t", row.cells));
     49                output.AppendLine(string.Join("\t", row.Cells));
    4950            }
    5051            return output.ToString();
     
    5455        {
    5556            StringBuilder output = new StringBuilder();
    56             foreach (var row in rows)
     57            foreach (var row in Rows)
    5758            {
    58                 output.AppendLine(string.Join(Environment.NewLine, row.cells));
     59                output.AppendLine(string.Join(Environment.NewLine, row.Cells));
    5960                output.AppendLine();
    6061                output.AppendLine("===========================");
     
    6768        {
    6869            StringBuilder output = new StringBuilder();
    69             foreach (var row in rows)
     70            foreach (var row in Rows)
    7071            {
    71                 output.AppendLine(string.Join(",", row.cells.Select(x => "\"" + x + "\"")));
     72                output.AppendLine(string.Join(",", row.Cells.Select(x => "\"" + x + "\"")));
    7273            }
    7374            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            }
    74118        }
    75119
     
    79123            else if (outputFormat == OutputFormat.Plain) return GetOutputPlain();
    80124            else if (outputFormat == OutputFormat.Csv) return GetOutputCsv();
     125            else if (outputFormat == OutputFormat.Html) return GetOutputHtml();
    81126            else return "";
    82127        }
Note: See TracChangeset for help on using the changeset viewer.