EPPlus替换成miniExcel
This commit is contained in:
@@ -2,9 +2,9 @@
|
||||
using Infrastructure.Model;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using MiniExcelLibs;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Serialization;
|
||||
using OfficeOpenXml;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
@@ -58,15 +58,15 @@ namespace ZR.Admin.WebApi.Controllers
|
||||
/// <summary>
|
||||
/// 导出Excel
|
||||
/// </summary>
|
||||
/// <param name="path"></param>
|
||||
/// <param name="fileName"></param>
|
||||
/// <param name="path">完整文件路径</param>
|
||||
/// <param name="fileName">带扩展文件名</param>
|
||||
/// <returns></returns>
|
||||
protected IActionResult ExportExcel(string path, string fileName)
|
||||
{
|
||||
IWebHostEnvironment webHostEnvironment = (IWebHostEnvironment)App.ServiceProvider.GetService(typeof(IWebHostEnvironment));
|
||||
string fileDir = Path.Combine(webHostEnvironment.WebRootPath, path, fileName);
|
||||
//IWebHostEnvironment webHostEnvironment = (IWebHostEnvironment)App.ServiceProvider.GetService(typeof(IWebHostEnvironment));
|
||||
//string fileDir = Path.Combine(webHostEnvironment.WebRootPath, path, fileName);
|
||||
|
||||
var stream = ff.File.OpenRead(fileDir); //创建文件流
|
||||
var stream = ff.File.OpenRead(path); //创建文件流
|
||||
return File(stream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", HttpUtility.UrlEncode(fileName));
|
||||
}
|
||||
|
||||
@@ -130,31 +130,19 @@ namespace ZR.Admin.WebApi.Controllers
|
||||
/// <param name="fileName"></param>
|
||||
protected string ExportExcel<T>(List<T> list, string sheetName, string fileName)
|
||||
{
|
||||
var fileInfo = ExportExcelNew(list, sheetName, fileName);
|
||||
|
||||
return fileInfo.Item1;
|
||||
return ExportExcelMini(list, sheetName, fileName).Item1;
|
||||
}
|
||||
|
||||
protected (string, string) ExportExcelNew<T>(List<T> list, string sheetName, string fileName)
|
||||
protected (string, string) ExportExcelMini<T>(List<T> list, string sheetName, string fileName)
|
||||
{
|
||||
IWebHostEnvironment webHostEnvironment = (IWebHostEnvironment)App.ServiceProvider.GetService(typeof(IWebHostEnvironment));
|
||||
string sFileName = $"{fileName}{DateTime.Now:MMddHHmmss}.xlsx";
|
||||
string newFileName = Path.Combine(webHostEnvironment.WebRootPath, "export", sFileName);
|
||||
//调试模式需要加上
|
||||
ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
|
||||
Directory.CreateDirectory(Path.GetDirectoryName(newFileName));
|
||||
using (ExcelPackage package = new(new FileInfo(newFileName)))
|
||||
{
|
||||
// 添加worksheet
|
||||
ExcelWorksheet worksheet = package.Workbook.Worksheets.Add(sheetName);
|
||||
//单元格自动适应大小
|
||||
worksheet.Cells.Style.ShrinkToFit = true;
|
||||
//全部字段导出
|
||||
worksheet.Cells.LoadFromCollection(list, true, OfficeOpenXml.Table.TableStyles.Light13);
|
||||
package.Save();
|
||||
}
|
||||
string sFileName = $"{fileName}{DateTime.Now:MM-dd-HHmmss}.xlsx";
|
||||
string fullPath = Path.Combine(webHostEnvironment.WebRootPath, "export", sFileName);
|
||||
|
||||
Directory.CreateDirectory(Path.GetDirectoryName(fullPath));
|
||||
|
||||
return (sFileName, newFileName);
|
||||
MiniExcel.SaveAs(fullPath, list, sheetName: sheetName);
|
||||
return (sFileName, fullPath);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -170,23 +158,12 @@ namespace ZR.Admin.WebApi.Controllers
|
||||
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.Style.ShrinkToFit = true;
|
||||
//全部字段导出
|
||||
worksheet.Cells.LoadFromCollection(list, true, OfficeOpenXml.Table.TableStyles.Light13);
|
||||
package.SaveAs(stream);
|
||||
}
|
||||
|
||||
MiniExcel.SaveAs(newFileName, list);
|
||||
return sFileName;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,13 +2,11 @@ using Infrastructure.Attribute;
|
||||
using Infrastructure.Enums;
|
||||
using Infrastructure.Model;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using MiniExcelLibs;
|
||||
using SqlSugar;
|
||||
using ZR.Admin.WebApi.Extensions;
|
||||
using ZR.Admin.WebApi.Filters;
|
||||
using ZR.Common;
|
||||
using ZR.Model;
|
||||
using ZR.Model.System;
|
||||
using ZR.Service.System.IService;
|
||||
@@ -179,7 +177,14 @@ namespace ZR.Admin.WebApi.Controllers.System
|
||||
[ActionPermissionFilter(Permission = "system:user:import")]
|
||||
public IActionResult ImportData([FromForm(Name = "file")] IFormFile formFile)
|
||||
{
|
||||
IEnumerable<SysUser> users = ExcelHelper<SysUser>.ImportData(formFile.OpenReadStream());
|
||||
//List<SysUser> users = (List<SysUser>)ExcelHelper<SysUser>.ImportData(formFile.OpenReadStream());
|
||||
List<SysUser> users = new();
|
||||
using (var stream = formFile.OpenReadStream())
|
||||
{
|
||||
users = stream.Query<SysUser>().ToList();
|
||||
}
|
||||
|
||||
string msg = UserService.ImportUsers(users);
|
||||
|
||||
//TODO 业务逻辑,自行插入数据到db
|
||||
return SUCCESS(users);
|
||||
@@ -213,9 +218,8 @@ namespace ZR.Admin.WebApi.Controllers.System
|
||||
{
|
||||
var list = UserService.SelectUserList(user, new PagerInfo(1, 10000));
|
||||
|
||||
//调试模式需要加上
|
||||
string sFileName = ExportExcel(list.Result, "user", "用户列表");
|
||||
return ExportExcel("export", sFileName);
|
||||
var result = ExportExcelMini(list.Result, "user", "用户列表");
|
||||
return ExportExcel(result.Item2, result.Item1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,7 +30,6 @@
|
||||
<PackageReference Include="NLog" Version="5.0.4" />
|
||||
<PackageReference Include="NLog.Web.AspNetCore" Version="5.1.4" />
|
||||
<PackageReference Include="Mapster" Version="7.3.0" />
|
||||
<PackageReference Include="EPPlus" Version="6.0.5" />
|
||||
<PackageReference Include="Hei.Captcha" Version="0.3.0" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
@@ -155,8 +155,8 @@ $if(replaceDto.ShowBtnExport)
|
||||
{
|
||||
return ToResponse(ResultCode.FAIL, "没有要导出的数据");
|
||||
}
|
||||
string sFileName = ExportExcel(list, "${genTable.FunctionName}", "${genTable.FunctionName}");
|
||||
return ExportExcel("export", sFileName);
|
||||
var result = ExportExcelMini(list, "${genTable.FunctionName}", "${genTable.FunctionName}");
|
||||
return ExportExcel(result.Item2, result.Item1);
|
||||
}
|
||||
$end
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@ using System.ComponentModel.DataAnnotations;
|
||||
using ${options.ModelsNamespace}.Dto;
|
||||
using ${options.ModelsNamespace}.Models;
|
||||
$if(replaceDto.ShowBtnExport)
|
||||
using OfficeOpenXml.Attributes;
|
||||
using MiniExcelLibs.Attributes;
|
||||
$end
|
||||
|
||||
namespace ${options.DtosNamespace}.Dto
|
||||
@@ -39,9 +39,9 @@ $if(item.IsRequired)
|
||||
$end
|
||||
$if(replaceDto.ShowBtnExport)
|
||||
$if(item.IsExport)
|
||||
[EpplusTableColumn(Header = "$if(item.ColumnComment == "")${item.CsharpField}${else}${item.ColumnComment}${end}"$if(item.CsharpType == "DateTime"), NumberFormat = "yyyy-MM-dd HH:mm:ss"$end)]
|
||||
[ExcelColumn(Name = "$if(item.ColumnComment == "")${item.CsharpField}${else}${item.ColumnComment}${end}"$if(item.CsharpType == "DateTime"), Format = "yyyy-MM-dd HH:mm:ss"$end)]
|
||||
$else
|
||||
[EpplusIgnore]
|
||||
[ExcelIgnore]
|
||||
$end
|
||||
$end
|
||||
public $item.CsharpType$item.RequiredStr $item.CsharpField { get; set; }
|
||||
@@ -50,14 +50,14 @@ $end
|
||||
|
||||
$if(genTable.TplCategory == "subNav" && genTable.SubTable != null)
|
||||
$if(replaceDto.ShowBtnExport)
|
||||
[EpplusIgnore]
|
||||
[ExcelIgnore]
|
||||
$end
|
||||
public ${genTable.SubTable.ClassName} ${genTable.SubTable.ClassName} { get; set; }
|
||||
$end
|
||||
|
||||
$if(genTable.TplCategory == "subNavMore" && genTable.SubTable != null)
|
||||
$if(replaceDto.ShowBtnExport)
|
||||
[EpplusIgnore]
|
||||
[ExcelIgnore]
|
||||
$end
|
||||
public List<${genTable.SubTable.ClassName}> ${genTable.SubTable.ClassName} { get; set; }
|
||||
$end
|
||||
|
||||
Reference in New Issue
Block a user