|
发表于 2014-4-3 16:40:11
|
显示全部楼层
湖北省十堰市
自己写的,记得给我精币
- public static string ToText(this ListView listView, bool ifExportColumn = false, bool aotoNum = false, char separator = ',')
- {
- StringBuilder sb = new StringBuilder();
- if (ifExportColumn == true)
- {
- if (listView.CheckBoxes == true) sb.Append("CheckBox,");
- foreach (ColumnHeader c in listView.Columns)
- {
- sb.Append(c.Text + separator);
- }
- sb.Remove(sb.Length - 1, 1);
- sb.Append("\r\n");
- }
- if (listView.Items.Count == 0) return sb.ToString();
- int s = aotoNum ? 1 : 0;
- int j;
- foreach (ListViewItem i in listView.Items)
- {
- if (listView.CheckBoxes) sb.Append((i.Checked ? "1" : "0") + separator);
- for (j = s; j <= listView.Columns.Count - 1; j++)
- {
- if (j >= i.SubItems.Count)
- break;
- sb.Append(i.SubItems[j].Text.Replace("\r\n", "[换行]").Trim() + separator);
- }
- sb.Remove(sb.Length - 1, 1);
- sb.Append("\r\n");
- }
- return sb.ToString().Trim();
- }
- public static void LoadFromText(this ListView listView, string text, bool ifHaveColumn = false, bool ifAutoNum = false, char separator = ',')
- {
- int j;
- var items = new List<ListViewItem>();
- listView.Items.Clear();
- if (ifHaveColumn) text = text.StrCut("\n", "");
- if (text.Trim() == "") return;
- string[] lines = text.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
- if (listView.Columns.Count == 0)
- {
- foreach(string x in lines[0].Split(separator))
- {
- listView.Columns.Add("");
- }
- }
- int loopIndex = 0;
- foreach (string line in lines)
- {
- loopIndex += 1;
- ListViewItem item = new ListViewItem();
- if (listView.CheckBoxes == true) item.Checked = ConvertHelper.ToBoolean(line.Split(separator)[0]);
- if (ifAutoNum == true) item.Text = (loopIndex).ToString();
- for (int x = 0; x <= listView.Columns.Count - 1; x++)
- {
- item.SubItems.Add("");
- }
- string[] colStrS = line.Split(separator);
- for (j = 0; j <= listView.Columns.Count - 1; j++)
- {
- if (j == 0 & ifAutoNum == true)
- {
- continue;
- }
- else if (listView.CheckBoxes == true)
- {
- if (j + 1 > colStrS.Length - 1)
- continue;
- item.SubItems[j].Text = colStrS[j + 1].Trim().Replace("[换行]", "\n");
- }
- else if (ifAutoNum == true)
- {
- if (j > colStrS.Length)
- continue;
- item.SubItems[j].Text = colStrS[j - 1].Trim().Replace("[换行]", "\n");
- }
- else
- {
- if (j >= colStrS.Length)
- continue;
- item.SubItems[j].Text = colStrS[j].Trim().Replace("[换行]", "\n");
- }
- }
- items.Add(item);
- }
- listView.Items.AddRange((ListViewItem[])items.ToArray());
- }
复制代码 |
|