DataRow.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*********************************************************************************
  2. *Author: OnClick
  3. *Version: 0.0.1
  4. *UnityVersion: 2018.3.11f1
  5. *Date: 2019-09-08
  6. *Description: IFramework
  7. *History: 2018.11--
  8. *********************************************************************************/
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using System.Text;
  13. namespace IFramework.Serialization.DataTable
  14. {
  15. /// <summary>
  16. /// 数据行
  17. /// </summary>
  18. public class DataRow : IDataRow
  19. {
  20. /// <summary>
  21. /// 读取一行
  22. /// </summary>
  23. /// <param name="val">行String</param>
  24. /// <param name="headNames">标题行</param>
  25. /// <returns></returns>
  26. public List<DataColumn> ReadLine(string val, List<string> headNames)
  27. {
  28. List<string> strVals = SpilitRow(val);
  29. if (strVals.Count != headNames.Count) throw new Exception("Read Err Count Is different");
  30. List<DataColumn> cols = new List<DataColumn>();
  31. for (int i = 0; i < headNames.Count; i++)
  32. {
  33. cols.Add(new DataColumn()
  34. {
  35. value = strVals[i],
  36. headNameForRead = headNames[i]
  37. });
  38. }
  39. return cols;
  40. }
  41. /// <summary>
  42. /// 切割一行
  43. /// </summary>
  44. /// <param name="val"></param>
  45. /// <returns></returns>
  46. protected virtual List<string> SpilitRow(string val)
  47. {
  48. var list = val.Split(',').ToList();
  49. if (string.IsNullOrEmpty(list.Last()))
  50. {
  51. list.RemoveAt(list.Count - 1);
  52. }
  53. return list;
  54. }
  55. /// <summary>
  56. /// 读取标题行
  57. /// </summary>
  58. /// <param name="val">行String</param>
  59. /// <returns></returns>
  60. public virtual List<string> ReadHeadLine(string val)
  61. {
  62. List<string> headNames = val.Split(',').ToList();
  63. if (string.IsNullOrEmpty(headNames.Last()))
  64. {
  65. headNames.RemoveAt(headNames.Count - 1);
  66. }
  67. return headNames;
  68. }
  69. /// <summary>
  70. /// 写入一行
  71. /// </summary>
  72. /// <param name="cols">写入的信息</param>
  73. /// <returns></returns>
  74. public virtual void WriteLine(List<DataColumn> cols, StringBuilder builder)
  75. {
  76. for (int i = 0; i < cols.Count; i++)
  77. {
  78. var index = i;
  79. var c = cols[index];
  80. string val = c.value;
  81. val = val.Replace("\"", "\"\"");
  82. if (val.Contains(StringConvert.dot) || val.Contains("\"") || val.Contains('\r') || val.Contains('\n'))
  83. val = string.Format("\"{0}\"", val);
  84. builder.Append(val);
  85. builder.Append(StringConvert.dot.ToString());
  86. }
  87. }
  88. /// <summary>
  89. /// 写入标题行
  90. /// </summary>
  91. /// <param name="headNames">写入的标题</param>
  92. /// <returns></returns>
  93. public virtual void WriteHeadLine(List<string> headNames, StringBuilder builder)
  94. {
  95. for (int i = 0; i < headNames.Count; i++)
  96. {
  97. var index = i;
  98. var val = headNames[index];
  99. val = val.Replace("\"", "\"\"");
  100. if (val.Contains(StringConvert.dot) || val.Contains("\"") || val.Contains('\r') || val.Contains('\n'))
  101. val = string.Format("\"{0}\"", val);
  102. builder.Append(val);
  103. builder.Append(StringConvert.dot.ToString());
  104. }
  105. }
  106. }
  107. }