using Microsoft.AspNetCore.Hosting;
|
using Microsoft.AspNetCore.Mvc;
|
using System;
|
using System.Collections.Generic;
|
using System.Data;
|
using System.IO;
|
using System.Linq;
|
using System.Threading.Tasks;
|
using VueWebCoreApi.DLL.BLL;
|
using VueWebCoreApi.Models;
|
using VueWebCoreApi.Models.SystemSetting;
|
using VueWebCoreApi.Tools;
|
|
namespace VueWebCoreApi.Controllers
|
{
|
[ApiExplorerSettings(GroupName = "Excel导入")]
|
[ApiController]
|
[Route("api/[controller]")]
|
[ChannelActionFilter]
|
public class ImportExcelController : Controller
|
{
|
private readonly IWebHostEnvironment _hostingEnvironment;
|
|
public ImportExcelController(IWebHostEnvironment hostingEnvironment)
|
{
|
_hostingEnvironment = hostingEnvironment;
|
}
|
|
#region[Excel导入模板列表]
|
/// <summary>
|
/// Excel导入模板列表
|
/// </summary>
|
/// <returns></returns>
|
[Route(template: "ExcelModelData")]
|
[HttpGet]
|
public JsonResult ExcelModelData()
|
{
|
ToMessage mes = new ToMessage();
|
List<ScoreReport> list = ExcelList.ExcelData();
|
mes.code = "200";
|
mes.data = list;
|
return Json(mes);
|
}
|
#endregion
|
|
#region [Excel导入模板下载]
|
/// <summary>
|
/// Excel导入模板下载
|
/// </summary>
|
/// <param name="FileCode"></param>
|
/// <returns></returns>
|
[Route(template: "DownLoadExcel")]
|
[HttpGet]
|
public JsonResult DownLoadExcel(string FileCode = null)
|
{
|
ToMessage mes = new ToMessage();
|
List<ScoreReport> list = ExcelList.ExcelData();
|
list = list.Where(s => s.FileCode == FileCode).ToList();
|
var filename = list[0].FileName + ".xls";
|
string fileip = AppSetting.GetAppSetting("FileIP");
|
var msg = fileip + "/Excel/" + filename;
|
mes.code = "200";
|
mes.data = msg;
|
return Json(mes);
|
|
//ToMessage mes = new ToMessage();
|
//List<ScoreReport> list = ExcelList.ExcelData();
|
//list = list.Where(s => s.FileCode == FileCode).ToList();
|
//var filename = list[0].FileName + ".xls";
|
//var wwwRootPath = _hostingEnvironment.WebRootPath;
|
//var fileDirectory = Path.Combine(wwwRootPath, "Excel"); // 替换为您的Excel文件所在目录
|
//var filePath = Path.Combine(fileDirectory, $"{filename}.xlsx"); // 假设文件名编码为文件名
|
|
//if (System.IO.File.Exists(filePath))
|
//{
|
// var fileInfo = new FileInfo(filePath);
|
// var result = new
|
// {
|
// FileName = fileInfo.Name,
|
// FilePath = filePath,
|
// FileSize = fileInfo.Length
|
// };
|
// return Json(result);
|
//}
|
//else
|
//{
|
// mes.code = "300";
|
// mes.data = "";
|
// mes.message = "文件不存在";
|
//}
|
//return Json(mes);
|
}
|
#endregion
|
|
#region [Excel导入数据提交、模板验证、数据导入]
|
/// <summary>
|
/// Excel导入数据提交、模板验证、数据导入
|
/// </summary>
|
/// <param name="myModel">提交数据</param>
|
/// <returns></returns>
|
[Route(template: "ExcelModelCheck")]
|
[HttpPost]
|
public JsonResult ExcelModelCheck([FromBody] ExceImport myModel)
|
{
|
string FileCode = myModel.FileCode;
|
List<DataTable> dataTable = myModel.TableData;
|
|
ExcelModelCheck list = new ExcelModelCheck();
|
list.json1 = ExcelCheck(dataTable, FileCode); //模板验证
|
if (list.json1.code == "301")
|
{
|
list.json1 = list.json1;
|
return Json(list);
|
}
|
list.json2 = ExcelCheckData(myModel); //数据验证
|
if (list.json2.code == "301")
|
{
|
list.json2 = list.json2;
|
return Json(list);
|
}
|
list.json3 = ExcelImportSubmit(dataTable, FileCode); //数据导入
|
if (list.json3.code == "300")
|
{
|
list.json3 = list.json3;
|
return Json(list);
|
}
|
return Json(list);
|
}
|
#endregion
|
|
#region [Excel导入模板验证]
|
/// <summary>
|
/// Excel导入模板验证
|
/// </summary>
|
/// <param name="dataTable">提交数据</param>
|
/// <param name="FileCode">文件编码</param>
|
/// <returns></returns>
|
[Route(template: "ExcelCheckUpload")]
|
[HttpGet]
|
[ApiExplorerSettings(IgnoreApi = true)]
|
public ToMessage ExcelCheck(List<DataTable> dataTable, string FileCode = null)
|
{
|
ToMessage mes = new ToMessage();
|
try
|
{
|
mes = ExcelCheckBLL.ExcelCheck(FileCode, dataTable);
|
if (mes.code == "301") //上传模板不是指定模板
|
{
|
return mes;
|
}
|
return mes;
|
}
|
catch (Exception e)
|
{
|
mes.code = "300";
|
mes.message = e.Message;
|
}
|
return mes;
|
}
|
#endregion
|
|
#region [Excel导入数据验证]
|
/// <summary>
|
/// Excel导入数据验证
|
/// </summary>
|
/// <param name="myModel">提交数据</param>
|
/// <returns></returns>
|
[Route(template: "ExcelCheckData")]
|
[HttpPost]
|
[ApiExplorerSettings(IgnoreApi = true)]
|
public ToMessage ExcelCheckData([FromBody] ExceImport myModel)
|
{
|
ToMessage mes = new ToMessage();
|
string message = "";
|
string StuCode = "";
|
int count = 0;
|
List<ExcelErro> list = new List<ExcelErro>();
|
string FileCode = myModel.FileCode;
|
List<DataTable> dataTable = myModel.TableData;
|
list = ExcelCheckBLL.ExcelCheckData(FileCode, dataTable, out StuCode, out message, out count);
|
mes.code = StuCode;
|
mes.message = message;
|
mes.count = count;
|
mes.data = list;
|
return mes;
|
}
|
#endregion
|
|
#region[Excel导入数据]
|
/// <summary>
|
/// Excel导入数据
|
/// </summary>
|
/// <param name="dataTable">提交数据</param>
|
/// <param name="FileCode">文件编码</param>
|
/// <returns></returns>
|
[Route(template: "ExcelImportSubmit")]
|
[HttpPost]
|
[ApiExplorerSettings(IgnoreApi = true)]
|
public ToMessage ExcelImportSubmit(List<DataTable> dataTable, string FileCode = null)
|
{
|
ToMessage mes = new ToMessage();
|
try
|
{
|
//var token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyY29kZSI6Ijk5OTkiLCJ1c2VybmFtZSI6Iuezu-e7n-euoeeQhuWRmCIsInN0b3JnX2NvZGUiOiIiLCJzdG9yZ19uYW1lIjoiIiwiaXNfc3lzdGVtX2FkbWluIjoiWSIsInJvbGVfY29kZSI6IiIsInJvbGVfZGF0YXBlcm1pc3Npb25zIjoiIiwidXNlcnR5cGUiOiJQQyIsInJlZGlza2V5IjoiU0ZNRVNMb2dpblVzZXJJRFBDOTk5OSIsInRpbWVvdXQiOiIyMDI0LTA4LTIzVDE2OjU5OjM1LjkzMDU5ODcrMDg6MDAifQ.XS4i_pJg4srt23pMoTJ8O0ZW8nq1dOkjQWOrkk6IfG4";
|
var token = HttpContext.Request.Headers["Token"].ToString();
|
User us = JwtTools.Denocode(token.ToString());
|
mes = ExcelCheckBLL.ExcelImportSubmit(FileCode, dataTable, us);
|
if (mes.code == "300")
|
{
|
return mes;
|
}
|
return mes;
|
}
|
catch (Exception e)
|
{
|
mes.code = "300";
|
mes.message = e.Message;
|
}
|
return mes;
|
}
|
#endregion
|
}
|
}
|