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.
 
 
 
 
 
 

334 lines
11 KiB

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