From 9f33b5009bdd369fb577dbcd5cfb0104c4ff6c7b Mon Sep 17 00:00:00 2001 From: zeyu xu <286513187@qq.com> Date: Fri, 5 Apr 2024 19:50:58 +0800 Subject: [PATCH] =?UTF-8?q?add=20excel=20=E5=AF=BC=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/AntSK.Domain/AntSK.Domain.xml | 78 +++++++++++++ src/AntSK.Domain/Common/Excel/ExeclHelper.cs | 106 ------------------ .../Domain/Model/Excel/KMSExcelModel.cs | 17 +++ .../Domain/Model/ImportKMSTaskReq.cs | 3 +- .../Domain/Service/ImportKMSService.cs | 20 +++- src/AntSK/Controllers/FileController.cs | 16 ++- src/AntSK/Pages/KmsPage/KmsDetail.razor | 2 +- src/AntSK/Pages/KmsPage/KmsDetail.razor.cs | 2 +- 8 files changed, 133 insertions(+), 111 deletions(-) create mode 100644 src/AntSK.Domain/Domain/Model/Excel/KMSExcelModel.cs diff --git a/src/AntSK.Domain/AntSK.Domain.xml b/src/AntSK.Domain/AntSK.Domain.xml index f2a333b..b94b693 100644 --- a/src/AntSK.Domain/AntSK.Domain.xml +++ b/src/AntSK.Domain/AntSK.Domain.xml @@ -69,6 +69,84 @@ + + + 将excel导入到datatable + + excel路径 + 第一行是否是列名 + 返回datatable + + + + 将excel导入到datatable + + 流 + 第一行是否是列名 + + + + + excel转list + + + + + + + + excel转list-根据sheetName得到List + + + + + + + + + List导出excel 二进制流 + + 实体 + List + sheetname 可不填,默认Sheet0 + + + + + Dt导出excel 二进制流 + + datatable + strFile + + + + + List写入excel + + + + 路径 + + + + + + dt写入excel + + datatable + 路径 + + + + + 设置单元格下拉框(除去标题行) + + + + + + + AI类型 diff --git a/src/AntSK.Domain/Common/Excel/ExeclHelper.cs b/src/AntSK.Domain/Common/Excel/ExeclHelper.cs index f8af6fe..34b4938 100644 --- a/src/AntSK.Domain/Common/Excel/ExeclHelper.cs +++ b/src/AntSK.Domain/Common/Excel/ExeclHelper.cs @@ -764,112 +764,6 @@ namespace AntSK.Domain } } - - - /// - /// List导出excel 二进制流(服务成本/项目收入下载列设置下拉框) - /// - /// 实体 - /// List - /// sheetname 可不填,默认Sheet0 - /// - public static byte[] ListToExcelHasDDL(T[] data, List feeLevelList, List feeChildList, List dicList, int[] colindex, string sheetName = "Sheet0") - { - IWorkbook workbook = null; - IRow row = null; - ISheet sheet = null; - ICell cell = null; - var propertyInfos = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance) - .OrderBy(p => p.GetCustomAttribute().Order).ToArray(); - workbook = new XSSFWorkbook(); - sheet = workbook.CreateSheet(sheetName);//创建一个名称为Sheet0的表 - int rowCount = data.Count();//行数 - int columnCount = propertyInfos.Length;//列数 - - #region 低版本Excel 标题列插入批注 - //// 创建绘图主控制器(用于包括单元格注释在内的所有形状的顶级容器) - //IDrawing patriarch = (HSSFPatriarch)sheet.CreateDrawingPatriarch(); - ////前四个参数是坐标点,后四个参数是编辑和显示批注时的大小. - //IComment comment = patriarch.CreateCellComment(new HSSFClientAnchor(0, 0, 0, 0, 2, 1, 4, 4)); - //// 设置批注作者 - //comment.Author = "issuser"; - //// 设置批注内容 - //comment.String = new HSSFRichTextString($"{comment.Author}:{Environment.NewLine}填写0~100之间的两位小数"); - - ////// 让作者像Excel一样在顶部以粗体显示 - ////// 当鼠标停留在已注释的单元格上时,作者将显示在状态栏中 - ////IFont font = workbook.CreateFont(); - ////font.Boldweight = (short)FontBoldWeight.Bold; - ////comment.String.ApplyFont(0, comment.Author.Length, font); - //// 设置批注显示 - //comment.Visible = false; - #endregion - - #region 高版本Excel 标题列插入批注 - //创建绘图主控制器(用于包括单元格注释在内的所有形状的顶级容器) - IDrawing xssfDraw = sheet.CreateDrawingPatriarch(); - //前四个参数是坐标点,后四个参数是编辑和显示批注时的大小. - IComment comment = xssfDraw.CreateCellComment(new XSSFClientAnchor(0, 0, 0, 0, 2, 1, 4, 4)); - //设置批注作者 - comment.Author = "issuser"; - //设置批注内容 - comment.String = new XSSFRichTextString($"{comment.Author}:{Environment.NewLine}填写0~100之间的两位小数"); - //设置批注是否显示 - comment.Visible = false; - - #endregion - - //设置列头 - row = sheet.CreateRow(0);//excel第一行设为列头 - for (int c = 0; c < columnCount; c++) - { - cell = row.CreateCell(c); - cell.SetCellValue(propertyInfos[c].GetCustomAttribute().DisplayName); - - if (propertyInfos[c].GetCustomAttribute().DisplayName == "工位占用比例%" || propertyInfos[c].GetCustomAttribute().DisplayName == "收费率(%)") - { - // 为单元格分配注释 - cell.CellComment = comment; - } - } - - //设置每行每列的单元格, - for (int i = 0; i < rowCount; i++) - { - row = sheet.CreateRow(i + 1); - for (int j = 0; j < columnCount; j++) - { - cell = row.CreateCell(j);//excel第二行开始写入数据 - cell.SetCellValue(propertyInfos[j].GetValue(data[i])?.ToString()); - } - } - - #region 为列设置下拉框 - if (feeLevelList != null && feeLevelList.Count > 0) - { - var feeLevel = feeLevelList.Select(s => s.dic_Name).Distinct().ToList(); //费率级别 - SetCellDropdownList(workbook, sheet, feeLevel, "feeLevel", 1, colindex[0], colindex[0]); - } - if (feeChildList != null && feeChildList.Count > 0) - { - var feeChildLevel = feeChildList.OrderBy(o => o.Rate_Code).Select(s => s.Rate_Name).Distinct().ToList(); //费率子级 - SetCellDropdownList(workbook, sheet, feeChildLevel, "feeChildLevel", 2, colindex[1], colindex[1]); - } - - if (dicList != null && dicList.Count > 0 && colindex.Length > 2) - { - var dicInfo = dicList.Select(s => s.dic_Name).Distinct().ToList(); - SetCellDropdownList(workbook, sheet, dicInfo, "dicsheet", 3, colindex[2], colindex[2]); - } - #endregion - - using (MemoryStream memoryStream = new MemoryStream()) - { - workbook.Write(memoryStream);//向打开的这个xls文件中写入数据 - return memoryStream.ToArray(); - } - } - /// /// 设置单元格下拉框(除去标题行) /// diff --git a/src/AntSK.Domain/Domain/Model/Excel/KMSExcelModel.cs b/src/AntSK.Domain/Domain/Model/Excel/KMSExcelModel.cs new file mode 100644 index 0000000..aef6fea --- /dev/null +++ b/src/AntSK.Domain/Domain/Model/Excel/KMSExcelModel.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AntSK.Domain.Domain.Model.Excel +{ + public class KMSExcelModel + { + [ExeclProperty("问题",0)] + public string Question { get; set; } + + [ExeclProperty("答案", 1)] + public string Answer { get; set; } + } +} diff --git a/src/AntSK.Domain/Domain/Model/ImportKMSTaskReq.cs b/src/AntSK.Domain/Domain/Model/ImportKMSTaskReq.cs index 7d24376..5898626 100644 --- a/src/AntSK.Domain/Domain/Model/ImportKMSTaskReq.cs +++ b/src/AntSK.Domain/Domain/Model/ImportKMSTaskReq.cs @@ -29,6 +29,7 @@ namespace AntSK.Domain.Domain.Model { File = 1, Url = 2, - Text = 3 + Text = 3, + Excel=4 } } diff --git a/src/AntSK.Domain/Domain/Service/ImportKMSService.cs b/src/AntSK.Domain/Domain/Service/ImportKMSService.cs index fffeec9..4566f85 100644 --- a/src/AntSK.Domain/Domain/Service/ImportKMSService.cs +++ b/src/AntSK.Domain/Domain/Service/ImportKMSService.cs @@ -2,6 +2,7 @@ using AntSK.Domain.Domain.Interface; using AntSK.Domain.Domain.Model; using AntSK.Domain.Domain.Model.Constant; +using AntSK.Domain.Domain.Model.Excel; using AntSK.Domain.Repositories; using Microsoft.KernelMemory; @@ -56,7 +57,7 @@ namespace AntSK.Domain.Domain.Service //导入文本 { var importResult = _memory.ImportTextAsync(req.Text, fileid, new TagCollection() { { KmsConstantcs.KmsIdTag, req.KmsId } } - , index: KmsConstantcs.KmsIndex).Result; + , index: KmsConstantcs.KmsIndex).Result; //查询文档数量 var docTextList = _kMService.GetDocumentByFileID(km.Id, fileid).Result; req.KmsDetail.Url = req.Url; @@ -64,6 +65,23 @@ namespace AntSK.Domain.Domain.Service } break; + case ImportType.Excel: + using (var fs = File.OpenRead(req.FilePath)) + { + var excelList= ExeclHelper.ExcelToList(fs); + foreach (var item in excelList) + { + var text = @$"Question:{item.Question}{Environment.NewLine} Answer:{item.Answer}"; + var importResult = _memory.ImportTextAsync(text, fileid, new TagCollection() { { KmsConstantcs.KmsIdTag, req.KmsId } } + , index: KmsConstantcs.KmsIndex).Result; + } + var testList = _kMService.GetDocumentByFileID(km.Id, fileid).Result; + req.KmsDetail.FileName = req.FileName; + string fileGuidName = Path.GetFileName(req.FilePath); + req.KmsDetail.FileGuidName = fileGuidName; + req.KmsDetail.DataCount = excelList.Count(); + } + break; } req.KmsDetail.Status = Model.Enum.ImportKmsStatus.Success; _kmsDetails_Repositories.Update(req.KmsDetail); diff --git a/src/AntSK/Controllers/FileController.cs b/src/AntSK/Controllers/FileController.cs index ef3ca5d..fc13934 100644 --- a/src/AntSK/Controllers/FileController.cs +++ b/src/AntSK/Controllers/FileController.cs @@ -1,4 +1,6 @@ -using AntSK.Domain.Options; +using AntSK.Domain; +using AntSK.Domain.Domain.Model.Excel; +using AntSK.Domain.Options; using Microsoft.AspNetCore.Mvc; namespace AntSK.Controllers @@ -41,5 +43,17 @@ namespace AntSK.Controllers return Ok(uploads); } + + /// + /// 下载模板 + /// + /// + [HttpGet] + public async Task DownExcelTemplate() + { + var list = new List(); + var file = ExeclHelper.ListToExcel(list.ToArray(), "AntSK导入模板"); + return File(file, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "AntSK导入模板.xlsx"); + } } } diff --git a/src/AntSK/Pages/KmsPage/KmsDetail.razor b/src/AntSK/Pages/KmsPage/KmsDetail.razor index 6a64047..6c711b5 100644 --- a/src/AntSK/Pages/KmsPage/KmsDetail.razor +++ b/src/AntSK/Pages/KmsPage/KmsDetail.razor @@ -159,7 +159,7 @@ OnOk="@ExcelHandleOk" OnCancel="@ExcelHandleCancel" ConfirmLoading="@_excelConfirmLoading"> - 下载模板 + 下载模板