From 933f8648605b2ba44fec645b7a5d2a6de93c2362 Mon Sep 17 00:00:00 2001 From: xiaochanghai Date: Thu, 4 Jul 2024 11:16:02 +0800 Subject: [PATCH] =?UTF-8?q?=E9=A2=98=E5=BA=93=E5=AF=BC=E5=85=A5=E6=A8=A1?= =?UTF-8?q?=E6=9D=BF=E4=B8=8B=E8=BD=BD=20=E8=AF=BE=E7=A8=8B=E5=88=97?= =?UTF-8?q?=E6=96=B0=E5=A2=9E=E5=8A=A8=E6=80=81=E7=BB=91=E5=AE=9A=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E5=BA=93=E8=AF=BE=E7=A8=8B=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Ghre/Ghre_ExamServices.cs | 26 ++--- .../Ghre/Ghre_QuestionServices.cs | 103 ++++++++++++++++++ 2 files changed, 116 insertions(+), 13 deletions(-) diff --git a/Tiobon.Core.Services/Ghre/Ghre_ExamServices.cs b/Tiobon.Core.Services/Ghre/Ghre_ExamServices.cs index 0cd04206..bf0409b3 100644 --- a/Tiobon.Core.Services/Ghre/Ghre_ExamServices.cs +++ b/Tiobon.Core.Services/Ghre/Ghre_ExamServices.cs @@ -236,17 +236,17 @@ public class Ghre_ExamServices : BaseServices(sql); - if (toolbars.Any(x => x.fnKey == "NewYN")) - Toolbar.Add(new Toolbar() - { - fnKey = "NewYN", - fnKeyValue = null, - fnTitle = "新增", - fnType = "table", - position = "left", - icon = "ghr-icon-add", - display = true - }); + //if (toolbars.Any(x => x.fnKey == "NewYN")) + // Toolbar.Add(new Toolbar() + // { + // fnKey = "NewYN", + // fnKeyValue = null, + // fnTitle = "新增", + // fnType = "table", + // position = "left", + // icon = "ghr-icon-add", + // display = true + // }); if (param.menuName == "F_ExamManageDraft") Toolbar.Add(new Toolbar() { @@ -270,8 +270,8 @@ public class Ghre_ExamServices : BaseServices x.fnKey == "ToExcelYN")) - Toolbar.Add(toolbars.First(x => x.fnKey == "ToExcelYN")); + //if (toolbars.Any(x => x.fnKey == "ToExcelYN")) + // Toolbar.Add(toolbars.First(x => x.fnKey == "ToExcelYN")); #endregion diff --git a/Tiobon.Core.Services/Ghre/Ghre_QuestionServices.cs b/Tiobon.Core.Services/Ghre/Ghre_QuestionServices.cs index 6ef47aa7..5a4aac3a 100644 --- a/Tiobon.Core.Services/Ghre/Ghre_QuestionServices.cs +++ b/Tiobon.Core.Services/Ghre/Ghre_QuestionServices.cs @@ -1,9 +1,17 @@ using System.Data; using System.Net; using AgileObjects.AgileMapper; +using AgileObjects.AgileMapper.Extensions; using Microsoft.AspNetCore.Http; using Newtonsoft.Json; using Newtonsoft.Json.Linq; +using NPOI.HSSF.UserModel; +using NPOI.OpenXmlFormats.Spreadsheet; +using NPOI.SS.Formula.Functions; +using NPOI.SS.UserModel; +using NPOI.SS.Util; +using NPOI.XSSF.UserModel; +using SharpCompress.Common; using SqlSugar; using Tiobon.Core.Common; using Tiobon.Core.Common.Caches; @@ -15,6 +23,7 @@ using Tiobon.Core.IServices; using Tiobon.Core.Model; using Tiobon.Core.Model.Models; using Tiobon.Core.Services.BASE; +using static Tiobon.Core.DataAccess.ReportHelper; namespace Tiobon.Core.Services; @@ -1052,9 +1061,103 @@ public class Ghre_QuestionServices : BaseServices(sql); + if (dataSourceLists.Any()) + { + var types = new List + { + "单选题", + "多选题", + "判断题", + "填空题", + "简答题", + }; + + types.ForEach(sheetName => + { + int sheetIndex = hssfworkbook.GetSheetIndex(sheetName); + if (sheetIndex >= 0) + { + sheet = hssfworkbook.GetSheetAt(sheetIndex); + + SetCellDropdownList(sheet, 0, 0, dataSourceLists.Select(x => x.name).ToArray()); + } + + }); + MemoryStream ms; + using (ms = new MemoryStream()) + { + hssfworkbook.Write(ms); + ms.Flush(); + hssfworkbook.Dispose(); + //ms.Position = 0; + //return ms; + } + using (FileStream fs = new FileStream(physicsPath + path + newFileName, FileMode.Create, FileAccess.Write)) + { + byte[] data = ms.ToArray(); + fs.Write(data, 0, data.Length); + fs.Flush(); + } + + } + + var physicsPath1 = physicsPath + path + fileName; + if (dataSourceLists.Any()) + physicsPath1 = physicsPath + path + newFileName; var result = ServiceResult.OprateSuccess(fileName, physicsPath + path + fileName); return result; } + + public static void SetCellDropdownList(ISheet sheet, int firstcol, int lastcol, string[] vals) + { + + //設置生成下拉框的行和列 + var cellRegions = new CellRangeAddressList(1, 65535, firstcol, lastcol); + IDataValidation validation = null; + + if (sheet.GetType().Name.Contains("XSSF")) // .xlsx + { + XSSFDataValidationHelper helper = new XSSFDataValidationHelper((XSSFSheet)sheet);//获得一个数据验证Helper + //IDataValidation + validation = helper.CreateValidation( + helper.CreateExplicitListConstraint(vals), cellRegions);//创建约束 + } + else // HSSF .xls + { + //設置 下拉框內容 + DVConstraint constraint = DVConstraint.CreateExplicitListConstraint(vals); + validation = new HSSFDataValidation(cellRegions, constraint); + + + /*綁定下拉框和作用區域,並設置錯誤提示信息 + HSSFDataValidation dataValidate = new HSSFDataValidation(cellRegions, constraint); + dataValidate.CreateErrorBox("輸入不合法", "請輸入或選擇下拉列表中的值。"); + dataValidate.ShowPromptBox = true; +*/ + } + + validation.CreateErrorBox("输入不合法", "请输入或选择下拉列表中的值。"); + validation.ShowPromptBox = true; + + sheet.AddValidationData(validation); + } + #endregion ///