Changeset 14 for Common/CsvExport.cs
- Timestamp:
- Aug 2, 2022, 11:46:25 AM (2 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
Common/CsvExport.cs
r1 r14 1 1 using System; 2 using System.Collections.Generic;3 using System.Linq;4 2 using System.Text; 5 3 using System.IO; 6 4 using System.Windows.Forms; 7 using System.Threading.Tasks;8 5 9 6 namespace Common … … 11 8 class CsvExport 12 9 { 13 public void ListViewToC SV(ListView listView, string filePath, bool includeHidden)10 public void ListViewToCsv(ListView listView, string filePath, bool includeHidden) 14 11 { 15 12 // Make header string 16 13 StringBuilder result = new StringBuilder(); 17 WriteC SVRow(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); 18 15 19 16 // Export data rows 20 17 foreach (ListViewItem listItem in listView.Items) 21 WriteC SVRow(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); 22 19 23 20 File.WriteAllText(filePath, result.ToString()); 24 21 } 25 22 26 public void DataGridViewToC SV(DataGridView dataGridView, string filePath, bool includeHidden)23 public void DataGridViewToCsv(DataGridView dataGridView, string filePath, bool includeHidden) 27 24 { 28 25 // Make header string 29 26 StringBuilder result = new StringBuilder(); 30 WriteC SVRow(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); 31 28 32 29 // Export data rows 33 30 foreach (DataGridViewRow row in dataGridView.Rows) 34 WriteC SVRow(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()); 35 32 36 33 File.WriteAllText(filePath, result.ToString()); 37 34 } 38 35 39 private void WriteC SVRow(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) 40 37 { 41 38 bool isFirstTime = true;
Note:
See TracChangeset
for help on using the changeset viewer.