mirror of
https://github.com/AIDotNet/AntSK.git
synced 2026-02-17 22:10:14 +08:00
add excel 导入
This commit is contained in:
@@ -69,6 +69,84 @@
|
||||
<param name="value"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:AntSK.Domain.ExeclHelper.ExcelToDataTable(System.String,System.Boolean)">
|
||||
<summary>
|
||||
将excel导入到datatable
|
||||
</summary>
|
||||
<param name="filePath">excel路径</param>
|
||||
<param name="isColumnName">第一行是否是列名</param>
|
||||
<returns>返回datatable</returns>
|
||||
</member>
|
||||
<member name="M:AntSK.Domain.ExeclHelper.ExcelToDataTable(System.IO.Stream,System.Boolean)">
|
||||
<summary>
|
||||
将excel导入到datatable
|
||||
</summary>
|
||||
<param name="stream">流</param>
|
||||
<param name="isColumnName">第一行是否是列名</param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:AntSK.Domain.ExeclHelper.ExcelToList``1(System.IO.Stream)">
|
||||
<summary>
|
||||
excel转list
|
||||
</summary>
|
||||
<typeparam name="TResult"></typeparam>
|
||||
<param name="stream"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:AntSK.Domain.ExeclHelper.ExcelToList``1(System.IO.Stream,System.String)">
|
||||
<summary>
|
||||
excel转list-根据sheetName得到List
|
||||
</summary>
|
||||
<typeparam name="TResult"></typeparam>
|
||||
<param name="stream"></param>
|
||||
<param name="sheetName"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:AntSK.Domain.ExeclHelper.ListToExcel``1(``0[],System.String)">
|
||||
<summary>
|
||||
List导出excel 二进制流
|
||||
</summary>
|
||||
<typeparam name="T">实体</typeparam>
|
||||
<param name="data">List</param>
|
||||
<param name="sheetName">sheetname 可不填,默认Sheet0</param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:AntSK.Domain.ExeclHelper.DataTableToExcel(System.Data.DataTable,System.String,System.String)">
|
||||
<summary>
|
||||
Dt导出excel 二进制流
|
||||
</summary>
|
||||
<param name="dt">datatable</param>
|
||||
<param name="strFile">strFile</param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:AntSK.Domain.ExeclHelper.ListWriteExcel``1(``0[],System.String,System.String)">
|
||||
<summary>
|
||||
List写入excel
|
||||
</summary>
|
||||
<typeparam name="T"></typeparam>
|
||||
<param name="data"></param>
|
||||
<param name="strFile">路径</param>
|
||||
<param name="sheetName"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:AntSK.Domain.ExeclHelper.DataTableWriteExcel(System.Data.DataTable,System.String,System.String)">
|
||||
<summary>
|
||||
dt写入excel
|
||||
</summary>
|
||||
<param name="dt">datatable</param>
|
||||
<param name="strFile">路径</param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:AntSK.Domain.ExeclHelper.SetCellDropdownList(NPOI.SS.UserModel.IWorkbook,NPOI.SS.UserModel.ISheet,System.Collections.Generic.List{System.String},System.String,System.Int32,System.Int32,System.Int32)">
|
||||
<summary>
|
||||
设置单元格下拉框(除去标题行)
|
||||
</summary>
|
||||
<param name="workbook"></param>
|
||||
<param name="sheet"></param>
|
||||
<param name="ddlList"></param>
|
||||
<param name="firstcol"></param>
|
||||
<param name="lastcol"></param>
|
||||
</member>
|
||||
<member name="T:AntSK.Domain.Domain.Model.Enum.AIType">
|
||||
<summary>
|
||||
AI类型
|
||||
|
||||
@@ -764,112 +764,6 @@ namespace AntSK.Domain
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// List导出excel 二进制流(服务成本/项目收入下载列设置下拉框)
|
||||
/// </summary>
|
||||
/// <typeparam name="T">实体</typeparam>
|
||||
/// <param name="data">List</param>
|
||||
/// <param name="sheetName">sheetname 可不填,默认Sheet0</param>
|
||||
/// <returns></returns>
|
||||
public static byte[] ListToExcelHasDDL<T>(T[] data, List<DicDTO> feeLevelList, List<FeeRateSetDTO> feeChildList, List<DicDTO> 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<ExeclPropertyAttribute>().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<ExeclPropertyAttribute>().DisplayName);
|
||||
|
||||
if (propertyInfos[c].GetCustomAttribute<ExeclPropertyAttribute>().DisplayName == "工位占用比例%" || propertyInfos[c].GetCustomAttribute<ExeclPropertyAttribute>().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();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置单元格下拉框(除去标题行)
|
||||
/// </summary>
|
||||
|
||||
17
src/AntSK.Domain/Domain/Model/Excel/KMSExcelModel.cs
Normal file
17
src/AntSK.Domain/Domain/Model/Excel/KMSExcelModel.cs
Normal file
@@ -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; }
|
||||
}
|
||||
}
|
||||
@@ -29,6 +29,7 @@ namespace AntSK.Domain.Domain.Model
|
||||
{
|
||||
File = 1,
|
||||
Url = 2,
|
||||
Text = 3
|
||||
Text = 3,
|
||||
Excel=4
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<KMSExcelModel>(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);
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 下载模板
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[HttpGet]
|
||||
public async Task<IActionResult> DownExcelTemplate()
|
||||
{
|
||||
var list = new List<KMSExcelModel>();
|
||||
var file = ExeclHelper.ListToExcel<KMSExcelModel>(list.ToArray(), "AntSK导入模板");
|
||||
return File(file, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "AntSK导入模板.xlsx");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -159,7 +159,7 @@
|
||||
OnOk="@ExcelHandleOk"
|
||||
OnCancel="@ExcelHandleCancel"
|
||||
ConfirmLoading="@_excelConfirmLoading">
|
||||
<a>下载模板</a>
|
||||
<a href="api/File/DownExcelTemplate" target="_blank">下载模板</a>
|
||||
<Upload Action="@("api/File/UploadFile")"
|
||||
Name="file"
|
||||
Drag
|
||||
|
||||
@@ -226,7 +226,7 @@ namespace AntSK.Pages.KmsPage
|
||||
{
|
||||
var result = await _httpService.PostAsync(NavigationManager.BaseUri + "api/KMS/ImportKMSTask", new ImportKMSTaskDTO()
|
||||
{
|
||||
ImportType = ImportType.File,
|
||||
ImportType = ImportType.Excel,
|
||||
KmsId = KmsId,
|
||||
FilePath = item.Url,
|
||||
FileName = item.FileName
|
||||
|
||||
Reference in New Issue
Block a user