新增加用户导入、Excel通用导入方法封装
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
using Infrastructure;
|
||||
using Infrastructure.Model;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Serialization;
|
||||
@@ -9,8 +8,6 @@ using OfficeOpenXml;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using ZR.Admin.WebApi.Filters;
|
||||
|
||||
namespace ZR.Admin.WebApi.Controllers
|
||||
@@ -134,5 +131,37 @@ namespace ZR.Admin.WebApi.Controllers
|
||||
|
||||
return sFileName;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 下载导入模板
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="list"></param>
|
||||
/// <param name="stream"></param>
|
||||
/// <param name="fileName">下载文件名</param>
|
||||
/// <returns></returns>
|
||||
protected string DownloadImportTemplate<T>(List<T> list, Stream stream, string fileName)
|
||||
{
|
||||
IWebHostEnvironment webHostEnvironment = (IWebHostEnvironment)App.ServiceProvider.GetService(typeof(IWebHostEnvironment));
|
||||
string sFileName = $"{fileName}模板.xlsx";
|
||||
string newFileName = Path.Combine(webHostEnvironment.WebRootPath, "importTemplate", sFileName);
|
||||
//调试模式需要加上
|
||||
ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
|
||||
if (!Directory.Exists(newFileName))
|
||||
{
|
||||
Directory.CreateDirectory(Path.GetDirectoryName(newFileName));
|
||||
}
|
||||
using (ExcelPackage package = new(new FileInfo(newFileName)))
|
||||
{
|
||||
// 添加worksheet
|
||||
ExcelWorksheet worksheet = package.Workbook.Worksheets.Add(fileName);
|
||||
|
||||
//全部字段导出
|
||||
worksheet.Cells.LoadFromCollection(list, true, OfficeOpenXml.Table.TableStyles.Light13);
|
||||
package.SaveAs(stream);
|
||||
}
|
||||
|
||||
return sFileName;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
using Infrastructure.Attribute;
|
||||
using Infrastructure.Enums;
|
||||
using Infrastructure.Model;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
@@ -8,7 +9,9 @@ using OfficeOpenXml;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
using ZR.Admin.WebApi.Filters;
|
||||
using ZR.Common;
|
||||
using ZR.Model;
|
||||
using ZR.Model.System;
|
||||
using ZR.Service;
|
||||
@@ -170,43 +173,37 @@ namespace ZR.Admin.WebApi.Controllers.System
|
||||
return ToResponse(ToJson(result));
|
||||
}
|
||||
|
||||
///// <summary>
|
||||
///// 导入 ok
|
||||
///// </summary>
|
||||
///// <param name="formFile">使用IFromFile必须使用name属性否则获取不到文件</param>
|
||||
///// <returns></returns>
|
||||
//[HttpPost("importData")]
|
||||
//[Log(Title = "用户导入", BusinessType = BusinessType.IMPORT)]
|
||||
//[ActionPermissionFilter(Permission = "system:user:import")]
|
||||
//public IActionResult ImportData([FromForm(Name = "file")] IFormFile formFile)
|
||||
//{
|
||||
// var mapper = new Mapper(formFile.OpenReadStream());// 从流获取
|
||||
// //读取的sheet信息
|
||||
// var rows = mapper.Take<SysUser>(0);
|
||||
// foreach (var item in rows)
|
||||
// {
|
||||
// SysUser u = item.Value;
|
||||
// }
|
||||
// //TODO 业务逻辑
|
||||
// return SUCCESS(1);
|
||||
//}
|
||||
/// <summary>
|
||||
/// 导入
|
||||
/// </summary>
|
||||
/// <param name="formFile">使用IFromFile必须使用name属性否则获取不到文件</param>
|
||||
/// <returns></returns>
|
||||
[HttpPost("importData")]
|
||||
[Log(Title = "用户导入", BusinessType = BusinessType.IMPORT)]
|
||||
[ActionPermissionFilter(Permission = "system:user:import")]
|
||||
public IActionResult ImportData([FromForm(Name = "file")] IFormFile formFile)
|
||||
{
|
||||
IEnumerable<SysUser> users = ExcelHelper<SysUser>.ImportData(formFile.OpenReadStream());
|
||||
|
||||
///// <summary>
|
||||
///// 用户模板 ok
|
||||
///// </summary>
|
||||
///// <returns></returns>
|
||||
//[HttpGet("importTemplate")]
|
||||
//[Log(Title = "用户模板", BusinessType = BusinessType.EXPORT)]
|
||||
//[ActionPermissionFilter(Permission = "system:user:export")]
|
||||
//public IActionResult ImportTemplateExcel()
|
||||
//{
|
||||
// List<SysUser> user = new List<SysUser>();
|
||||
// var mapper = new Mapper();
|
||||
// MemoryStream stream = new MemoryStream();
|
||||
// mapper.Save(stream, user, "sheel1", overwrite: true, xlsx: true);
|
||||
// //Response.Headers.Append("content-disposition", "attachment;filename=sysUser.xlsx");
|
||||
// return File(stream.ToArray(), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "sysUser.xlsx");
|
||||
//}
|
||||
//TODO 业务逻辑,自行插入数据到db
|
||||
return SUCCESS(users);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 用户导入模板下载
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[HttpGet("importTemplate")]
|
||||
[Log(Title = "用户模板", BusinessType = BusinessType.EXPORT)]
|
||||
[AllowAnonymous]
|
||||
public IActionResult ImportTemplateExcel()
|
||||
{
|
||||
List<SysUser> user = new List<SysUser>();
|
||||
MemoryStream stream = new MemoryStream();
|
||||
|
||||
string sFileName = DownloadImportTemplate(user, stream, "用户列表");
|
||||
return File(stream.ToArray(), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", $"{sFileName}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 用户导出
|
||||
|
||||
Reference in New Issue
Block a user