From dd09fb2283107f4199d4f4f3a9c236a5c71398e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A9=99=E5=AD=90?= <454313500@qq.com> Date: Sat, 4 Jun 2022 14:40:59 +0800 Subject: [PATCH 1/2] Update README.md --- README.md | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index e4dd0045..76218358 100644 --- a/README.md +++ b/README.md @@ -74,7 +74,7 @@ WebFirst开发:所有代码生成器已经配置完成,无需任何操作数 **身份验证**:JWT、IdentityServer4 -**组件**:~~EFcore~~SqlSugar、Autofac、Castle、Swagger、Log4Net、Redis、RabbitMq、ES、Quartz.net、~~T4~~ +**组件**:SqlSugar、Autofac、Castle、Swagger、Log4Net、Redis、RabbitMq、ES、Quartz.net、~~T4~~ **分布式**:CAP、Lock @@ -96,9 +96,8 @@ WebFirst开发:所有代码生成器已经配置完成,无需任何操作数 - [x] 支持采用`异步`开发awit/async - [x] 支持数据库主从`读写分离` - [x] 支持功能替换,无需改动代码,只需配置`json文件`进行装配即可 -- [x] ~~-支持采用DbFirst开发方式,使用`T4模板代码生成器`,自动映射模型一键生成Service及IService所有代码~~ - [x] 支持WebFirst,无需改动代码,自动生成全套代码与数据库,只需点点点 -- [x] ~~-支持`用户-角色-菜单-接口`以及vue2.0前端全部逻辑代码,下载无需修改直接使用~~ +- [x] 支持`用户-角色-菜单-接口`以及vue2.0前端全部逻辑代码,下载无需修改直接使用 - [x] 支持`Aop封装`,FilterAop、IocAop、LogAop、SqlAop - [x] 支持`Log4Net日志`记录,自动生成至bin目录下的logs文件夹 - [x] 支持`DbSeed数据库种子数据`接入 @@ -114,7 +113,6 @@ WebFirst开发:所有代码生成器已经配置完成,无需任何操作数 - [x] 支持`Ocelot`网关,路由、服务聚合、服务发现、认证、鉴权、限流、熔断、缓存、Header头传递 - [x] 支持`Apollo`全局配置中心; - [x] 支持`docker`镜像制作 -- [x] ~~-支持页面`静态化处理`,将动态页面生成静态页面~~ - [x] 支持`Quartz.net`任务调度,实现任意接口被调度 - [x] 支持`ELK`,log4net+kafka+es+logstach+kibana - [x] 支持`IdentityService4`授权中心 @@ -124,7 +122,7 @@ WebFirst开发:所有代码生成器已经配置完成,无需任何操作数 - [x] 支持`Docker+k8s`部署 - [x] 支持`Jenkins+CI/CD` - [x] 支持`AutoMapper`模块映射 -- [ ] 支持`微信支付`(没账号) +- [x] 支持`微信支付` - [x] 支持`单表多租户`常用功能 - [x] 支持`逻辑删除`常用功能 - [x] 支持`操作日志`常用功能 From 300d8224ec018e3b9804b652347dd9713271cf1a Mon Sep 17 00:00:00 2001 From: chenchun Date: Wed, 22 Jun 2022 14:07:09 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=8D=87=E7=BA=A7excel=E6=93=8D=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Yi.Framework.Common/Helper/ExcelHelper.cs | 240 +++++++++++++++--- 1 file changed, 201 insertions(+), 39 deletions(-) diff --git a/Yi.Framework.Net6/Yi.Framework.Common/Helper/ExcelHelper.cs b/Yi.Framework.Net6/Yi.Framework.Common/Helper/ExcelHelper.cs index 8d0f808d..f3e31ae0 100644 --- a/Yi.Framework.Net6/Yi.Framework.Common/Helper/ExcelHelper.cs +++ b/Yi.Framework.Net6/Yi.Framework.Common/Helper/ExcelHelper.cs @@ -1,60 +1,222 @@ -using System; +using OfficeOpenXml; +using System; using System.Collections.Generic; using System.IO; using System.Linq; +using System.Reflection; using System.Text; using System.Threading.Tasks; -using OfficeOpenXml; namespace Yi.Framework.Common.Helper { public class ExcelHelper { /// - /// + /// 导出Excel /// /// - /// 数据 - /// 表头 - /// - public static string CreateExcelFromList(List dataList, List headers,string evn) + /// + /// + /// + public static string ExportExcel(List list, string sheetName, string fileName,string path) { + string sFileName = $"{fileName}.xlsx"; + string newFileName = Path.Combine(path, sFileName); + //调试模式需要加上 ExcelPackage.LicenseContext = LicenseContext.NonCommercial; - string sWebRootFolder = Path.Combine($"{evn}", "wwwroot/Excel");//如果用浏览器url下载的方式 存放excel的文件夹一定要建在网站首页的同级目录下!!! - if (!Directory.Exists(sWebRootFolder)) + Directory.CreateDirectory(Path.GetDirectoryName(newFileName)!); + using (ExcelPackage package = new(new FileInfo(newFileName))) { - Directory.CreateDirectory(sWebRootFolder); - } - string sFileName = $@"Excel_{DateTime.Now.ToString("yyyyMMddHHmmss")}.xlsx"; - var path = Path.Combine(sWebRootFolder, sFileName); - FileInfo file = new FileInfo(path); - if (file.Exists) - { - file.Delete(); - file = new FileInfo(path); - } - using (ExcelPackage package = new(file)) - { - //创建sheet - ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("sheet1"); - worksheet.Cells.LoadFromCollection(dataList, true); - //表头字段 - for (int i = 0; i < headers.Count; i++) - { - worksheet.Cells[1, i + 1].Value = headers[i]; - } - for (int i = 0; i < headers.Count + 1; i++) - {//删除不需要的列 - string aa = worksheet.Cells[1, i + 1].Value.ToString(); - if (aa == "总行数") - { - worksheet.DeleteColumn(i + 1); - } - } + // 添加worksheet + ExcelWorksheet worksheet = package.Workbook.Worksheets.Add(sheetName); + //单元格自动适应大小 + worksheet.Cells.Style.ShrinkToFit = true; + //全部字段导出 + worksheet.Cells.LoadFromCollection(list, true, OfficeOpenXml.Table.TableStyles.Light13); package.Save(); } - //return path;//这是返回文件的方式 - return sFileName; //如果用浏览器url下载的方式 这里直接返回生成的文件名就可以了 + + return newFileName; + } + + /// + /// 下载导入模板 + /// + /// + /// + /// + /// 下载文件名 + /// + protected string DownloadImportTemplate(List list, Stream stream, string fileName,string path) + { + string sFileName = $"{fileName}.xlsx"; + string newFileName = Path.Combine(path, 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); + } + + return sFileName; + } + + + + + /// + /// 导入数据 + /// + /// + /// + public static List ImportData(Stream stream) where T : new() + { + using ExcelPackage package = new(stream); + ExcelPackage.LicenseContext = LicenseContext.NonCommercial; + ExcelWorksheet worksheet = package.Workbook.Worksheets[0];//读取第1个sheet + //获取表格的列数和行数 + + int colStart = worksheet.Dimension.Start.Column; + int colEnd = worksheet.Dimension.End.Column; + int rowStart = worksheet.Dimension.Start.Row; + int rowEnd = worksheet.Dimension.End.Row; + //int rowCount = worksheet.Dimension.Rows; + //int ColCount = worksheet.Dimension.Columns; + + List resultList = new(); + List propertyInfos = new();// new(typeof(T).GetProperties()); + Dictionary dictHeader = new(); + for (int i = colStart; i < colEnd; i++) + { + var name = worksheet.Cells[rowStart, i].Value.ToString(); + dictHeader[name!] = i; + + PropertyInfo propertyInfo = MapPropertyInfo(name!); + if (propertyInfo != null) + { + propertyInfos.Add(propertyInfo); + } + } + for (int row = rowStart + 1; row <= rowEnd; row++) + { + T result = new(); + + foreach (PropertyInfo p in propertyInfos) + { + try + { + ExcelRange cell = worksheet.Cells[row, dictHeader[p.Name]]; + if (cell.Value == null) + { + continue; + } + switch (p.PropertyType.Name.ToLower()) + { + case "string": + p.SetValue(result, cell.GetValue()); + break; + case "int16": + p.SetValue(result, cell.GetValue()); break; + case "int32": + p.SetValue(result, cell.GetValue()); break; + case "int64": + p.SetValue(result, cell.GetValue()); break; + case "decimal": + p.SetValue(result, cell.GetValue()); + break; + case "double": + p.SetValue(result, cell.GetValue()); break; + case "datetime": + p.SetValue(result, cell.GetValue()); break; + case "boolean": + p.SetValue(result, cell.GetValue()); break; + case "char": + p.SetValue(result, cell.GetValue()); break; + default: + break; + } + } + catch (KeyNotFoundException ex) + { + Console.WriteLine("未找到该列将继续循环," + ex.Message); + continue; + } + } + resultList.Add(result); + } + + return resultList.ToList(); + } + + /// + /// 查找Excel列名对应的实体属性 + /// + /// + /// + private static PropertyInfo MapPropertyInfo(string columnName) + { + PropertyInfo[] propertyList = GetProperties(typeof(T)); + PropertyInfo propertyInfo = propertyList.Where(p => p.Name == columnName).FirstOrDefault()!; + if (propertyInfo != null) + { + return propertyInfo; + } + else + { + foreach (PropertyInfo tempPropertyInfo in propertyList) + { + System.ComponentModel.DescriptionAttribute[] attributes = (System.ComponentModel.DescriptionAttribute[])tempPropertyInfo.GetCustomAttributes(typeof(System.ComponentModel.DescriptionAttribute), false); + if (attributes.Length > 0) + { + if (attributes[0].Description == columnName) + { + return tempPropertyInfo; + } + } + } + } + return null!; + } + + /// + /// 得到类里面的属性集合 + /// + /// + /// + /// + private static PropertyInfo[] GetProperties(Type type, string[] columns = null!) + { + PropertyInfo[] properties = null!; + properties = type.GetProperties(); + + if (columns != null && columns.Length > 0) + { + // 按columns顺序返回属性 + var columnPropertyList = new List(); + foreach (var column in columns) + { + var columnProperty = properties.Where(p => p.Name == column).FirstOrDefault(); + if (columnProperty != null) + { + columnPropertyList.Add(columnProperty); + } + } + return columnPropertyList.ToArray(); + } + else + { + return properties; + } } } }