From 4d3298ba615427446c709094bd6f5bd157bf0585 Mon Sep 17 00:00:00 2001 From: xiaochanghai Date: Mon, 12 Aug 2024 14:54:30 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9F=B9=E8=AE=AD=E6=A8=A1=E5=9D=97Excel?= =?UTF-8?q?=E5=AF=BC=E5=87=BA=E6=A3=80=E6=A0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Ghre/Ghre_ExamRecordController.cs | 137 +++++++++++++++++- .../Ghre/IGhre_ExamRecordServices.cs | 8 +- .../View/Ghre/Ghre_ExamRecord.Dto.View.cs | 2 +- Tiobon.Core.Services/CommonServices.cs | 1 + .../Ghre/Ghre_ExamRecordServices.cs | 5 +- 5 files changed, 146 insertions(+), 7 deletions(-) diff --git a/Tiobon.Core.Api/Controllers/Ghre/Ghre_ExamRecordController.cs b/Tiobon.Core.Api/Controllers/Ghre/Ghre_ExamRecordController.cs index 80d945af..52138810 100644 --- a/Tiobon.Core.Api/Controllers/Ghre/Ghre_ExamRecordController.cs +++ b/Tiobon.Core.Api/Controllers/Ghre/Ghre_ExamRecordController.cs @@ -1,4 +1,10 @@ -namespace Tiobon.Core.Api.Controllers; +using System.Collections; +using System.Data; +using System.Reflection; +using SqlSugar; +using Tiobon.Core.Common.DB.Dapper; + +namespace Tiobon.Core.Api.Controllers; /// /// 考试记录(Controller) @@ -114,4 +120,133 @@ public class Ghre_ExamRecordController : BaseController> ExportExcel([FromBody] QueryExport body, long examId) + { + QueryBody filter = new QueryBody(); + filter.pageNum = 1; + filter.pageSize = 1000000; + filter.langId = body.langId; + + var condition = $"ExamId='{examId}'"; + if (body.exportSet.SelectRowKeys != null && body.exportSet.SelectRowKeys.Any()) + condition += $" AND Id IN({string.Join(",", body.exportSet.SelectRowKeys)})"; + var data = await _service.QueryFilterPage(filter, condition); + //var data = await _service.Query(examId, filter) QueryTotal(filter, condition); + + string sql = $@"SELECT * + FROM Ghrs_PageSettingQuery + WHERE IsEnable = 1 + AND PageNo = '{body.menuName}' + AND (defaultHidden = 'false' OR defaultHidden is null) + ORDER BY SortNo ASC"; + + var columns = DbAccess.QueryList(sql); + + var fieldDescs = new Dictionary(); + if (body.exportSet.ExFields.Any()) + body.exportSet.ExFields.ForEach(x => + { + if (columns.Any(o => o.field == x)) + fieldDescs.Add(x, columns.FirstOrDefault(o => o.field == x)?.label); + }); + else + fieldDescs = columns.ToDictionary(item => item.field, item => item.label); + var dt = ToDataTable1(data.result.DT_TableDataT1, fieldDescs, null); + // 获取所有列名 + var dtColumns = dt.Columns; + + var id = SnowFlakeSingle.instance.getID(); + var physicsPath = $"{Environment.CurrentDirectory}{Path.DirectorySeparatorChar}wwwroot"; + var path = $"{$"{Path.DirectorySeparatorChar}files{Path.DirectorySeparatorChar}export{Path.DirectorySeparatorChar}{id}{Path.DirectorySeparatorChar}"}"; + if (!Directory.Exists(physicsPath + path)) + Directory.CreateDirectory(physicsPath + path); + + path = path + body.exportSet.TitleName + ".xlsx"; + NPOIHelper.ExportExcel(dt, body.exportSet.TitleName, "sheet1", physicsPath + path); + + var result = new ExcelData(); + result.filePath = path; + result.fileName = body.exportSet.TitleName + ".xlsx"; + return ServiceResult.OprateSuccess("导出成功", result); + } + + public static DataTable ToDataTable1(List list, Dictionary fieldDescs = null, params string[] propertyName) + { + var (fields, colunms) = Sort(fieldDescs, null); + + List propertyNameList = new List(); + if (propertyName != null) + { + propertyNameList.AddRange(propertyName); + } + DataTable result = new DataTable(); + if (list.Count > 0) + { + PropertyInfo[] propertys = list[0].GetType().GetProperties(); + for (int i = 0; i < fields.Count; i++) + { + foreach (PropertyInfo pi in propertys) + { + if (propertyNameList.Count == 0) + { + //if (DBNull.Value.Equals(pi.PropertyType)) + //{ + // // pi.PropertyType = DateTime; + //} + Type colType = pi.PropertyType; + if (colType.IsGenericType && colType.GetGenericTypeDefinition() == typeof(Nullable<>)) + { + colType = colType.GetGenericArguments()[0]; + } + if (fields[i] == pi.Name) + { + if (!result.Columns.Contains(colunms[i])) + result.Columns.Add(colunms[i], colType); + } + //result.Columns.Add(pi.Name, pi.PropertyType); + } + else + { + if (propertyNameList.Contains(pi.Name)) + { + if (fields[i] == pi.Name) + result.Columns.Add(fields[i], pi.PropertyType); + } + } + } + } + for (int i = 0; i < list.Count; i++) + { + ArrayList tempList = new ArrayList(); + for (int j = 0; j < fields.Count; j++) + { + foreach (PropertyInfo pi in propertys) + { + if (fields[j] == pi.Name) + { + if (propertyNameList.Count == 0) + { + object obj = pi.GetValue(list[i], null); + tempList.Add(obj); + } + else + { + if (propertyNameList.Contains(pi.Name)) + { + object obj = pi.GetValue(list[i], null); + tempList.Add(obj); + } + } + } + } + } + object[] array = tempList.ToArray(); + result.LoadDataRow(array, true); + } + } + return result; + } + } \ No newline at end of file diff --git a/Tiobon.Core.IServices/Ghre/IGhre_ExamRecordServices.cs b/Tiobon.Core.IServices/Ghre/IGhre_ExamRecordServices.cs index 7fc136a2..34cd0e2e 100644 --- a/Tiobon.Core.IServices/Ghre/IGhre_ExamRecordServices.cs +++ b/Tiobon.Core.IServices/Ghre/IGhre_ExamRecordServices.cs @@ -8,11 +8,11 @@ namespace Tiobon.Core.IServices /// /// 考试记录(自定义服务接口) /// - public interface IGhre_ExamRecordServices :IBaseServices - { - Task> Query(string examId, QueryBody body); + public interface IGhre_ExamRecordServices : IBaseServices + { + Task> Query(string examId, QueryBody body); - Task ModifyAdjustScore(string examRecordId, EditGhre_ExamRecordInput edit); + Task ModifyAdjustScore(string examRecordId, EditGhre_ExamRecordInput edit); Task> ExtendAsync(long examRecordId); diff --git a/Tiobon.Core.Model/View/Ghre/Ghre_ExamRecord.Dto.View.cs b/Tiobon.Core.Model/View/Ghre/Ghre_ExamRecord.Dto.View.cs index daf1d742..7ca08d0d 100644 --- a/Tiobon.Core.Model/View/Ghre/Ghre_ExamRecord.Dto.View.cs +++ b/Tiobon.Core.Model/View/Ghre/Ghre_ExamRecord.Dto.View.cs @@ -43,7 +43,7 @@ public class Ghre_ExamRecordDto : Ghre_ExamRecord public string TitleName { get; set; } public DateTime? Indate { get; set; } public string InStatus { get; set; } - public string ExamDate { get; set; } + public string ExamDate1 { get; set; } public string StaffPhotoUrl { get; set; } public string ExamNo { get; set; } public string ExamName { get; set; } diff --git a/Tiobon.Core.Services/CommonServices.cs b/Tiobon.Core.Services/CommonServices.cs index 60eebe63..6feb3e30 100644 --- a/Tiobon.Core.Services/CommonServices.cs +++ b/Tiobon.Core.Services/CommonServices.cs @@ -1075,6 +1075,7 @@ public partial class CommonServices : BaseServices>, ICommon case "F_OnlineExamScores": result.DT_Procedure.QueryProcedure = dt.Rows[0]["QueryProcedure"].ToString(); result.DT_Procedure.QueryProcedure += "/" + param.jsonParam.MasterId; + result.DT_Procedure.ExportExcel += "/" + param.jsonParam.MasterId; toolbar = result.JM_PageControlT1.Toolbar.Where(x => x.fnKey == "DetailYN").FirstOrDefault(); if (toolbar != null) { toolbar.fnKey = "TBD3YN"; } diff --git a/Tiobon.Core.Services/Ghre/Ghre_ExamRecordServices.cs b/Tiobon.Core.Services/Ghre/Ghre_ExamRecordServices.cs index 8e2da1f4..86e900cd 100644 --- a/Tiobon.Core.Services/Ghre/Ghre_ExamRecordServices.cs +++ b/Tiobon.Core.Services/Ghre/Ghre_ExamRecordServices.cs @@ -119,8 +119,11 @@ public class Ghre_ExamRecordServices : BaseServices