Changeset 14 for Common/CsvExport.cs


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

Legend:

Unmodified
Added
Removed
  • Common/CsvExport.cs

    r1 r14  
    11using System;
    2 using System.Collections.Generic;
    3 using System.Linq;
    42using System.Text;
    53using System.IO;
    64using System.Windows.Forms;
    7 using System.Threading.Tasks;
    85
    96namespace Common
     
    118    class CsvExport
    129    {
    13         public void ListViewToCSV(ListView listView, string filePath, bool includeHidden)
     10        public void ListViewToCsv(ListView listView, string filePath, bool includeHidden)
    1411        {
    1512            // Make header string
    1613            StringBuilder result = new StringBuilder();
    17             WriteCSVRow(result, listView.Columns.Count, i => includeHidden || listView.Columns[i].Width > 0, i => listView.Columns[i].Text);
     14            WriteCsvRow(result, listView.Columns.Count, i => includeHidden || listView.Columns[i].Width > 0, i => listView.Columns[i].Text);
    1815
    1916            // Export data rows
    2017            foreach (ListViewItem listItem in listView.Items)
    21                 WriteCSVRow(result, listView.Columns.Count, i => includeHidden || listView.Columns[i].Width > 0, i => listItem.SubItems[i].Text);
     18                WriteCsvRow(result, listView.Columns.Count, i => includeHidden || listView.Columns[i].Width > 0, i => listItem.SubItems[i].Text);
    2219
    2320            File.WriteAllText(filePath, result.ToString());
    2421        }
    2522
    26         public void DataGridViewToCSV(DataGridView dataGridView, string filePath, bool includeHidden)
     23        public void DataGridViewToCsv(DataGridView dataGridView, string filePath, bool includeHidden)
    2724        {
    2825            // Make header string
    2926            StringBuilder result = new StringBuilder();
    30             WriteCSVRow(result, dataGridView.Columns.Count, i => includeHidden || dataGridView.Columns[i].Width > 0, i => dataGridView.Columns[i].HeaderText);
     27            WriteCsvRow(result, dataGridView.Columns.Count, i => includeHidden || dataGridView.Columns[i].Width > 0, i => dataGridView.Columns[i].HeaderText);
    3128
    3229            // Export data rows
    3330            foreach (DataGridViewRow row in dataGridView.Rows)
    34                 WriteCSVRow(result, row.Cells.Count, i => includeHidden || row.Cells[i].Size.Width > 0, i => row.Cells[i].Value.ToString());
     31                WriteCsvRow(result, row.Cells.Count, i => includeHidden || row.Cells[i].Size.Width > 0, i => row.Cells[i].Value.ToString());
    3532
    3633            File.WriteAllText(filePath, result.ToString());
    3734        }       
    3835
    39         private void WriteCSVRow(StringBuilder result, int itemsCount, Func<int, bool> isColumnNeeded, Func<int, string> columnValue)
     36        private void WriteCsvRow(StringBuilder result, int itemsCount, Func<int, bool> isColumnNeeded, Func<int, string> columnValue)
    4037        {
    4138            bool isFirstTime = true;
Note: See TracChangeset for help on using the changeset viewer.