CSVHelper.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Reflection;
  5. using UnityEngine;
  6. using System.IO;
  7. using System.Linq;
  8. using TMPro;
  9. public class CSVHelper
  10. {
  11. /// <summary>
  12. /// 从CSC文件读取信息
  13. /// </summary>
  14. /// <param name="tableName">文件路径名(需要有文件后缀)</param>
  15. /// <param name="headCount">表头</param>
  16. /// <returns></returns>
  17. public static List<T> ReadInfoFromCSV<T>(string filedPath, int headCount)
  18. {
  19. List<T> tmpMsg = new List<T>();
  20. if (!File.Exists(filedPath))
  21. {
  22. Debug.LogError($"{filedPath}不存在");
  23. return null;
  24. }
  25. string config = File.ReadAllText(filedPath);
  26. if (!string.IsNullOrEmpty(config))
  27. {
  28. string[] tmpLines = config.Split('\n');
  29. for (int i = 1; i < tmpLines.Length; i++)
  30. {
  31. if (string.IsNullOrEmpty(tmpLines[i])) continue;
  32. if (tmpLines[i].Contains('\r')) tmpLines[i] = tmpLines[i].Replace("\r", "");
  33. string[] columns = tmpLines[i].Split(',');
  34. FieldInfo[] tmpFieldInfos = typeof(T).GetFields();
  35. if (tmpFieldInfos.Length != columns.Length)
  36. {
  37. Debug.LogError(string.Format($"请检查{typeof(T).Name}与表格{filedPath}元素数量是否相同!!!!"));
  38. return null;
  39. }
  40. object tmpT = Activator.CreateInstance(typeof(T));
  41. for (int j = 0; j < tmpFieldInfos.Length; j++)
  42. {
  43. FieldInfo tmpFieldInfo = tmpFieldInfos[j];
  44. tmpFieldInfo.SetValue(tmpT, columns[j].ToString());
  45. }
  46. tmpMsg.Add((T)tmpT);
  47. }
  48. }
  49. return tmpMsg;
  50. }
  51. /// <summary>
  52. /// 将数据信息写入到CSV文件中
  53. /// </summary>
  54. /// <typeparam name="T">类型</typeparam>
  55. /// <param name="ts">信息</param>
  56. /// <param name="path">路径</param>
  57. public static bool WriteInfoToCsv<T>(string path,List<T> ts)
  58. {
  59. bool tmpResult = false ;
  60. using (FileStream fs = new FileStream(path, FileMode.Create))
  61. {
  62. using (StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.UTF8))
  63. {
  64. string tmpHead = string.Empty;
  65. FieldInfo[] tmpFieldInfos = typeof(T).GetFields();
  66. for (int i = 0;i < tmpFieldInfos.Length; i++)
  67. {
  68. tmpHead += tmpFieldInfos[i].Name;
  69. if (i < tmpFieldInfos.Length - 1) tmpHead += ",";
  70. }
  71. sw.WriteLine(tmpHead);
  72. for (int i = 0; i < ts.Count; i++)
  73. {
  74. string operationInfo = string.Empty;
  75. for (int j = 0;j < tmpFieldInfos.Length; j++)
  76. {
  77. operationInfo += tmpFieldInfos[j].GetValue(ts[i]).ToString();
  78. if (j < tmpFieldInfos.Length - 1) operationInfo += ",";
  79. }
  80. if (i == ts.Count - 1) sw.Write(operationInfo);
  81. else sw.WriteLine(operationInfo);
  82. }
  83. }
  84. }
  85. tmpResult = true;
  86. return tmpResult;
  87. }
  88. }