CsvUtils.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. using System;
  2. using System.ComponentModel;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. using System.Data;
  6. using System.Data.Common;
  7. using System.IO;
  8. using System.Net;
  9. using FastReport.Utils;
  10. using System.Globalization;
  11. using System.Collections;
  12. namespace FastReport.Data
  13. {
  14. internal static class CsvUtils
  15. {
  16. /// <summary>
  17. /// The default field name.
  18. /// </summary>
  19. public const string DEFAULT_FIELD_NAME = "Field";
  20. private static void DetermineTypes(List<string[]> lines, DataTable table, NumberFormatInfo numberInfo, NumberFormatInfo currencyInfo, DateTimeFormatInfo dateTimeInfo)
  21. {
  22. int intTemp;
  23. double doubleTemp;
  24. decimal decimalTemp;
  25. DateTime dateTemp;
  26. for (int i = 0; i < table.Columns.Count; i++)
  27. {
  28. // gather types here
  29. Dictionary<Type, int> types = new Dictionary<Type, int>();
  30. // check all values in the column
  31. for (int j = 0; j < lines.Count; j++)
  32. {
  33. if (i >= lines[j].Length)
  34. {
  35. // number of values is less than number of table columns. Reasons: wrong separator or bad-formed csv file?
  36. // just skip this line
  37. }
  38. else
  39. {
  40. string value = lines[j][i];
  41. if (!String.IsNullOrEmpty(value))
  42. {
  43. if (Int32.TryParse(value, out intTemp))
  44. {
  45. types[typeof(Int32)] = 1;
  46. }
  47. else if (value.Contains(currencyInfo.CurrencySymbol) && Decimal.TryParse(value, NumberStyles.Currency, currencyInfo, out decimalTemp))
  48. {
  49. types[typeof(Decimal)] = 1;
  50. }
  51. else if (Double.TryParse(value, NumberStyles.Number, numberInfo, out doubleTemp))
  52. {
  53. types[typeof(Double)] = 1;
  54. }
  55. else if (DateTime.TryParse(value, dateTimeInfo, DateTimeStyles.NoCurrentDateDefault, out dateTemp))
  56. {
  57. types[typeof(DateTime)] = 1;
  58. }
  59. else
  60. {
  61. types[typeof(String)] = 1;
  62. break;
  63. }
  64. }
  65. }
  66. }
  67. // cases allowed:
  68. // - single type -> the type
  69. // - mix of ints and doubles -> double
  70. // - all others should not be mixed -> string
  71. Type guessType = typeof(String);
  72. if (types.Count == 1)
  73. {
  74. // get a single value this way
  75. foreach (Type t in types.Keys)
  76. {
  77. guessType = t;
  78. }
  79. }
  80. else if (types.Count == 2 && types.ContainsKey(typeof(Int32)) && types.ContainsKey(typeof(Double)))
  81. {
  82. guessType = typeof(Double);
  83. }
  84. table.Columns[i].DataType = guessType;
  85. }
  86. }
  87. internal static List<string> ReadLines(CsvConnectionStringBuilder builder, int maxLines = 0)
  88. {
  89. if (String.IsNullOrEmpty(builder.CsvFile) || String.IsNullOrEmpty(builder.Separator))
  90. return null;
  91. ServicePointManager.Expect100Continue = true;
  92. ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
  93. WebRequest request;
  94. WebResponse response = null;
  95. try
  96. {
  97. // fix for datafile in current folder
  98. if (File.Exists(builder.CsvFile))
  99. builder.CsvFile = Path.GetFullPath(builder.CsvFile);
  100. Uri uri = new Uri(builder.CsvFile);
  101. if (uri.IsFile)
  102. {
  103. if (Config.ForbidLocalData)
  104. throw new Exception(Res.Get("ConnectionEditors,Common,OnlyUrlException"));
  105. request = (FileWebRequest)WebRequest.Create(uri);
  106. request.Timeout = 5000;
  107. response = (FileWebResponse)request.GetResponse();
  108. }
  109. else if (uri.OriginalString.StartsWith("http"))
  110. {
  111. request = (HttpWebRequest)WebRequest.Create(uri);
  112. request.Timeout = 5000;
  113. response = (HttpWebResponse)request.GetResponse();
  114. }
  115. else if (uri.OriginalString.StartsWith("ftp"))
  116. {
  117. request = (FtpWebRequest)WebRequest.Create(uri);
  118. request.Timeout = 5000;
  119. response = (FtpWebResponse)request.GetResponse();
  120. }
  121. }
  122. catch (Exception e)
  123. {
  124. throw e;
  125. }
  126. if (response == null)
  127. return null;
  128. List<string> lines = new List<string>();
  129. if (maxLines == 0)
  130. maxLines = int.MaxValue;
  131. // read lines
  132. using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(builder.Codepage)))
  133. {
  134. for (int i = 0; i < maxLines; i++)
  135. {
  136. string line = reader.ReadLine();
  137. // end of stream reached
  138. if (line == null)
  139. break;
  140. // skip empty lines
  141. if (!String.IsNullOrEmpty(line))
  142. lines.Add(line);
  143. }
  144. }
  145. return lines;
  146. }
  147. internal static DataTable CreateDataTable(CsvConnectionStringBuilder builder, List<string> rawLines)
  148. {
  149. if (rawLines == null)
  150. return null;
  151. // split each line to array of values
  152. List<string[]> lines = new List<string[]>();
  153. for (int i = 0; i < rawLines.Count; i++)
  154. {
  155. string line = rawLines[i];
  156. string[] values = line.Split(builder.Separator.ToCharArray());
  157. if (builder.RemoveQuotationMarks)
  158. {
  159. for (int j = 0; j < values.Length; j++)
  160. {
  161. values[j] = values[j].Trim("\"".ToCharArray());
  162. }
  163. }
  164. lines.Add(values);
  165. }
  166. if (lines.Count == 0)
  167. return null;
  168. NumberFormatInfo numberInfo = CultureInfo.GetCultureInfo(builder.NumberFormat)?.NumberFormat ?? CultureInfo.CurrentCulture.NumberFormat;
  169. NumberFormatInfo currencyInfo = CultureInfo.GetCultureInfo(builder.CurrencyFormat)?.NumberFormat ?? CultureInfo.CurrentCulture.NumberFormat;
  170. DateTimeFormatInfo dateTimeInfo = CultureInfo.GetCultureInfo(builder.DateTimeFormat)?.DateTimeFormat ?? CultureInfo.CurrentCulture.DateTimeFormat;
  171. // get table name from file name
  172. string tableName = Path.GetFileNameWithoutExtension(builder.CsvFile).Replace(".", "_");
  173. if (String.IsNullOrEmpty(tableName))
  174. {
  175. tableName = "Table";
  176. }
  177. DataTable table = new DataTable(tableName);
  178. string[] fields = lines[0];
  179. // create table columns
  180. for (int i = 0; i < fields.Length; i++)
  181. {
  182. DataColumn column = new DataColumn();
  183. column.DataType = typeof(string);
  184. // get field names from first string if needed
  185. string fieldName = fields[i].Replace("\t", "");
  186. if (builder.FieldNamesInFirstString && !table.Columns.Contains(fieldName))
  187. {
  188. column.ColumnName = fieldName;
  189. column.Caption = column.ColumnName;
  190. }
  191. else
  192. {
  193. column.ColumnName = DEFAULT_FIELD_NAME + i.ToString();
  194. column.Caption = column.ColumnName;
  195. }
  196. table.Columns.Add(column);
  197. }
  198. int startIndex = builder.FieldNamesInFirstString ? 1 : 0;
  199. // cast types of fields if needed
  200. if (builder.ConvertFieldTypes)
  201. {
  202. int number = lines.Count - startIndex;
  203. DetermineTypes(lines.GetRange(startIndex, number), table, numberInfo, currencyInfo, dateTimeInfo);
  204. }
  205. // add table rows
  206. for (int i = startIndex; i < lines.Count; i++)
  207. {
  208. if (lines[i].Length > 0)
  209. {
  210. // get values from the string
  211. fields = lines[i];
  212. // add a new row
  213. DataRow row = table.NewRow();
  214. int valuesCount = fields.Length < table.Columns.Count ? fields.Length : table.Columns.Count;
  215. for (int j = 0; j < valuesCount; j++)
  216. {
  217. string value = fields[j];
  218. if (!String.IsNullOrEmpty(value))
  219. {
  220. if (table.Columns[j].DataType == typeof(String))
  221. {
  222. row[j] = value;
  223. }
  224. else if (table.Columns[j].DataType == typeof(Int32))
  225. {
  226. row[j] = Int32.Parse(value);
  227. }
  228. else if (table.Columns[j].DataType == typeof(Decimal))
  229. {
  230. row[j] = Decimal.Parse(value, NumberStyles.Currency, currencyInfo);
  231. }
  232. else if (table.Columns[j].DataType == typeof(Double))
  233. {
  234. row[j] = Double.Parse(value, NumberStyles.Number, numberInfo);
  235. }
  236. else if (table.Columns[j].DataType == typeof(DateTime))
  237. {
  238. row[j] = DateTime.Parse(value, dateTimeInfo);
  239. }
  240. }
  241. }
  242. table.Rows.Add(row);
  243. }
  244. }
  245. return table;
  246. }
  247. }
  248. }