CommonController.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Threading.Tasks;
  6. using Bowin.Common.JSON;
  7. using Bowin.Common.Office;
  8. using Bowin.Common.ServiceToken.Permission;
  9. using Bowin.Common.WebModels;
  10. using YLShipBuildLandMap.Entity;
  11. using Microsoft.AspNetCore.Authorization;
  12. using Microsoft.AspNetCore.Http;
  13. using Microsoft.AspNetCore.Mvc;
  14. using Bowin.Common.Data;
  15. using System.Data;
  16. namespace YLShipBuildLandMap.Web.Controllers.Common
  17. {
  18. [Route("api/common/[controller]/[action]")]
  19. [Authorize]
  20. [ApiController]
  21. public class CommonController : ControllerBase
  22. {
  23. [HttpPost]
  24. public async Task<OkObjectResult> ImportExcel([FromForm] string columns, [FromForm] int firstRow, [FromForm] int firstColumn)
  25. {
  26. try
  27. {
  28. Dictionary<string, string> columnList = columns?.ToObject<Dictionary<string, string>>();
  29. var files = Request.Form.Files;
  30. if (files.Count == 0)
  31. {
  32. return Ok(new { success = false, msg = "没有上传的文件" });
  33. }
  34. Stream stream = files[0].OpenReadStream();
  35. var excel = new AsposeExcelHelper();
  36. var sepeatColumn = excel.RepeatColumn(stream, firstRow);
  37. if (!string.IsNullOrEmpty(sepeatColumn))
  38. {
  39. return Ok(new { success = false, msg = $"[{sepeatColumn}]列重复" });
  40. }
  41. var dt = excel.ImportAsString(stream, firstRow, firstColumn);
  42. if (columnList.Keys.Where(x => !dt.Columns.Contains(x)).Any())
  43. {
  44. var emptycolumns = columnList.Keys.Where(x => !dt.Columns.Contains(x)).ToArray();
  45. return Ok(new { success = false, msg = $"[{string.Join(",", emptycolumns)}]列不存在导入文件" });
  46. }
  47. if (columnList == null)
  48. {
  49. columnList = new Dictionary<string, string>();
  50. }
  51. columnList.Keys.ToList().ForEach(key => {
  52. dt.Columns[key].ColumnName = columnList[key];
  53. });
  54. return Ok(new { success = true, msg = "上传成功", item = dt });
  55. }
  56. catch (Exception ex)
  57. {
  58. return Ok(new { success = false, msg = ex.Message });
  59. throw;
  60. }
  61. }
  62. [HttpPost]
  63. public ResultMessage ImportErrorExport([FromBody] dynamic inputObj)
  64. {
  65. Dictionary<string, string> columnList = inputObj.columns.ToObject<Dictionary<string, string>>();
  66. List<Dictionary<string, object>> errorList = inputObj.items.ToObject<List<Dictionary<string, object>>>();
  67. int[] stringColumnList = inputObj.stringColumn?.ToObject<int[]>();
  68. var table = new DataTable();
  69. foreach (var item in columnList)
  70. {
  71. table.Columns.Add(item.Value);
  72. }
  73. errorList.ForEach(data =>
  74. {
  75. var row = table.NewRow();
  76. foreach (var item in columnList)
  77. {
  78. if (data.ContainsKey(item.Key))
  79. {
  80. row[item.Value] = data[item.Key];
  81. }
  82. }
  83. table.Rows.Add(row);
  84. });
  85. string[] liststring = columnList.Select(x => x.Value).ToArray();
  86. AsposeExcelHelper excel = new AsposeExcelHelper();
  87. var fileName = excel.ExportToFile(table, liststring, Configuration.Current.AppSettings.TemplatePhysicalPath, stringColumnList);
  88. return ResultMessage.Success(fileName);
  89. }
  90. }
  91. }