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.

ExcelImportController.cs 13 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. using Learun.Application.Excel;
  2. using System.Collections.Generic;
  3. using System.Web.Mvc;
  4. using Learun.Util;
  5. using System.Data;
  6. using Learun.Application.Base.SystemModule;
  7. using System;
  8. using System.Drawing;
  9. using Learun.Application.TwoDevelopment.EducationalAdministration;
  10. namespace Learun.Application.Web.Areas.LR_SystemModule.Controllers
  11. {
  12. /// <summary>
  13. /// 版 本 Learun-ADMS-Ultimate V7.0.0 数字化智慧校园
  14. /// Copyright (c) 2013-2018 北京泉江科技有限公司
  15. /// 创建人:陈彬彬
  16. /// 日 期:2017.04.01
  17. /// 描 述:Excel导入管理
  18. /// </summary>
  19. public class ExcelImportController : MvcControllerBase
  20. {
  21. private ExcelImportIBLL excelImportIBLL = new ExcelImportBLL();
  22. private AnnexesFileIBLL annexesFileIBLL = new AnnexesFileBLL();
  23. private StuInfoFreshIBLL stuInfoFreshIBll = new StuInfoFreshBLL();
  24. #region 视图功能
  25. /// <summary>
  26. /// 导入模板管理页面
  27. /// </summary>
  28. /// <returns></returns>
  29. [HttpGet]
  30. public ActionResult Index()
  31. {
  32. return View();
  33. }
  34. /// <summary>
  35. /// 导入模板管理表单
  36. /// </summary>
  37. /// <returns></returns>
  38. [HttpGet]
  39. public ActionResult Form()
  40. {
  41. return View();
  42. }
  43. /// <summary>
  44. /// 设置字段属性
  45. /// </summary>
  46. /// <returns></returns>
  47. [HttpGet]
  48. public ActionResult SetFieldForm()
  49. {
  50. return View();
  51. }
  52. /// <summary>
  53. /// 导入页面
  54. /// </summary>
  55. /// <returns></returns>
  56. [HttpGet]
  57. public ActionResult ImportForm()
  58. {
  59. return View();
  60. }
  61. #endregion
  62. #region 获取数据
  63. /// <summary>
  64. /// 获取分页数据
  65. /// </summary>
  66. /// <param name="pagination">分页参数</param>
  67. /// <param name="queryJson">查询参数</param>
  68. /// <returns></returns>
  69. [HttpGet]
  70. [AjaxOnly]
  71. public ActionResult GetPageList(string pagination, string queryJson)
  72. {
  73. Pagination paginationobj = pagination.ToObject<Pagination>();
  74. var data = excelImportIBLL.GetPageList(paginationobj, queryJson);
  75. var jsonData = new
  76. {
  77. rows = data,
  78. total = paginationobj.total,
  79. page = paginationobj.page,
  80. records = paginationobj.records,
  81. };
  82. return JsonResult(jsonData);
  83. }
  84. /// <summary>
  85. /// 获取分页数据
  86. /// </summary>
  87. /// <param name="moduleId">功能模块主键</param>
  88. /// <returns></returns>
  89. [HttpGet]
  90. [AjaxOnly]
  91. public ActionResult GetList(string moduleId)
  92. {
  93. var data = excelImportIBLL.GetList(moduleId);
  94. return JsonResult(data);
  95. }
  96. /// <summary>
  97. /// 获取表单数据
  98. /// <param name="keyValue">主键</param>
  99. /// <summary>
  100. /// <returns></returns>
  101. [HttpGet]
  102. [AjaxOnly]
  103. public ActionResult GetFormData(string keyValue)
  104. {
  105. ExcelImportEntity entity = excelImportIBLL.GetEntity(keyValue);
  106. IEnumerable<ExcelImportFieldEntity> list = excelImportIBLL.GetFieldList(keyValue);
  107. var data = new
  108. {
  109. entity = entity,
  110. list = list
  111. };
  112. return JsonResult(data);
  113. }
  114. #endregion
  115. #region 提交数据
  116. /// <summary>
  117. /// 保存表单数据
  118. /// </summary>
  119. /// <param name="keyValue">主键</param>
  120. /// <param name="entity">实体</param>
  121. /// <returns></returns>
  122. [HttpPost]
  123. [ValidateAntiForgeryToken]
  124. [AjaxOnly]
  125. public ActionResult SaveForm(string keyValue, string strEntity, string strList)
  126. {
  127. ExcelImportEntity entity = strEntity.ToObject<ExcelImportEntity>();
  128. List<ExcelImportFieldEntity> filedList = strList.ToObject<List<ExcelImportFieldEntity>>();
  129. excelImportIBLL.SaveEntity(keyValue, entity, filedList);
  130. return Success("保存成功!");
  131. }
  132. /// <summary>
  133. /// 删除表单数据
  134. /// </summary>
  135. /// <param name="keyValue">主键</param>
  136. /// <returns></returns>
  137. [HttpPost]
  138. [AjaxOnly]
  139. public ActionResult DeleteForm(string keyValue)
  140. {
  141. excelImportIBLL.DeleteEntity(keyValue);
  142. return Success("删除成功!");
  143. }
  144. /// <summary>
  145. /// 更新表单数据
  146. /// </summary>
  147. /// <param name="keyValue">主键</param>
  148. /// <param name="entity">实体数据</param>
  149. /// <returns></returns>
  150. [HttpPost]
  151. [AjaxOnly]
  152. public ActionResult UpdateForm(string keyValue, ExcelImportEntity entity)
  153. {
  154. excelImportIBLL.UpdateEntity(keyValue, entity);
  155. return Success("操作成功!");
  156. }
  157. #endregion
  158. #region 扩展方法
  159. /// <summary>
  160. /// 下载文件
  161. /// </summary>
  162. /// <param name="fileId">文件id</param>
  163. /// <returns></returns>
  164. [HttpPost]
  165. [ValidateAntiForgeryToken]
  166. public void DownSchemeFile(string keyValue)
  167. {
  168. ExcelImportEntity templateInfo = excelImportIBLL.GetEntity(keyValue);
  169. IEnumerable<ExcelImportFieldEntity> fileds = excelImportIBLL.GetFieldList(keyValue);
  170. //设置导出格式
  171. ExcelConfig excelconfig = new ExcelConfig();
  172. excelconfig.FileName = Server.UrlDecode(templateInfo.F_Name) + ".xls";
  173. excelconfig.IsAllSizeColumn = true;
  174. excelconfig.ColumnEntity = new List<ColumnModel>();
  175. //表头
  176. DataTable dt = new DataTable();
  177. foreach (var col in fileds)
  178. {
  179. if (col.F_RelationType != 1 && col.F_RelationType != 4 && col.F_RelationType != 5 && col.F_RelationType != 6 && col.F_RelationType != 7)
  180. {
  181. excelconfig.ColumnEntity.Add(new ColumnModel()
  182. {
  183. Column = col.F_Name,
  184. ExcelColumn = col.F_ColName,
  185. Alignment = "center",
  186. Background = col.F_IsMandatory == true ? Color.Red : new Color()
  187. });
  188. dt.Columns.Add(col.F_Name, typeof(string));
  189. }
  190. }
  191. ExcelHelper.ExcelDownload(dt, excelconfig);
  192. }
  193. /// <summary>
  194. /// excel文件导入(通用)
  195. /// </summary>
  196. /// <param name="templateId">模板Id</param>
  197. /// <param name="fileId">文件主键</param>
  198. /// <param name="chunks">分片数</param>
  199. /// <param name="ext">文件扩展名</param>
  200. /// <returns></returns>
  201. [HttpPost]
  202. [ValidateAntiForgeryToken]
  203. public ActionResult ExecuteImportExcel(string templateId, string fileId, int chunks,string ext)
  204. {
  205. UserInfo userInfo = LoginUserInfo.Get();
  206. string path = annexesFileIBLL.SaveAnnexes(fileId, fileId + "."+ ext, chunks, userInfo);
  207. if (!string.IsNullOrEmpty(path))
  208. {
  209. DataTable dt = ExcelHelper.ExcelImport(path);
  210. string res = excelImportIBLL.ImportTable(templateId, fileId, dt);
  211. var data = new
  212. {
  213. Success = res.Split('|')[0],
  214. Fail = res.Split('|')[1]
  215. };
  216. return JsonResult(data);
  217. }
  218. else
  219. {
  220. return Fail("导入数据失败!");
  221. }
  222. }
  223. /// <summary>
  224. /// 工资导入
  225. /// </summary>
  226. /// <param name="templateId">模板Id</param>
  227. /// <param name="fileId">文件主键</param>
  228. /// <param name="chunks">分片数</param>
  229. /// <param name="ext">文件扩展名</param>
  230. /// <returns></returns>
  231. [HttpPost]
  232. [ValidateAntiForgeryToken]
  233. public ActionResult ExecuteImportSaralExcel(string fileId, int chunks,string ext)
  234. {
  235. UserInfo userInfo = LoginUserInfo.Get();
  236. string path = annexesFileIBLL.SaveAnnexes(fileId, fileId + "."+ ext, chunks, userInfo);
  237. if (!string.IsNullOrEmpty(path))
  238. {
  239. DataTable dt = ExcelHelper.ExcelImport(path);
  240. string res = excelImportIBLL.ImportSalaryInfo(dt,fileId);
  241. var data = new
  242. {
  243. Success = res.Split('|')[0],
  244. Fail = res.Split('|')[1]
  245. };
  246. return JsonResult(data);
  247. }
  248. else
  249. {
  250. return Fail("导入数据失败!");
  251. }
  252. }
  253. /// <summary>
  254. /// 金隅教师信息导入
  255. /// </summary>
  256. /// <param name="templateId">模板Id</param>
  257. /// <param name="fileId">文件主键</param>
  258. /// <param name="chunks">分片数</param>
  259. /// <param name="ext">文件扩展名</param>
  260. /// <returns></returns>
  261. [HttpPost]
  262. [ValidateAntiForgeryToken]
  263. public ActionResult EmpInfoImport(string fileId, int chunks,string ext)
  264. {
  265. UserInfo userInfo = LoginUserInfo.Get();
  266. string path = annexesFileIBLL.SaveAnnexes(fileId, fileId + "."+ ext, chunks, userInfo);
  267. if (!string.IsNullOrEmpty(path))
  268. {
  269. DataTable dt = ExcelHelper.ExcelImport(path);
  270. string res = excelImportIBLL.EmpInfoImport(dt,fileId);
  271. var data = new
  272. {
  273. Success = res.Split('|')[0],
  274. Fail = res.Split('|')[1]
  275. };
  276. return JsonResult(data);
  277. }
  278. else
  279. {
  280. return Fail("导入数据失败!");
  281. }
  282. }
  283. /// <summary>
  284. /// 导入缴费明细(西昌)
  285. /// </summary>
  286. /// <param name="templateId"></param>
  287. /// <param name="fileId"></param>
  288. /// <param name="chunks"></param>
  289. /// <param name="ext"></param>
  290. /// <returns></returns>
  291. [HttpPost]
  292. [ValidateAntiForgeryToken]
  293. public ActionResult ExecuteImportOrderDetail(string templateId, string fileId, int chunks, string ext)
  294. {
  295. UserInfo userInfo = LoginUserInfo.Get();
  296. string path = annexesFileIBLL.SaveAnnexes(fileId, fileId + "." + ext, chunks, userInfo);
  297. if (!string.IsNullOrEmpty(path))
  298. {
  299. DataTable dt = ExcelHelper.ExcelImport(path);
  300. string res = stuInfoFreshIBll.ImportOrderDetail(dt, fileId, templateId);
  301. var data = new
  302. {
  303. Success = res.Split('|')[0],
  304. Fail = res.Split('|')[1]
  305. };
  306. return JsonResult(data);
  307. }
  308. else
  309. {
  310. return Fail("导入数据失败!");
  311. }
  312. }
  313. /// <summary>
  314. /// 下载文件(导入文件未被导入的数据)
  315. /// </summary>
  316. /// <param name="fileId">文件id</param>
  317. /// <returns></returns>
  318. [HttpPost]
  319. [ValidateAntiForgeryToken]
  320. public void DownImportErrorFile(string fileId,string fileName)
  321. {
  322. //设置导出格式
  323. ExcelConfig excelconfig = new ExcelConfig();
  324. excelconfig.FileName = Server.UrlDecode("未导入错误数据【" + fileName + "】") + ".xls";
  325. excelconfig.IsAllSizeColumn = true;
  326. excelconfig.ColumnEntity = new List<ColumnModel>();
  327. //表头
  328. DataTable dt = excelImportIBLL.GetImportError(fileId);
  329. foreach (DataColumn col in dt.Columns)
  330. {
  331. if (col.ColumnName == "导入错误")
  332. {
  333. excelconfig.ColumnEntity.Add(new ColumnModel()
  334. {
  335. Column = col.ColumnName,
  336. ExcelColumn = col.ColumnName,
  337. Alignment = "center",
  338. Background = Color.Red
  339. });
  340. }
  341. else
  342. {
  343. excelconfig.ColumnEntity.Add(new ColumnModel()
  344. {
  345. Column = col.ColumnName,
  346. ExcelColumn = col.ColumnName,
  347. Alignment = "center",
  348. });
  349. }
  350. }
  351. ExcelHelper.ExcelDownload(dt, excelconfig);
  352. }
  353. #endregion
  354. }
  355. }