EPPlus实战篇——Excel写入
.net core 项目
可以向excel写入任何类型(T)的数据,只要T中的field的[Display(Name = "1233", Description = "#,##0.00")]:name==excel column header's name ,dicription==excel cell's formate
引用的nuget包:
1.EPPlus.Core
2. System.ComponentModel.Annotations
操作类:
public class ExcelWriteReadAccordingDisplayService<T> : IExcelWriteService<T> where T : class
{
ILogBase _logger;
Dictionary<int, PropertyInfo> _columnIndexDicForProperInfo;
Dictionary<int, DisplayAttribute> _columnIndexDicForDisplayAttr;
public ExcelWriteReadAccordingDisplayService(ILogBase logBase)
{
_logger = logBase;
}
}
class ExcelWriteReadAccordingDisplayService 中的方法:
main method:
public bool WriteData(List<T> data, string excelPath, string sheetName)
{ try
{
if (!WriteRequestCheck(excelPath))
{
_logger.Warn($"WriteData Request not valid.excelPath :{excelPath},sheetName:{sheetName}");
return false;
}
if (string.IsNullOrWhiteSpace(sheetName))
{
sheetName = DateTime.Now.ToString("yyyyMM");
} //set sheet style
Func<ExcelWorksheet, Color, bool> SetHeadStyle = (targetSheet, backgroundColor) =>
{
using (ExcelRange rng = targetSheet.Cells[, , , targetSheet.Dimension.Columns])
{
rng.Style.Font.Bold = true;
rng.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
rng.Style.Fill.PatternType = ExcelFillStyle.Solid;
rng.Style.Fill.BackgroundColor.SetColor(backgroundColor);
}
targetSheet.Row().Height = targetSheet.Row().Height * 1.4f;
return true;
}; Func<ExcelWorksheet, bool> SetAllCellsStyle = (targetSheet) =>
{
using (ExcelRange rng = targetSheet.Cells)
{
rng.AutoFitColumns();
}
return true;
}; using (ExcelPackage package = new ExcelPackage())
{
ExcelWorksheet sheet = AddSheet(package, sheetName); //write data to excel
GetColumnIndexDic(sheet);
WriteContent(data, sheet); //set style for excel
SetHeadStyle(sheet, Color.FromArgb(, , ));
SetAllCellsStyle(sheet); //save
package.SaveAs(new FileInfo(excelPath));
} return true;
}catch(Exception ex)
{
_logger.Error($"ExcelWrite data exception :{ex.ToString()},excel:{excelPath},data:{JsonConvert.SerializeObject(data)}");
throw ex;
}
}
public byte[] WriteData(List<T> data, string sheetName)
{
try
{
if (string.IsNullOrWhiteSpace(sheetName))
{
sheetName = DateTime.Now.ToString("yyyyMM");
}
if (data == null || data.Count() == 0)
{
_logger.Warn($"WriteData Request not valid. request.data:{JsonConvert.SerializeObject(data)}");
return null;
}
return SaveData(data, sheetName);
}
catch (Exception ex)
{
_logger.Error($"ExcelWrite data exception :{ex.ToString()},sheetName:{sheetName},data:{JsonConvert.SerializeObject(data)}");
throw ex;
}
} private bool WriteRequestCheck(string excelPath)
{
Func<string, bool> pathValidCheck = (path) =>
{
if (string.IsNullOrWhiteSpace(path) || !Path.IsPathRooted(path)) return false;
return true;
}; if (!pathValidCheck(excelPath))
{
_logger.Warn($"excelPath not valid,path :{excelPath}");
return false;
}
return true;
} private ExcelWorksheet AddSheet(ExcelPackage package, string sheetName)
{
if (package.Workbook.Worksheets[sheetName] != null)
{
package.Workbook.Worksheets.Delete(sheetName);
}
var sheet = package.Workbook.Worksheets.Add(sheetName);
return sheet;
} private byte[] SaveData(List<T> data, string sheetName)
{
byte[] excelContent = new byte[] { }; using (ExcelPackage package = new ExcelPackage())
{
ExcelWorksheet sheet = AddSheet(package, sheetName); //write data to excel
GetColumnIndexDic(sheet);
WriteContent(data, sheet); //set style for excel
SetHeadStyle(sheet, Color.FromArgb(255, 242, 204));
SetAllCellsStyle(sheet); //save
using (System.IO.MemoryStream outStream = new System.IO.MemoryStream())
{
package.SaveAs(outStream);
excelContent = outStream.ToArray();
}
}
return excelContent;
}
// type T => init excel's header && get some setting info to write excel cell
private void GetColumnIndexDic(ExcelWorksheet sheet,bool writeHeader=true)
{
try
{
var typeOfObject = typeof(T);
var pds = typeOfObject.GetProperties();
if (pds == null)
{
_logger.Warn($"no PropertyInfos can get from class Type:{typeOfObject.FullName} ");
return;
} //Dictionary<excel column index,T's PropertyInfo>
_columnIndexDicForProperInfo = new Dictionary<int, PropertyInfo>();
//Dictionary<excel column index,T's PropertyInfo's DisplayAttribute(its name=excle header name,its discription =excel cell style formate)>
_columnIndexDicForDisplayAttr = new Dictionary<int, DisplayAttribute>();
int column = 1;
int row = 1;
foreach (var p in pds)
{
var attr = p.GetCustomAttribute(typeof(DisplayAttribute)) as DisplayAttribute;
if (attr != null)
{
_columnIndexDicForDisplayAttr.Add(column, attr);
}
else
{
_logger.Warn($"no DisplayAttribute can get from PropertyInfo:(class:{typeOfObject.FullName},property:{p.Name})");
} if (writeHeader)
{
sheet.Cells[row, column].Value = attr == null ? p.Name : attr.Name;
}
_columnIndexDicForProperInfo.Add(column, p);
column++;
} if (_columnIndexDicForProperInfo.Count == 0)
{
_logger.Warn($"no _columnIndexDicForProperInfo can get from type:{typeOfObject.FullName}");
}
if (_columnIndexDicForDisplayAttr.Count == 0)
{
_logger.Warn($"no _columnIndexDicForDisplayAttr can get from type:{typeOfObject.FullName}");
}
}
catch (Exception ex)
{
_logger.Error($"ExcelWrite-GetColumnIndexDic exception :{ex.ToString()}");
throw ex;
}
}
//fill sheet content according list data
private void WriteContent(List<T> data,ExcelWorksheet sheet,int startRow=2)
{
try
{
PropertyInfo propertyTemp = null;
DisplayAttribute displayAttrTemp = null;
int column = 1;
int row = startRow;
Dictionary<string, Dictionary<string, DisplayAttribute>> enumDic = new Dictionary<string, Dictionary<string, DisplayAttribute>>();
foreach (var eachData in data)
{
column = 1;
foreach (var eachColumn in _columnIndexDicForProperInfo)
{
if (!_columnIndexDicForProperInfo.ContainsKey(column))
{
_logger.Warn($"no PropertyInfos can get from _columnIndexDic. current column:{column},_columnIndexDic:{JsonConvert.SerializeObject(_columnIndexDicForProperInfo)} ");
continue;
}
propertyTemp = _columnIndexDicForProperInfo[column];
var cellValue = propertyTemp.GetValue(eachData); if (cellValue != null)
{
FormatCellValue(ref enumDic, ref cellValue, propertyTemp.PropertyType);
}
sheet.Cells[row, column].Value = cellValue == null ? "" : cellValue;
if (_columnIndexDicForDisplayAttr.ContainsKey(column))
{
displayAttrTemp = _columnIndexDicForDisplayAttr[column];
var styleFormate = displayAttrTemp.Description;
if (!string.IsNullOrWhiteSpace(styleFormate))
{
sheet.Cells[row, column].Style.Numberformat.Format = styleFormate;
}
}
column++;
}
row++;
}
}
catch (Exception ex)
{
_logger.Error($"ExcelWrite-WriteContent exception :{ex.ToString()},data:{JsonConvert.SerializeObject(data)}");
throw ex;
}
} // formate cell value according type T‘s property’s DisplayAttribute
private void FormatCellValue(ref Dictionary<string, Dictionary<string, DisplayAttribute>> enumDic, ref object cellValue, Type propertyTypeOfCell)
{
if (cellValue == null) return;
if (propertyTypeOfCell.IsEnum)
{
Dictionary<string, DisplayAttribute> enumDicTemp;
if (enumDic.ContainsKey(propertyTypeOfCell.FullName))
{
enumDicTemp = enumDic[propertyTypeOfCell.FullName];
}
else
{
enumDicTemp = GetEnumNameDicForDisplayAttr(propertyTypeOfCell);
enumDic.Add(propertyTypeOfCell.FullName, enumDicTemp);
} if (enumDicTemp != null)
{
if (enumDicTemp.ContainsKey(cellValue.ToString()))
{
cellValue = enumDicTemp[cellValue.ToString()].Name;
return;
}
else
{
_logger.Warn($"no enum value can get from enum dictionary:{JsonConvert.SerializeObject(enumDicTemp.Keys)} , enum Type:{propertyTypeOfCell.FullName},cell value:{cellValue}");
}
}
else
{
_logger.Warn($"no enum dictionary can get from enum Type:{propertyTypeOfCell.FullName} ");
}
return;
}
/*if (propertyTypeOfCell == typeof(int))
{
cellValue = Convert.ToInt32(cellValue);
return;
}
if (propertyTypeOfCell == typeof(long))
{
cellValue = Convert.ToInt64(cellValue);
return;
}
if (propertyTypeOfCell == typeof(DateTime))
{
cellValue = Convert.ToDateTime(cellValue);
return;
}
if (propertyTypeOfCell == typeof(string))
{
cellValue = cellValue.ToString();
return;
}*/
return; } // get enum property Dic<enum value,DisplayAttribute> =>show in excel cell
private Dictionary<string, DisplayAttribute> GetEnumNameDicForDisplayAttr(Type enumClassType)
{
try
{
var result = new Dictionary<string, DisplayAttribute>();
if (enumClassType.IsEnum)
{
var enumValues = enumClassType.GetEnumValues();
foreach (var value in enumValues)
{
MemberInfo memberInfo =
enumClassType.GetMember(value.ToString()).First();
var descriptionAttribute =
memberInfo.GetCustomAttribute<DisplayAttribute>();
if (descriptionAttribute != null)
{
var enumString = Enum.GetName(enumClassType, value);
result.Add(value.ToString(), descriptionAttribute);
}
}
if (result == null || result.Count() == 0)
{
_logger.Warn($"no EnumDic can get from enum Type:{enumClassType.FullName} ");
}
}
return result;
}
catch (Exception ex)
{
_logger.Error($"ExcelWrite-GetEnumNameDicForDisplayAttr exception :{ex.ToString()},Type:{enumClassType.FullName}");
throw ex;
}
}
private bool WriteRequestCheck(string excelPath)
{
Func<string, bool> pathValidCheck = (path) =>
{
if (string.IsNullOrWhiteSpace(path) || !Path.IsPathRooted(path)) return false;
return true;
}; if (!pathValidCheck(excelPath))
{
_logger.Warn($"excelPath not valid,path :{excelPath}");
return false;
}
return true;
} private ExcelWorksheet AddSheet(ExcelPackage package, string sheetName)
{
if (package.Workbook.Worksheets[sheetName] != null)
{
package.Workbook.Worksheets.Delete(sheetName);
}
var sheet = package.Workbook.Worksheets.Add(sheetName);
return sheet;
}
enum 定义:
public enum AdvertiseType:Int32
{
/// <summary>
/// Search
/// </summary>
[Display(Name = "Search")]//important
Search = 1, /// <summary>
/// Display
/// </summary>
[Display(Name = "Display")]
Display = 2,
}
type T 的定义
public class FinancialBillEntity
{ [Display(Name = "类型")]
public AdvertiseType AdvertiseType{ get; set; } [Display(Name = "总金额", Description = "#,##0.00")]//name==excel header name;discription=cell style formate
public decimal TotalAdivitisingCost{ get; set; } [Display(Name = "赠送", Description = "#,##0.00")]
public decimal PromotionAmountUSD { get; set; }
}
应用:
//register interface
services.RegisterServiceR<IExcelWriteService<FinancialBillEntity>, ExcelWriteReadAccordingDisplayService<FinancialBillEntity>>(lifeStyle); //get interface instance
var excelWriteService= services.GetInstance<IExcelWriteService<FinancialBillEntity>>(); //execute interface method
bool result=_excelWriteService.WriteData(financeBills,cmdOptions.OutputFinanceBillExcelPath,cmdOptions.OutputFinanceBillSheetName);
EPPlus实战篇——Excel写入的更多相关文章
- EPPlus实战篇——Excel读取
.net core 项目 可以从excel读取任何类型(T)的数据,只要T中的field的[Display(Name = "1233")]中的name==excel column ...
- python基础 实战作业 ---Excel基本读写与数据处理
代码地址如下:http://www.demodashi.com/demo/11650.html 看完本篇需要: 10min 作业练习需要: 0.5h~3h(依练习者对python熟悉程度而定) 看完本 ...
- 二、Redis基本操作——String(实战篇)
小喵万万没想到,上一篇博客,居然已经被阅读600次了!!!让小喵感觉压力颇大.万一有写错的地方,岂不是会误导很多筒子们.所以,恳请大家,如果看到小喵的博客有什么不对的地方,请尽快指正!谢谢! 小喵的唠 ...
- 2天驾驭DIV+CSS (实战篇)(转)
这是去年看到的一片文章,感觉在我的学习中,有不少的影响.于是把它分享给想很快了解css的兄弟们.本文是实战篇. 基础篇[知识一] “DIV+CSS” 的叫法是不准确的[知识二] “DIV+CSS” ...
- EpPlus读取生成Excel帮助类+读取csv帮助类+Aspose.Cells生成Excel帮助类
大部分功能逻辑都在,少量自定义异常类和扩展方法 ,可用类似代码自己替换 //EpPlus读取生成Excel帮助类+读取csv帮助类,epplus只支持开放的Excel文件格式:xlsx,不支持 xls ...
- 构建NetCore应用框架之实战篇(七):BitAdminCore框架登录功能源码解读
本篇承接上篇内容,如果你不小心点击进来,建议从第一篇开始完整阅读,文章内容继承性连贯性. 构建NetCore应用框架之实战篇系列 一.简介 1.登录功能完成后,框架的雏形已经形成,有必要进行复习. 2 ...
- SQL Server ->> 高可用与灾难恢复(HADR)技术 -- AlwaysOn(实战篇)之AlwaysOn可用性组搭建
因为篇幅原因,AlwaysOn可用性组被拆成了两部分:理论部分和实战部分.而实战部分又被拆成了准备工作和AlwaysOn可用性组搭建. 三篇文章各自的链接: SQL Server ->> ...
- Redis实战篇
Redis实战篇 1 Redis 客户端 1.1 客户端通信 原理 客户端和服务器通过 TCP 连接来进行数据交互, 服务器默认的端口号为 6379 . 客户端和服务器发送的命令或数据一律以 \r\n ...
- MySQL 5.7主从复制实战篇
MySQL 5.7主从复制实战篇 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.安装MySQL数据库并启动 1>.在MySQL官方下载相应的安装包(https://dev ...
随机推荐
- 【CDH学习之二】ClouderaManager安装
环境 虚拟机:VMware 10 Linux版本:CentOS-6.5-x86_64 客户端:Xshell4 FTP:Xftp4 jdk8 zookeeper-3.4.11 搭建方案: serve ...
- caffe生成voc格式lmdb
要训练ssd基本都是在liu wei框架下改,生成lmdb这一关照葫芦画瓢总遇坑,记录之: 1. labelmap_voc.prototxt要根据自己的分类修改,比如人脸检测改成这样: item { ...
- Vue + vant-UI 打造移动商城
- flask 在视图函数中验证表单
在视图函数中验证表单 因为现在的basic_form视图同时接受两种类型的请求:GET请求和POST请求.所以我们要根据请求方法的不同执行不同的代码.具体来说,首先是实例化表单,如果是GET请求,就渲 ...
- SpringMVC中参数接收
/** * * SpringMVC中参数接收 * 1.接收简单类型 int String * 2.可以使用对象pojo接收 * 3.可以使用集合数据接收参数 * 页面: name="ids ...
- Golang通过git clone beego框架报错 error: while accessing https://github.com/astaxie/beego/info/refs fatal: HTTP request failed package github.com/astaxie/beego: exit status 128
在Centos6.4尝试搭建beego框架,使用git命令clone时报错 # cd .; git clone https://github.com/astaxie/beego /www/projec ...
- P5290 [十二省联考2019]春节十二响(堆+启发式合并)
P5290 [十二省联考2019]春节十二响 从特殊到一般 我们先看链的情况. 我们把点$1$左右的两条子链分别扔入堆里 每次取出两个堆的最大值,把答案累加上更大的那个(另一堆为空则直接加上去). 那 ...
- HTTP小幺鸡接口管理工具安装与配置说明
http://www.xiaoyaoji.cn/doc/TxybXPTdx 小幺鸡接口管理工具安装说明 使用可以参考:https://blog.csdn.net/qincidong/article/d ...
- 03: pip使用
1.1 pip常用方法 1.自我升级(升级pip到最新版本) pip install --upgrade pip 2. 安装库 pip install XXX 3. 查看当前环境所有已安装的库 pip ...
- linux配置powerline(bash/vim)美化
安装powerline需要pip 链接:https://pan.baidu.com/s/1Jc59VD35PYic2fTK5v8h1w 密码:otfp pip curl https://bootstr ...