You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

211 lines
8.1 KiB

2 years ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.IO;
  7. using System.Text.RegularExpressions;
  8. using NPOI;
  9. using System.Data;
  10. using NPOI.SS.UserModel;
  11. using NPOI.XSSF.UserModel;
  12. using NPOI.HSSF.UserModel;
  13. namespace EasyBL
  14. {
  15. public class CSVHelper
  16. {
  17. /// <summary>
  18. /// ReadCSV:問卷調查轉換CSV資料
  19. /// </summary>
  20. /// <param name="i_sPath"></param>
  21. /// <param name="o_sMsg"></param>
  22. /// <returns></returns>
  23. public static List<Dictionary<string, object>> ReadCSV(string i_sPath, out string o_sMsg)
  24. {
  25. o_sMsg = null;
  26. List<Dictionary<string, object>> lCSVData = new List<Dictionary<string, object>>();
  27. do
  28. {
  29. try
  30. {
  31. if (string.IsNullOrEmpty(i_sPath))
  32. {
  33. o_sMsg = "FilePath Not Null";
  34. break;
  35. }
  36. if (File.Exists(i_sPath) == false)
  37. {
  38. o_sMsg = "File Not Exists";
  39. break;
  40. }
  41. List<string> lColumnName = new List<string>(); //欄位名稱
  42. Regex CSVParser = new Regex(",(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))");
  43. using (FileStream fs = new FileStream(i_sPath, FileMode.Open, FileAccess.Read))
  44. {
  45. StreamReader sr = new StreamReader(fs, Encoding.UTF8);
  46. string sLine = string.Empty;
  47. bool blHeaderRow = true;
  48. while ((sLine = sr.ReadLine()) != null)
  49. {
  50. if (blHeaderRow)
  51. {
  52. //第一列為標題列
  53. List<string> lTempHeaderRow = CSVParser.Split(sLine).ToList();
  54. Dictionary<string, int> dicRepeatCount = new Dictionary<string, int>();
  55. foreach (string HeaderColumn in lTempHeaderRow)
  56. {
  57. int iRepeatCount = 0;
  58. string sRpcHeaderColumn = HeaderColumn.Replace("\"", "");
  59. if (lColumnName.Contains(sRpcHeaderColumn))
  60. {
  61. if (dicRepeatCount.Keys.Contains(sRpcHeaderColumn) == false)
  62. {
  63. dicRepeatCount.Add(sRpcHeaderColumn, iRepeatCount);
  64. }
  65. else
  66. {
  67. iRepeatCount = dicRepeatCount[sRpcHeaderColumn];
  68. }
  69. iRepeatCount = iRepeatCount + 1;
  70. lColumnName.Add(sRpcHeaderColumn + iRepeatCount.ToString());
  71. dicRepeatCount[sRpcHeaderColumn] = iRepeatCount;
  72. }
  73. else
  74. {
  75. lColumnName.Add(sRpcHeaderColumn);
  76. }
  77. }
  78. blHeaderRow = false;
  79. }
  80. else
  81. {
  82. Dictionary<string, object> dicRowData = new Dictionary<string, object>();
  83. List<string> lRowData = CSVParser.Split(sLine).ToList(); //分割資料列
  84. for (int iColIdx = 0; iColIdx < lColumnName.Count; iColIdx++)
  85. {
  86. dicRowData.Add(lColumnName[iColIdx], lRowData[iColIdx]);
  87. }
  88. lCSVData.Add(dicRowData);
  89. }
  90. }
  91. }
  92. }
  93. catch (Exception ex)
  94. {
  95. o_sMsg = ex.Message;
  96. }
  97. } while (false);
  98. return lCSVData;
  99. }
  100. /// <summary>
  101. /// CRM_CustomerDataConvert:CRM客戶資料轉換
  102. /// </summary>
  103. /// <param name="i_sPath"></param>
  104. /// <param name="o_sMsg"></param>
  105. /// <returns></returns>
  106. public static DataTable CRM_CustomerDataConvert(string i_sPath, out string o_sMsg)
  107. {
  108. o_sMsg = null;
  109. FileStream fs = null;
  110. DataTable dt = new DataTable();
  111. IWorkbook wb = null;
  112. using (fs = File.Open(i_sPath, FileMode.Open, FileAccess.Read))
  113. {
  114. switch (Path.GetExtension(i_sPath).ToUpper())
  115. {
  116. case ".XLS":
  117. {
  118. wb = new HSSFWorkbook(fs);
  119. }
  120. break;
  121. case ".XLSX":
  122. {
  123. wb = new XSSFWorkbook(fs);
  124. }
  125. break;
  126. }
  127. if (wb.NumberOfSheets > 0)
  128. {
  129. ISheet sheet = wb.GetSheetAt(0);
  130. if (sheet == null)
  131. {
  132. sheet = wb.GetSheetAt(0);
  133. }
  134. IRow headerRow = sheet.GetRow(0);
  135. int index = 0;
  136. //處理標題列
  137. for (int i = headerRow.FirstCellNum; i < headerRow.LastCellNum; i++)
  138. {
  139. dt.Columns.Add(headerRow.GetCell(i).StringCellValue.Trim());
  140. }
  141. IRow row = null;
  142. DataRow dr = null;
  143. CellType ct = CellType.Blank;
  144. //標題列之後的資料
  145. for (int i = sheet.FirstRowNum + 1; i <= sheet.LastRowNum; i++)
  146. {
  147. dr = dt.NewRow();
  148. row = sheet.GetRow(i);
  149. if (row.Cells.All(d => d.CellType == CellType.Blank)) continue;
  150. if (row == null) continue;
  151. for (int j = row.FirstCellNum; j < row.LastCellNum; j++)
  152. {
  153. if (row.GetCell(j) == null)
  154. {
  155. dr[j] = DBNull.Value;
  156. }
  157. else
  158. {
  159. ct = row.GetCell(j).CellType;
  160. //如果此欄位格式為公式 則去取得CachedFormulaResultType
  161. switch (ct)
  162. {
  163. case CellType.Formula:
  164. ct = row.GetCell(j).CachedFormulaResultType;
  165. break;
  166. case CellType.Numeric:
  167. var da = DateUtil.IsCellDateFormatted(row.GetCell(j)) ? dr[j] = row.GetCell(j).DateCellValue : dr[j] = row.GetCell(j).NumericCellValue;
  168. dr[j] = da;
  169. break;
  170. case CellType.String:
  171. dr[j] = row.GetCell(j).ToString();
  172. break;
  173. default:
  174. dr[j] = row.GetCell(j).ToString().Replace("$", "");
  175. break;
  176. }
  177. }
  178. }
  179. dt.Rows.Add(dr);
  180. }
  181. }
  182. }
  183. return dt;
  184. }
  185. }
  186. }