using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Forms; namespace Yw.WinFrmUI.Phart { /// /// 剪切板文本辅助类 /// public class ClipboardCurveTextHelper { /// /// 获取Excel文本列表 /// /// public static List> GetExcelColTextList() { return GetColTextList("\t"); } /// /// 获取Excel double列表 /// /// public static List> GetExcelColDoubleList() { return GetColDoubleList(null, "\t"); } /// /// 获取纵向文本列表 /// /// /// public static List> GetColTextList(string separator = ",") { var text = Clipboard.GetText(); if (string.IsNullOrEmpty(text)) return null; var rows = text.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries).ToList(); if (rows.Count < 1) return null; var list = new List>(); foreach (var row in rows) { var cols = row.Split(new string[] { separator }, StringSplitOptions.None).ToList(); list.Add(cols); } return list; } /// /// 获取纵向文本列表 /// /// /// public static List> GetRowTextList(string separator = ",") { var rowsList = GetColTextList(separator); if (rowsList == null || rowsList.Count < 1) return null; var colsList = new List>(); for (int i = 0; i < rowsList.First().Count; i++) { var list = rowsList.Select(x => { if (x.Count <= i) return null; return x[i]; }).ToList(); colsList.Add(list); } return colsList; } /// /// 获取纵向Double 列表 /// /// /// public static List> GetColDoubleList(List neglectList, string separator = ",") { var text = Clipboard.GetText(); if (string.IsNullOrEmpty(text)) return null; if(neglectList != null && neglectList.Count>0) { neglectList.ForEach(x => { text = text.Replace(x, ""); }); } var rows = text.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries).ToList(); if (rows.Count < 1) return null; var list = new List>(); foreach (var row in rows) { var cols = row.Split(new string[] { separator }, StringSplitOptions.RemoveEmptyEntries); var col_doubles = cols.Select(x => { double? r; if (string.IsNullOrEmpty(x)) { r = null; } else { double d; if (double.TryParse(x, out d)) { r = d; } else { r = null; } } return r; }).ToList(); list.Add(col_doubles); } return list; } /// /// 获取横向Double 列表 /// /// /// public static List> GetRowDoubleText(List neglectList, string separator = ",") { var rowsList = GetColDoubleList(neglectList, separator); if (rowsList == null || rowsList.Count < 1) return null; var colsList = new List>(); for (int i = 0; i < rowsList.First().Count; i++) { var list = rowsList.Select(x => { if (x.Count <= i) return null; return x[i]; }).ToList(); colsList.Add(list); } return colsList; } } }