SqeExcelService.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Text.RegularExpressions;
  8. using System.Threading.Tasks;
  9. using Business;
  10. using Business.Core.Utilities;
  11. using Business.Dto;
  12. using Business.ResourceExamineManagement;
  13. using NPOI.HSSF.UserModel;
  14. using NPOI.SS.UserModel;
  15. using NPOI.XSSF.UserModel;
  16. using Volo.Abp.Application.Services;
  17. using Volo.Abp.MultiTenancy;
  18. namespace Business.VSM
  19. {
  20. /// <summary>
  21. /// Sqe数据导入接口实现
  22. /// </summary>
  23. public class SqeExcelService : ApplicationService, ISqeExcelService
  24. {
  25. private readonly ICurrentTenant _currentTenant;
  26. //LoggerHelper WLLog;
  27. public SqeExcelService()
  28. {
  29. // WLLog = new LoggerHelper();
  30. }
  31. /// <summary>
  32. /// 导入Excel文件的sheetName的sheet页面到数据表
  33. /// </summary>
  34. /// <param name="filePath"></param>
  35. /// <param name="sheetName"></param>
  36. /// <returns></returns>
  37. public DataTable ImportExcelSheetToDataTable_test(string filePath, string sheetName)
  38. {
  39. FileStream file = null;
  40. DataTable dt = new DataTable();
  41. int index = 0;
  42. try
  43. {
  44. file = new FileStream(filePath, FileMode.Open, FileAccess.Read);
  45. //打开Excel对象
  46. IWorkbook workbook;
  47. IRow row;
  48. string fileExt = Path.GetExtension(filePath).ToLower();
  49. workbook = null;
  50. if (fileExt == ".xlsx")
  51. {
  52. workbook = new XSSFWorkbook(file);
  53. }
  54. else
  55. {
  56. if (fileExt == ".xls")
  57. {
  58. workbook = new HSSFWorkbook(file);
  59. }
  60. else
  61. {
  62. workbook = null;
  63. }
  64. }
  65. if (workbook == null) { return null; }
  66. //Excel的Sheet对象
  67. NPOI.SS.UserModel.ISheet sheet = workbook.GetSheet(sheetName);
  68. //获取Sheet的所有的行
  69. System.Collections.IEnumerator rows = sheet.GetRowEnumerator();
  70. //int colCountMax = 0;
  71. dt.TableName = sheetName;
  72. //判断是否拥有首行
  73. bool firstLine = true;
  74. //if (!haveColumnName)
  75. //{
  76. // firstLine = false;
  77. //}
  78. bool bDate = false;
  79. while (rows.MoveNext())
  80. {
  81. index++;
  82. bDate = false;
  83. if (fileExt == ".xlsx")
  84. {
  85. row = (XSSFRow)rows.Current;
  86. }
  87. else
  88. {
  89. row = (HSSFRow)rows.Current;
  90. }
  91. if (!firstLine)
  92. {
  93. //生成表结构
  94. //仅当首次调用时会进行循环生成
  95. for (int i = dt.Columns.Count; i < row.Cells.Count; i++)
  96. {
  97. dt.Columns.Add(Convert.ToChar(((int)'A') + i).ToString());
  98. }
  99. }
  100. else
  101. {
  102. //依据首行进行字段生成
  103. for (int i = dt.Columns.Count; i < row.Cells.Count; i++)
  104. {
  105. dt.Columns.Add(row.Cells[i].ToString());
  106. }
  107. }
  108. //没有首行,生成数据
  109. if (!firstLine)
  110. {
  111. DataRow dr = dt.NewRow();
  112. for (int i = 0; i < row.LastCellNum - 1; i++)
  113. {
  114. NPOI.SS.UserModel.ICell cell = row.GetCell(i);
  115. String s = "";
  116. if (cell != null)
  117. {
  118. switch (cell.CellType)
  119. {
  120. case NPOI.SS.UserModel.CellType.Unknown:
  121. break;
  122. case NPOI.SS.UserModel.CellType.Numeric:
  123. if (HSSFDateUtil.IsCellDateFormatted(cell))
  124. {
  125. if (index < 6)
  126. {
  127. s = cell.DateCellValue.ToString();
  128. }
  129. else
  130. {
  131. s = cell.ToString();
  132. }
  133. }
  134. else
  135. {
  136. s = cell.NumericCellValue.ToString();
  137. }
  138. break;
  139. case NPOI.SS.UserModel.CellType.String:
  140. s = cell.StringCellValue;
  141. break;
  142. case NPOI.SS.UserModel.CellType.Formula:
  143. if (i == 0 || i == 1)
  144. {
  145. s = cell.StringCellValue;
  146. }
  147. else
  148. {
  149. try
  150. {
  151. if (cell.CellFormula == "序号")
  152. {
  153. s = cell.ToString();
  154. }
  155. else
  156. {
  157. s = cell.NumericCellValue != null ? cell.NumericCellValue.ToString() : null;
  158. }
  159. }
  160. catch (Exception ex)
  161. {
  162. //WLLog.Error("Excel导入问题:", ex);
  163. new NLogHelper("SqeExcelService").WriteLog("ImportExcelSheetToDataTable", "Excel导入问题" + ex.Message, _currentTenant.Id.ToString());
  164. }
  165. }
  166. break;
  167. case NPOI.SS.UserModel.CellType.Blank:
  168. if (bDate)
  169. {
  170. //对于日期的特殊处理
  171. s = dr[i - 1].ToString();
  172. }
  173. else
  174. {
  175. s = cell.StringCellValue;
  176. }
  177. break;
  178. case NPOI.SS.UserModel.CellType.Boolean:
  179. break;
  180. case NPOI.SS.UserModel.CellType.Error:
  181. break;
  182. default:
  183. break;
  184. }
  185. }
  186. dr[i] = s;
  187. }
  188. dt.Rows.Add(dr);
  189. }
  190. firstLine = false;
  191. }
  192. return dt;
  193. }
  194. catch (Exception ex)
  195. {
  196. //WLLog.Error("Excel导入问题:", ex);
  197. new NLogHelper("SqeExcelService").WriteLog("ImportExcelSheetToDataTable", "Excel导入问题" + ex.Message, _currentTenant.Id.ToString());
  198. }
  199. finally
  200. {
  201. if (file != null)
  202. {
  203. file.Close();
  204. }
  205. }
  206. return dt;
  207. }
  208. public DataTable ImportExcelSheetToDataTable_process(string filePath, string sheetName)
  209. {
  210. FileStream file = null;
  211. DataTable dt = new DataTable();
  212. int index = 0;
  213. try
  214. {
  215. file = new FileStream(filePath, FileMode.Open, FileAccess.Read);
  216. //打开Excel对象
  217. IWorkbook workbook;
  218. IRow row;
  219. string fileExt = Path.GetExtension(filePath).ToLower();
  220. workbook = null;
  221. if (fileExt == ".xlsx")
  222. {
  223. workbook = new XSSFWorkbook(file);
  224. }
  225. else
  226. {
  227. if (fileExt == ".xls")
  228. {
  229. workbook = new HSSFWorkbook(file);
  230. }
  231. else
  232. {
  233. workbook = null;
  234. }
  235. }
  236. if (workbook == null) { return null; }
  237. //Excel的Sheet对象
  238. NPOI.SS.UserModel.ISheet sheet = workbook.GetSheet(sheetName);
  239. //获取Sheet的所有的行
  240. System.Collections.IEnumerator rows = sheet.GetRowEnumerator();
  241. //int colCountMax = 0;
  242. dt.TableName = sheetName;
  243. //判断是否拥有首行
  244. bool firstLine = true;
  245. //if (!haveColumnName)
  246. //{
  247. // firstLine = false;
  248. //}
  249. bool bDate = false;
  250. while (rows.MoveNext())
  251. {
  252. index++;
  253. bDate = false;
  254. if (fileExt == ".xlsx")
  255. {
  256. row = (XSSFRow)rows.Current;
  257. }
  258. else
  259. {
  260. row = (HSSFRow)rows.Current;
  261. }
  262. if (!firstLine)
  263. {
  264. //生成表结构
  265. //仅当首次调用时会进行循环生成
  266. for (int i = dt.Columns.Count; i < row.Cells.Count; i++)
  267. {
  268. dt.Columns.Add(Convert.ToChar(((int)'A') + i).ToString());
  269. }
  270. }
  271. else
  272. {
  273. //依据首行进行字段生成
  274. for (int i = dt.Columns.Count; i < row.Cells.Count; i++)
  275. {
  276. dt.Columns.Add(row.Cells[i].ToString());
  277. }
  278. }
  279. //没有首行,生成数据
  280. if (!firstLine)
  281. {
  282. DataRow dr = dt.NewRow();
  283. for (int i = 0; i < row.LastCellNum - 1; i++)
  284. {
  285. NPOI.SS.UserModel.ICell cell = row.GetCell(i);
  286. String s = "";
  287. if (cell != null)
  288. {
  289. switch (cell.CellType)
  290. {
  291. case NPOI.SS.UserModel.CellType.Unknown:
  292. break;
  293. case NPOI.SS.UserModel.CellType.Numeric:
  294. if (HSSFDateUtil.IsCellDateFormatted(cell))
  295. {
  296. if (index < 6)
  297. {
  298. s = cell.DateCellValue.ToString();
  299. }
  300. else
  301. {
  302. s = cell.ToString();
  303. }
  304. }
  305. else
  306. {
  307. s = cell.NumericCellValue.ToString();
  308. }
  309. break;
  310. case NPOI.SS.UserModel.CellType.String:
  311. s = cell.StringCellValue;
  312. break;
  313. case NPOI.SS.UserModel.CellType.Formula:
  314. try
  315. {
  316. if (cell.CellFormula == "序号")
  317. {
  318. s = cell.ToString();
  319. }
  320. else
  321. {
  322. s = cell.NumericCellValue != null ? cell.NumericCellValue.ToString() : null;
  323. }
  324. }
  325. catch (Exception ex)
  326. {
  327. //WLLog.Error("Excel导入问题:", ex);
  328. new NLogHelper("SqeExcelService").WriteLog("ImportExcelSheetToDataTable", "Excel导入问题" + ex.Message, _currentTenant.Id.ToString());
  329. }
  330. break;
  331. case NPOI.SS.UserModel.CellType.Blank:
  332. if (bDate)
  333. {
  334. //对于日期的特殊处理
  335. s = dr[i - 1].ToString();
  336. }
  337. else
  338. {
  339. s = cell.StringCellValue;
  340. }
  341. break;
  342. case NPOI.SS.UserModel.CellType.Boolean:
  343. break;
  344. case NPOI.SS.UserModel.CellType.Error:
  345. break;
  346. default:
  347. break;
  348. }
  349. }
  350. dr[i] = s;
  351. }
  352. dt.Rows.Add(dr);
  353. }
  354. firstLine = false;
  355. }
  356. return dt;
  357. }
  358. catch (Exception ex)
  359. {
  360. //WLLog.Error("Excel导入问题:", ex);
  361. new NLogHelper("SqeExcelService").WriteLog("ImportExcelSheetToDataTable", "Excel导入问题" + ex.Message, _currentTenant.Id.ToString());
  362. }
  363. finally
  364. {
  365. if (file != null)
  366. {
  367. file.Close();
  368. }
  369. }
  370. return dt;
  371. }
  372. /// <summary>
  373. /// 获取Excel文件Sheet名称列表,与Jet引擎不一致,获取的时候没有$多余字符。
  374. /// </summary>
  375. /// <param name="filePath"></param>
  376. /// <returns></returns>
  377. public List<String> GetSheetNames(string filePath)
  378. {
  379. List<String> tables = null;
  380. FileStream file = null;
  381. try
  382. {
  383. tables = new List<string>();
  384. file = new FileStream(filePath, FileMode.Open, FileAccess.Read);
  385. HSSFWorkbook hssfworkbook = new HSSFWorkbook(file);
  386. int i = 0;
  387. while (true)
  388. {
  389. try
  390. {
  391. String s = hssfworkbook.GetSheetName(i);
  392. if (s != null)
  393. {
  394. tables.Add(s);
  395. i++;
  396. }
  397. else
  398. {
  399. break;
  400. }
  401. }
  402. catch (Exception ex)
  403. {
  404. break;
  405. }
  406. }
  407. }
  408. catch (Exception ex)
  409. {
  410. //WLLog.Error("Excel导入问题:", ex);
  411. new NLogHelper("SqeExcelService").WriteLog("ImportExcelSheetToDataTable", "Excel导入问题"+ex.Message, _currentTenant.Id.ToString());
  412. }
  413. finally
  414. {
  415. if (file != null)
  416. {
  417. file.Close();
  418. }
  419. }
  420. return tables;
  421. }
  422. }
  423. }