ExcelFile.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Text;
  5. namespace Wayne.Lib.IO
  6. {
  7. /// <summary>
  8. /// Class that converts a string-table to and from the CSV format.
  9. /// </summary>
  10. public class ExcelFile
  11. {
  12. #region Fields
  13. private readonly IFileSupport fileSupport;
  14. #endregion
  15. #region Construction
  16. /// <summary>
  17. /// Constructor.
  18. /// </summary>
  19. /// <param name="serviceLocator">The service locator.</param>
  20. public ExcelFile(IServiceLocator serviceLocator)
  21. {
  22. fileSupport = serviceLocator.GetService<IFileSupport>();
  23. }
  24. #endregion
  25. #region Properties
  26. /// <summary>
  27. /// The file extension.
  28. /// </summary>
  29. public const string FileExtension = ".csv";
  30. /// <summary>
  31. /// The actual rows.
  32. /// </summary>
  33. public readonly List<List<string>> Rows = new List<List<string>>();
  34. #endregion
  35. #region Methods
  36. /// <summary>
  37. /// Save the current rows to a CSV file.
  38. /// </summary>
  39. /// <param name="fileName">The name of the file. The extension "CSV" will be added if missing.</param>
  40. public void SaveToFile(string fileName)
  41. {
  42. if (!Path.GetExtension(fileName).Equals(FileExtension, StringComparison.CurrentCultureIgnoreCase))
  43. fileName += FileExtension;
  44. using (Stream stream = fileSupport.Open(fileName, FileMode.Create, FileAccess.Write, FileShare.None))
  45. {
  46. using (StreamWriter streamWriter = new StreamWriter(stream))
  47. {
  48. foreach (List<string> row in Rows)
  49. streamWriter.WriteLine(GetCsvRowString(row));
  50. }
  51. }
  52. }
  53. /// <summary>
  54. /// Fill the rows with the content of the CSV file.
  55. /// </summary>
  56. /// <param name="fileName">The name of the file.</param>
  57. public void LoadFromFile(string fileName)
  58. {
  59. Rows.Clear();
  60. using (Stream stream = fileSupport.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
  61. {
  62. using (StreamReader streamReader = new StreamReader(stream))
  63. {
  64. while (!streamReader.EndOfStream)
  65. {
  66. Rows.Add(GetCsvRowStringList(streamReader.ReadLine()));
  67. }
  68. }
  69. }
  70. }
  71. private static string GetCsvRowString(IList<string> row)
  72. {
  73. List<string> stringItems = new List<string>();
  74. for (int i = 0; i < row.Count; i++)
  75. {
  76. if (row[i] != null)
  77. {
  78. string stringItem = row[i].Replace("\"", "\"\"").Replace("\r", "").Replace("\n", "");
  79. if ((stringItem.IndexOf(',') > -1) || (stringItem.IndexOf(';') > -1) ||
  80. (stringItem.IndexOf('"') > -1) ||
  81. stringItem.StartsWith(" ") || stringItem.EndsWith(" ") ||
  82. stringItem.StartsWith("\t") || stringItem.EndsWith("\t"))
  83. {
  84. stringItems.Add(string.Concat("\"", stringItem, "\""));
  85. }
  86. else
  87. stringItems.Add(stringItem);
  88. }
  89. }
  90. return string.Join(";", stringItems.ToArray());
  91. }
  92. private static List<string> GetCsvRowStringList(string row)
  93. {
  94. List<string> stringItems = new List<string>();
  95. bool inString = false;
  96. StringBuilder currentCell = new StringBuilder();
  97. for (int i = 0; i < row.Length; i++)
  98. {
  99. char thisChar = row[i];
  100. char nextChar = i < row.Length - 1 ? row[i + 1] : '\0';
  101. if (thisChar == '"')
  102. {
  103. if (inString)
  104. {
  105. if (nextChar == '"')
  106. {
  107. // Found a ...""... symbol
  108. currentCell.Append('"');
  109. i++;
  110. }
  111. else
  112. inString = false;
  113. }
  114. else
  115. inString = true;
  116. }
  117. else if (thisChar == ';')
  118. {
  119. if (inString)
  120. currentCell.Append(thisChar);
  121. else
  122. {
  123. stringItems.Add(currentCell.ToString());
  124. currentCell.Length = 0;
  125. }
  126. }
  127. else
  128. currentCell.Append(thisChar);
  129. }
  130. stringItems.Add(currentCell.ToString());
  131. return stringItems;
  132. }
  133. #endregion
  134. }
  135. }