| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295 |
- using Abp.Logging;
- using NPOI.HSSF.UserModel;
- using NPOI.HSSF.Util;
- using NPOI.SS.UserModel;
- using NPOI.SS.Util;
- using NPOI.XSSF.UserModel;
- using System.Collections;
- using System.Linq.Expressions;
- using System.Reflection;
- using System.Runtime.CompilerServices;
- using System.Security.Cryptography;
- using System.Text;
- using System.Text.RegularExpressions;
- namespace VberZero.Tools.FileHelpers;
- public static class ExcelHelper
- {
- #region Excel导入
- /// <summary>
- /// 从Excel取数据并记录到List集合里
- /// </summary>
- /// <param name="cellHeader">单元头的值和名称:{ { "UserName", "姓名" }, { "Age", "年龄" } };</param>
- /// <param name="filePath">保存文件绝对路径</param>
- /// <param name="errMsg">错误信息</param>
- /// <param name="startIndex">数据行开始序列,默认为1(即第二列,从0开始)</param>
- /// <returns>转换后的List对象集合</returns>
- public static List<T> ExcelToEntityList<T>(this Dictionary<string, string> cellHeader, string filePath, out string errMsg, int startIndex = 1) where T : new()
- {
- List<T> enlist = new List<T>();
- var errorMsg = new StringBuilder();
- try
- {
- if (Regex.IsMatch(filePath, ".xls$")) // 2003
- {
- enlist = Excel2003ToEntityList<T>(cellHeader, filePath, out errorMsg, startIndex);
- }
- else if (Regex.IsMatch(filePath, ".xlsx$")) // 2007
- {
- enlist = Excel2007ToEntityList<T>(cellHeader, filePath, out errorMsg, startIndex);
- }
- }
- catch (Exception ex)
- {
- errMsg = ex.Message;
- LogHelper.LogException(ex);
- return default;
- }
- errMsg = errorMsg.ToString();
- return enlist;
- }
- /// <summary>
- /// 从Excel2003取数据并记录到List集合里
- /// </summary>
- /// <param name="cellHeader">单元头的Key和Value:{ { "UserName", "姓名" }, { "Age", "年龄" } };</param>
- /// <param name="filePath">保存文件绝对路径</param>
- /// <param name="errorMsg">错误信息</param>
- /// <param name="startIndex"></param>
- /// <returns>转换好的List对象集合</returns>
- private static List<T> Excel2003ToEntityList<T>(this Dictionary<string, string> cellHeader, string filePath, out StringBuilder errorMsg, int startIndex = 1) where T : new()
- {
- errorMsg = new StringBuilder(); // 错误信息,Excel转换到实体对象时,会有格式的错误信息
- List<T> enlist = new List<T>(); // 转换后的集合
- try
- {
- using (FileStream fs = File.OpenRead(filePath))
- {
- HSSFWorkbook workbook = new HSSFWorkbook(fs);
- HSSFSheet sheet = (HSSFSheet)workbook.GetSheetAt(0); // 获取此文件第一个Sheet页
- for (int rowIndex = startIndex; rowIndex <= sheet.LastRowNum; rowIndex++)
- {
- // 1.判断当前行是否空行,若空行就不在进行读取下一行操作,结束Excel读取操作
- IRow row = sheet.GetRow(rowIndex);
- if (row == null)
- {
- break;
- }
- // 2.每一个Excel row转换为一个实体对象
- T en = new T();
- ExcelRowToEntity(cellHeader, row, rowIndex, en, ref errorMsg);
- enlist.Add(en);
- }
- }
- return enlist;
- }
- catch (Exception ex)
- {
- LogHelper.LogException(ex);
- return default;
- }
- }
- /// <summary>
- /// 从Excel2007取数据并记录到List集合里
- /// </summary>
- /// <param name="cellHeader">单元头的Key和Value:{ { "UserName", "姓名" }, { "Age", "年龄" } };</param>
- /// <param name="filePath">保存文件绝对路径</param>
- /// <param name="errorMsg">错误信息</param>
- /// <param name="startIndex">数据行开始序列,默认为1(即第二列,从0开始)</param>
- /// <returns>转换好的List对象集合</returns>
- private static List<T> Excel2007ToEntityList<T>(this Dictionary<string, string> cellHeader, string filePath, out StringBuilder errorMsg, int startIndex = 1)
- where T : new()
- {
- errorMsg = new StringBuilder(); // 错误信息,Excel转换到实体对象时,会有格式的错误信息
- List<T> enlist = new List<T>(); // 转换后的集合
- try
- {
- using (FileStream fs = File.OpenRead(filePath))
- {
- XSSFWorkbook workbook = new XSSFWorkbook(fs);
- XSSFSheet sheet = (XSSFSheet)workbook.GetSheetAt(0); // 获取此文件第一个Sheet页
- for (int rowIndex = startIndex; rowIndex <= sheet.LastRowNum; rowIndex++)
- {
- // 1.判断当前行是否空行,若空行就不在进行读取下一行操作,结束Excel读取操作
- IRow row = sheet.GetRow(rowIndex);
- if (row == null)
- {
- break;
- }
- // 2.每一个Excel row转换为一个实体对象
- T en = new T();
- ExcelRowToEntity(cellHeader, row, rowIndex, en, ref errorMsg);
- enlist.Add(en);
- }
- }
- return enlist;
- }
- catch (Exception ex)
- {
- LogHelper.LogException(ex);
- return default(List<T>);
- }
- }
- #endregion Excel导入
- #region Excel导出
- /// <summary>
- /// 实体类集合导出到EXCEL2003
- /// </summary>
- /// <param name="cellHeader">单元头的Key和Value:{ { "UserName", "姓名" }, { "Age", "年龄" } };</param>
- /// <param name="enList">数据源</param>
- /// <param name="sheetName">工作表名称</param>
- /// <param name="filePath">文件的下载地址</param>
- /// <returns></returns>
- public static string EntityListToExcel2003(this Dictionary<string, string> cellHeader, IList enList, string sheetName, string filePath)
- {
- var lcRetVal = "error@";
- try
- {
- string fileName = $"D-{sheetName}-{DateTime.Now:yyyyMMddHHmmssfff}.xls"; // 文件名称
- filePath = filePath.StartsWith("/") ? filePath : ("/" + filePath);
- filePath = filePath.EndsWith("/") ? filePath : (filePath + "/");
- string path = $"{AppDomain.CurrentDomain.BaseDirectory}{filePath}";
- if (!Directory.Exists(path))
- Directory.CreateDirectory(path);
- // 2.解析单元格头部,设置单元头的中文名称
- HSSFWorkbook workbook = new HSSFWorkbook(); // 工作簿
- ISheet sheet = workbook.CreateSheet(sheetName); // 工作表
- IRow row = sheet.CreateRow(0);
- List<string> keys = cellHeader.Keys.ToList();
- for (int i = 0; i < keys.Count; i++)
- {
- row.CreateCell(i).SetCellValue(cellHeader[keys[i]]); // 列名为Key的值
- }
- // 3.List对象的值赋值到Excel的单元格里
- int rowIndex = 1; // 从第二行开始赋值(第一行已设置为单元头)
- object propertyValue = null;
- foreach (var en in enList)
- {
- IRow rowTmp = sheet.CreateRow(rowIndex);
- for (int i = 0; i < keys.Count; i++) // 根据指定的属性名称,获取对象指定属性的值
- {
- string cellValue = "";
- PropertyInfo propertyInfo = null; // 属性的信息
- // 3.1 若属性头的名称包含'.',就表示是子类里的属性,那么就要遍历子类,eg:UserEn.UserName
- string[] propertyArray = keys[i].Split(new[] { "." }, StringSplitOptions.RemoveEmptyEntries);
- if (keys[i].IndexOf(".", StringComparison.Ordinal) >= 0)
- {
- // 3.1.1 解析子类属性(这里只解析1层子类,多层子类未处理)
- string subClassName = propertyArray[0]; // '.'前面的为子类的名称
- string subClassPropertyName = propertyArray[1]; // '.'后面的为子类的属性名称
- PropertyInfo subClassInfo = en.GetType().GetProperty(subClassName); // 获取子类的类型
- if (subClassInfo != null)
- {
- // 3.1.2 获取子类的实例
- var subClassEn = en.GetType().GetProperty(subClassName)?.GetValue(en, null);
- // 3.1.3 根据属性名称获取子类里的属性类型
- propertyInfo = subClassInfo.PropertyType.GetProperty(subClassPropertyName);
- if (propertyInfo != null)
- {
- propertyValue = propertyInfo.GetValue(subClassEn, null); // 获取子类属性的值
- }
- }
- }
- else
- {
- // 3.2 若不是子类的属性,直接根据属性名称获取对象对应的属性
- propertyInfo = en.GetType().GetProperty(keys[i]);
- if (propertyInfo != null)
- {
- propertyValue = propertyInfo.GetValue(en, null);
- }
- }
- // 3.3 属性值经过转换赋值给单元格值
- if (propertyValue != null)
- {
- if (propertyInfo?.PropertyType.Name == "int" || propertyInfo?.PropertyType.Name == "float" || propertyInfo?.PropertyType.Name == "double" || propertyInfo?.PropertyType.Name == "decimal")
- {
- var value = Convert.ToDouble(propertyValue);
- rowTmp.CreateCell(i).SetCellValue(value);
- }
- else if (propertyInfo?.PropertyType.Name == "bool")
- {
- var value = Convert.ToBoolean(propertyValue);
- rowTmp.CreateCell(i).SetCellValue(value);
- }
- else
- {
- cellValue = propertyValue.ToString();
- // 3.3.1 对时间初始值赋值为空
- if (cellValue.Trim() == "0001/1/1 0:00:00" || cellValue.Trim() == "0001/1/1 23:59:59")
- {
- cellValue = "";
- }
- // 3.4 填充到Excel的单元格里
- rowTmp.CreateCell(i).SetCellValue(cellValue);
- }
- }
- else
- {
- // 3.4 填充到Excel的单元格里
- rowTmp.CreateCell(i).SetCellValue(cellValue);
- }
- }
- rowIndex++;
- }
- // 4.生成文件
- FileStream file = new FileStream($"{path}{fileName}", FileMode.Create);
- workbook.Write(file);
- file.Close();
- lcRetVal = $"{filePath}{fileName}";
- }
- catch (Exception ex)
- {
- lcRetVal += ex.Message;
- LogHelper.LogException(ex);
- }
- return lcRetVal;
- }
- public static string EntityListToExcel2003(this HSSFWorkbook workbook,
- string sheetName, string filePath)
- {
- var lcRetVal = "error@";
- try
- {
- string fileName = $"D-{sheetName}-{DateTime.Now:yyyyMMddHHmmssfff}.xls"; // 文件名称
- filePath = filePath.StartsWith("/") ? filePath : ("/" + filePath);
- filePath = filePath.EndsWith("/") ? filePath : (filePath + "/");
- string path = $"{AppDomain.CurrentDomain.BaseDirectory}{filePath}";
- if (!Directory.Exists(path))
- Directory.CreateDirectory(path);
- FileStream file = new FileStream($"{path}{fileName}", FileMode.Create);
- workbook.Write(file);
- file.Close();
- lcRetVal = $"{filePath}{fileName}";
- }
- catch (Exception ex)
- {
- lcRetVal += ex.Message;
- LogHelper.LogException(ex);
- }
- return lcRetVal;
- }
- public static HSSFWorkbook EntityListToExcel2003Book(this Dictionary<string, string> cellHeader, IList enList,
- string sheetName)
- {
- HSSFWorkbook workbook = new HSSFWorkbook(); // 工作簿
- ISheet sheet = workbook.CreateSheet(sheetName); // 工作表
- IRow row = sheet.CreateRow(0);
- List<string> keys = cellHeader.Keys.ToList();
- for (int i = 0; i < keys.Count; i++)
- {
- row.CreateCell(i).SetCellValue(cellHeader[keys[i]]); // 列名为Key的值
- }
- // 3.List对象的值赋值到Excel的单元格里
- int rowIndex = 1; // 从第二行开始赋值(第一行已设置为单元头)
- foreach (var en in enList)
- {
- IRow rowTmp = sheet.CreateRow(rowIndex);
- for (int i = 0; i < keys.Count; i++) // 根据指定的属性名称,获取对象指定属性的值
- {
- string cellValue = ""; // 单元格的值
- object propertyValue = null; // 属性的值
- PropertyInfo propertyInfo; // 属性的信息
- // 3.1 若属性头的名称包含'.',就表示是子类里的属性,那么就要遍历子类,eg:UserEn.UserName
- if (keys[i].IndexOf(".", StringComparison.Ordinal) >= 0)
- {
- // 3.1.1 解析子类属性(这里只解析1层子类,多层子类未处理)
- string[] propertyArray = keys[i].Split(new[] { "." }, StringSplitOptions.RemoveEmptyEntries);
- string subClassName = propertyArray[0]; // '.'前面的为子类的名称
- string subClassPropertyName = propertyArray[1]; // '.'后面的为子类的属性名称
- PropertyInfo subClassInfo = en.GetType().GetProperty(subClassName); // 获取子类的类型
- if (subClassInfo != null)
- {
- // 3.1.2 获取子类的实例
- var subClassEn = en.GetType().GetProperty(subClassName)?.GetValue(en, null);
- // 3.1.3 根据属性名称获取子类里的属性类型
- propertyInfo = subClassInfo.PropertyType.GetProperty(subClassPropertyName);
- if (propertyInfo != null)
- {
- propertyValue = propertyInfo.GetValue(subClassEn, null); // 获取子类属性的值
- }
- }
- }
- else
- {
- // 3.2 若不是子类的属性,直接根据属性名称获取对象对应的属性
- propertyInfo = en.GetType().GetProperty(keys[i]);
- if (propertyInfo != null)
- {
- propertyValue = propertyInfo.GetValue(en, null);
- }
- }
- // 3.3 属性值经过转换赋值给单元格值
- if (propertyValue != null)
- {
- cellValue = propertyValue.ToString();
- // 3.3.1 对时间初始值赋值为空
- if (cellValue.Trim() == "0001/1/1 0:00:00" || cellValue.Trim() == "0001/1/1 23:59:59")
- {
- cellValue = "";
- }
- }
- // 3.4 填充到Excel的单元格里
- rowTmp.CreateCell(i).SetCellValue(cellValue);
- }
- rowIndex++;
- }
- return workbook;
- }
- /// <summary>
- /// 实体类集合导出到EXCEL2007
- /// </summary>
- /// <param name="cellHeader">单元头的Key和Value:{ { "UserName", "姓名" }, { "Age", "年龄" } };</param>
- /// <param name="enList">数据源</param>
- /// <param name="sheetName">工作表名称</param>
- /// <param name="filePath">文件的下载地址</param>
- /// <returns></returns>
- public static string EntityListToExcel2007(this Dictionary<string, string> cellHeader, IList enList, string sheetName, string filePath)
- {
- var lcRetVal = "error@";
- try
- {
- string fileName = $"D-{sheetName}-{DateTime.Now:yyyyMMddHHmmssfff}.xlsx"; // 文件名称
- filePath = filePath.StartsWith("/") ? filePath : ("/" + filePath);
- filePath = filePath.EndsWith("/") ? filePath : (filePath + "/");
- string path = $"{AppDomain.CurrentDomain.BaseDirectory}{filePath}";
- if (!Directory.Exists(path))
- Directory.CreateDirectory(path);
- // 2.解析单元格头部,设置单元头的中文名称
- XSSFWorkbook workbook = new XSSFWorkbook(); // 工作簿
- ISheet sheet = workbook.CreateSheet(sheetName); // 工作表
- IRow row = sheet.CreateRow(0);
- List<string> keys = cellHeader.Keys.ToList();
- for (int i = 0; i < keys.Count; i++)
- {
- row.CreateCell(i).SetCellValue(cellHeader[keys[i]]); // 列名为Key的值
- }
- // 3.List对象的值赋值到Excel的单元格里
- int rowIndex = 1; // 从第二行开始赋值(第一行已设置为单元头)
- foreach (var en in enList)
- {
- IRow rowTmp = sheet.CreateRow(rowIndex);
- for (int i = 0; i < keys.Count; i++) // 根据指定的属性名称,获取对象指定属性的值
- {
- string cellValue = ""; // 单元格的值
- object propertyValue = null; // 属性的值
- PropertyInfo propertyInfo = null; // 属性的信息
- // 3.1 若属性头的名称包含'.',就表示是子类里的属性,那么就要遍历子类,eg:UserEn.UserName
- if (keys[i].IndexOf(".", StringComparison.Ordinal) >= 0)
- {
- // 3.1.1 解析子类属性(这里只解析1层子类,多层子类未处理)
- string[] propertyArray = keys[i].Split(new[] { "." }, StringSplitOptions.RemoveEmptyEntries);
- string subClassName = propertyArray[0]; // '.'前面的为子类的名称
- var subClassPropertyName = propertyArray[1]; // '.'后面的为子类的属性名称
- PropertyInfo subClassInfo = en.GetType().GetProperty(subClassName); // 获取子类的类型
- if (subClassInfo != null)
- {
- // 3.1.2 获取子类的实例
- var subClassEn = en.GetType().GetProperty(subClassName)?.GetValue(en, null);
- // 3.1.3 根据属性名称获取子类里的属性类型
- propertyInfo = subClassInfo.PropertyType.GetProperty(subClassPropertyName);
- if (propertyInfo != null)
- {
- propertyValue = propertyInfo.GetValue(subClassEn, null); // 获取子类属性的值
- }
- }
- }
- else
- {
- // 3.2 若不是子类的属性,直接根据属性名称获取对象对应的属性
- propertyInfo = en.GetType().GetProperty(keys[i]);
- if (propertyInfo != null)
- {
- propertyValue = propertyInfo.GetValue(en, null);
- }
- }
- // 3.3 属性值经过转换赋值给单元格值
- if (propertyValue != null)
- {
- if (propertyInfo.PropertyType.Name.ToLower() == "int" || propertyInfo.PropertyType.Name.ToLower() == "int16" || propertyInfo.PropertyType.Name.ToLower() == "int32" || propertyInfo.PropertyType.Name.ToLower() == "int64" || propertyInfo.PropertyType.Name.ToLower() == "double" || propertyInfo.PropertyType.Name.ToLower() == "decimal")
- {
- var value = Convert.ToDouble(propertyValue);
- rowTmp.CreateCell(i).SetCellValue(value);
- }
- else if (propertyInfo.PropertyType.Name.ToLower() == "bool")
- {
- var value = Convert.ToBoolean(propertyValue);
- rowTmp.CreateCell(i).SetCellValue(value);
- }
- else if (propertyInfo.PropertyType.Name.ToLower() == "nullable`1")
- {
- if (propertyInfo.PropertyType.FullName == null)
- {
- }
- else if (propertyInfo.PropertyType.FullName.ToLower().Contains("datetime"))
- {
- cellValue = propertyValue.ToString();
- // 3.3.1 对时间初始值赋值为空
- if (cellValue.Trim() == "0001/1/1 0:00:00" || cellValue.Trim() == "0001/1/1 23:59:59")
- {
- cellValue = "";
- }
- rowTmp.CreateCell(i).SetCellValue(cellValue);
- }
- else if (propertyInfo.PropertyType.FullName.ToLower().Contains("int"))
- {
- var value = (int)Convert.ChangeType(propertyValue, typeof(int));
- rowTmp.CreateCell(i).SetCellValue(value);
- }
- else if (propertyInfo.PropertyType.FullName.ToLower().Contains("float") || propertyInfo.PropertyType.FullName.ToLower().Contains("double") || propertyInfo.PropertyType.FullName.ToLower().Contains("decimal") || propertyInfo.PropertyType.FullName.ToLower().Contains("single"))
- {
- var value = (float)Convert.ChangeType(propertyValue, typeof(float));
- rowTmp.CreateCell(i).SetCellValue(value);
- }
- else if (propertyInfo.PropertyType.FullName.ToLower().Contains("bool"))
- {
- var value = (bool)Convert.ChangeType(propertyValue, typeof(bool));
- rowTmp.CreateCell(i).SetCellValue(value);
- }
- }
- else
- {
- cellValue = propertyValue.ToString();
- // 3.3.1 对时间初始值赋值为空
- if (cellValue.Trim() == "0001/1/1 0:00:00" || cellValue.Trim() == "0001/1/1 23:59:59")
- {
- cellValue = "";
- }
- // 3.4 填充到Excel的单元格里
- rowTmp.CreateCell(i).SetCellValue(cellValue);
- }
- }
- else
- {
- // 3.4 填充到Excel的单元格里
- rowTmp.CreateCell(i).SetCellValue(cellValue);
- }
- }
- rowIndex++;
- }
- // 4.生成文件
- FileStream file = new FileStream($"{path}{fileName}", FileMode.Create);
- workbook.Write(file);
- file.Close();
- lcRetVal = $"{filePath}{fileName}";
- }
- catch (Exception ex)
- {
- lcRetVal += ex.Message;
- LogHelper.LogException(ex);
- }
- return lcRetVal;
- }
- #endregion Excel导出
- #region Common
- /// <summary>
- /// Excel row转换为实体对象
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="cellHeader">单元头的Key和Value:{ { "UserName", "姓名" }, { "Age", "年龄" } };</param>
- /// <param name="row">Excel row</param>
- /// <param name="rowIndex">row index</param>
- /// <param name="en">实体</param>
- /// <param name="errorMsg">错误信息</param>
- private static void ExcelRowToEntity<T>(Dictionary<string, string> cellHeader, IRow row, int rowIndex, T en, ref StringBuilder errorMsg)
- {
- List<string> keys = cellHeader.Keys.ToList(); // 要赋值的实体对象属性名称
- string errStr = ""; // 当前行转换时,是否有错误信息,格式为:第1行数据转换异常:XXX列;
- for (int i = 0; i < keys.Count; i++)
- {
- // 1.若属性头的名称包含'.',就表示是子类里的属性,那么就要遍历子类,eg:UserEn.TrueName
- if (keys[i].IndexOf(".", StringComparison.Ordinal) >= 0)
- {
- // 1)解析子类属性
- string[] propertyArray = keys[i].Split(new[] { "." }, StringSplitOptions.RemoveEmptyEntries);
- string subClassName = propertyArray[0]; // '.'前面的为子类的名称
- string classPropertyName = propertyArray[1]; // '.'后面的为子类的属性名称
- PropertyInfo subClassInfo = en.GetType().GetProperty(subClassName); // 获取子类的类型
- if (subClassInfo != null)
- {
- // 2)获取子类的实例
- var subClassEn = en.GetType().GetProperty(subClassName)?.GetValue(en, null);
- // 3)根据属性名称获取子类里的属性信息
- PropertyInfo propertyInfo = subClassInfo.PropertyType.GetProperty(classPropertyName);
- if (propertyInfo != null)
- {
- try
- {
- // Excel单元格的值转换为对象属性的值,若类型不对,记录出错信息
- propertyInfo.SetValue(subClassEn, GetExcelCellToProperty(propertyInfo.PropertyType, row.GetCell(i)), null);
- }
- catch (Exception ex)
- {
- LogHelper.LogException(ex);
- if (errStr.Length == 0)
- {
- errStr = "第" + rowIndex + "行数据转换异常:";
- }
- errStr += cellHeader[keys[i]] + "列;";
- }
- }
- }
- }
- else
- {
- // 2.给指定的属性赋值
- PropertyInfo propertyInfo = en.GetType().GetProperty(keys[i]);
- if (propertyInfo != null)
- {
- try
- {
- // Excel单元格的值转换为对象属性的值,若类型不对,记录出错信息
- propertyInfo.SetValue(en, GetExcelCellToProperty(propertyInfo.PropertyType, row.GetCell(i)), null);
- }
- catch (Exception ex)
- {
- LogHelper.LogException(ex);
- if (errStr.Length == 0)
- {
- errStr = "第" + rowIndex + "行数据转换异常:";
- }
- errStr += cellHeader[keys[i]] + "列;";
- }
- }
- }
- }
- // 若有错误信息,就添加到错误信息里
- if (errStr.Length > 0)
- {
- errorMsg.AppendLine(errStr);
- }
- }
- /// <summary>
- /// Excel Cell转换为实体的属性值
- /// </summary>
- /// <param name="distanceType">目标对象类型</param>
- /// <param name="sourceCell">对象属性的值</param>
- private static object GetExcelCellToProperty(Type distanceType, ICell sourceCell)
- {
- object rs = distanceType.IsValueType ? Activator.CreateInstance(distanceType) : null;
- // 1.判断传递的单元格是否为空
- if (sourceCell == null || string.IsNullOrEmpty(sourceCell.ToString()))
- {
- return rs;
- }
- // 2.Excel文本和数字单元格转换,在Excel里文本和数字是不能进行转换,所以这里预先存值
- object sourceValue = null;
- switch (sourceCell.CellType)
- {
- case CellType.Blank:
- break;
- case CellType.Boolean:
- sourceValue = sourceCell.BooleanCellValue;
- break;
- case CellType.Error:
- break;
- case CellType.Formula:
- break;
- case CellType.Numeric:
- sourceValue = sourceCell.NumericCellValue;
- break;
- case CellType.String:
- sourceValue = sourceCell.StringCellValue;
- break;
- case CellType.Unknown:
- break;
- default:
- sourceValue = sourceCell.StringCellValue;
- break;
- }
- string valueDataType = distanceType.Name;
- // 在这里进行特定类型的处理
- switch (valueDataType.ToLower()) // 以防出错,全部小写
- {
- case "string":
- rs = sourceValue?.ToString();
- break;
- case "int":
- case "int32":
- rs = (int)Convert.ChangeType(sourceValue?.ToString() ?? "", distanceType);
- break;
- case "int16":
- rs = (short)Convert.ChangeType(sourceValue?.ToString() ?? "", distanceType);
- break;
- case "int64":
- rs = (long)Convert.ChangeType(sourceValue?.ToString() ?? "", distanceType);
- break;
- case "float":
- case "single":
- rs = (float)Convert.ChangeType(sourceValue?.ToString() ?? "", distanceType);
- break;
- case "double":
- rs = (double)Convert.ChangeType(sourceValue?.ToString() ?? "", distanceType);
- break;
- case "decimal":
- rs = (decimal)Convert.ChangeType(sourceValue?.ToString() ?? "", distanceType);
- break;
- case "datetime":
- rs = (DateTime)Convert.ChangeType(sourceValue?.ToString() ?? "", distanceType);
- break;
- case "guid":
- rs = (Guid)Convert.ChangeType(sourceValue?.ToString() ?? "", distanceType);
- break;
- case "nullable`1":
- if (distanceType.FullName == null)
- {
- rs = "";
- }
- else if (distanceType.FullName.ToLower().Contains("datetime"))
- {
- rs = (DateTime)Convert.ChangeType(sourceValue?.ToString() ?? "", typeof(DateTime));
- }
- else if ((bool)distanceType.FullName?.ToLower().Contains("int"))
- {
- rs = (int)Convert.ChangeType(sourceValue?.ToString() ?? "", typeof(int));
- }
- else if ((bool)distanceType.FullName?.ToLower().Contains("float") || (bool)distanceType.FullName?.ToLower().Contains("double") || (bool)distanceType.FullName?.ToLower().Contains("decimal") || (bool)distanceType.FullName?.ToLower().Contains("single"))
- {
- rs = (float)Convert.ChangeType(sourceValue?.ToString() ?? "", typeof(float));
- }
- else if ((bool)distanceType.FullName?.ToLower().Contains("bool"))
- {
- rs = (bool)Convert.ChangeType(sourceValue?.ToString() ?? "", typeof(bool));
- }
- break;
- }
- return rs;
- }
- #endregion Common
- public static IWorkbook CreateWorkBook(string filePath = "")
- {
- return filePath.ToLower().EndsWith(".xlsx") ? CreateWorkBook07(filePath) :
- filePath.ToLower().EndsWith(".xls") ? (IWorkbook)CreateWorkBook03(filePath) : new XSSFWorkbook();
- }
- public static HSSFWorkbook CreateWorkBook03(string filePath = null)
- {
- if (string.IsNullOrEmpty(filePath))
- {
- return new HSSFWorkbook();
- }
- FileStream file = new FileStream(filePath, FileMode.Open);
- return new HSSFWorkbook(file);
- }
- public static XSSFWorkbook CreateWorkBook07(string filePath = null)
- {
- if (string.IsNullOrEmpty(filePath))
- {
- return new XSSFWorkbook();
- }
- return new XSSFWorkbook(filePath);
- }
- /// <summary>
- /// 创建Sheet
- /// </summary>
- /// <param name="sheetName"></param>
- /// <param name="defaultWidth"></param>
- /// <param name="defaultHeight"></param>
- /// <param name="is07"></param>
- /// <param name="workbook"></param>
- /// <returns></returns>
- public static ISheet GenerateSheet(this string sheetName, int defaultWidth = 20, int defaultHeight = 20, bool is07 = true, IWorkbook workbook = null)
- {
- workbook = is07 ? workbook ?? CreateWorkBook07() : workbook ?? CreateWorkBook03();
- var sheet = workbook.CreateSheet(sheetName);
- sheet.DefaultColumnWidth = defaultWidth;
- sheet.DefaultRowHeight = (short)(defaultHeight * 20);
- return sheet;
- }
- /// <summary>
- /// 创建行
- /// </summary>
- /// <param name="sheet">表</param>
- /// <param name="rowIndex">第几行(从1开始计数)</param>
- /// <returns></returns>
- public static IRow GenerateRow(this ISheet sheet, int rowIndex)
- {
- rowIndex = rowIndex <= 0 ? 1 : rowIndex;
- var row = sheet.GetRow(rowIndex - 1) ?? sheet.CreateRow(rowIndex - 1);
- return row;
- }
- /// <summary>
- /// 插入行
- /// </summary>
- /// <param name="sheet">表</param>
- /// <param name="startRow">第几行后开始插入(源行)</param>
- /// <param name="count">插入的行数</param>
- /// <returns></returns>
- public static void InsertRows(this ISheet sheet, int startRow, int count)
- {
- var rowSource = sheet.GenerateRow(startRow);
- sheet.ShiftRows(startRow, sheet.LastRowNum, count, true, false);
- if (rowSource == null)
- return;
- var rowStyle = rowSource.RowStyle;
- for (int i = startRow + 1; i <= startRow + count; i++)
- {
- var rowInsert = sheet.GenerateRow(i);
- rowInsert.RowStyle = rowStyle;
- rowInsert.Height = rowSource.Height;
- for (int col = 1; col <= rowSource.LastCellNum; col++)
- {
- var cellSource = rowSource.GenerateCell(col);
- var cellInsert = rowInsert.GenerateCell(col);
- var cellStyle = cellSource.CellStyle;
- //设置单元格样式
- if (cellStyle != null)
- cellInsert.CellStyle = cellSource.CellStyle;
- }
- }
- }
- /// <summary>
- /// 创建单元格
- /// </summary>
- /// <param name="sheet">表</param>
- /// <param name="rowIndex">第几行(从1开始计数)</param>
- /// <param name="columnIndex">第几列(从1开始计数)</param>
- /// <param name="val">值</param>
- /// <param name="cellType">单元格类型</param>
- /// <returns></returns>
- public static ICell GenerateCell(this ISheet sheet, int rowIndex, int columnIndex, string val = null, CellType cellType = CellType.String)
- {
- var row = sheet.GenerateRow(rowIndex);
- var cell = GenerateCell(row, columnIndex, val, cellType);
- return cell;
- }
- /// <summary>
- /// 创建单元格
- /// </summary>
- /// <param name="row">行</param>
- /// <param name="columnIndex">第几列(从1开始计数)</param>
- /// <param name="val">值</param>
- /// <param name="cellType">单元格类型</param>
- /// <returns></returns>
- public static ICell GenerateCell(this IRow row, int columnIndex, string val = null, CellType cellType = CellType.String)
- {
- columnIndex = columnIndex <= 0 ? 1 : columnIndex;
- var cell = row.GetCell(columnIndex - 1) ?? row.CreateCell(columnIndex - 1);
- if (!string.IsNullOrEmpty(val))
- {
- cell.SetCellValue(val);
- cell.SetCellType(cellType);
- }
- return cell;
- }
- /// <summary>
- /// 设置列宽度
- /// </summary>
- /// <param name="sheet">表</param>
- /// <param name="columnIndex">第几列(从1开始计数)</param>
- /// <param name="width">宽度</param>
- /// <returns></returns>
- public static ISheet SetCellWidth(this ISheet sheet, int columnIndex, int width)
- {
- sheet.SetColumnWidth(columnIndex - 1, width * 256);
- return sheet;
- }
- /// <summary>
- /// 设置列宽度
- /// </summary>
- /// <param name="row">行</param>
- /// <param name="columnIndex">第几列(从1开始计数)</param>
- /// <param name="width">宽度</param>
- /// <returns></returns>
- public static IRow SetCellWidth(this IRow row, int columnIndex, int width)
- {
- row.Sheet.SetColumnWidth(columnIndex - 1, width * 256);
- return row;
- }
- /// <summary>
- /// 设置列宽度
- /// </summary>
- /// <param name="cell">单元格</param>
- /// <param name="columnIndex">第几列(从1开始计数)</param>
- /// <param name="width">宽度</param>
- /// <returns></returns>
- public static ICell SetCellWidth(this ICell cell, int columnIndex, int width)
- {
- cell.Sheet.SetColumnWidth(columnIndex - 1, width * 256);
- return cell;
- }
- /// <summary>
- /// 设置行高度
- /// </summary>
- /// <param name="sheet">表</param>
- /// <param name="rowIndex">第几行(从1开始计数)</param>
- /// <param name="height">宽度</param>
- /// <returns></returns>
- public static ISheet SetRowHeight(this ISheet sheet, int rowIndex, int height)
- {
- sheet.GetRow(rowIndex - 1).SetRowHeight(height);
- return sheet;
- }
- /// <summary>
- /// 设置行高度
- /// </summary>
- /// <param name="row">行</param>
- /// <param name="height">宽度</param>
- /// <returns></returns>
- public static IRow SetRowHeight(this IRow row, int height)
- {
- row.Height = (short)(height * 20);
- return row;
- }
- /// <summary>
- /// 设置行高度
- /// </summary>
- /// <param name="cell">单元格</param>
- /// <param name="height">宽度</param>
- /// <returns></returns>
- public static ICell SetRowHeight(this ICell cell, int height)
- {
- cell.Row.SetRowHeight(height);
- return cell;
- }
- /// <summary>
- /// 设置单元格样式
- /// </summary>
- /// <param name="cell"></param>
- /// <param name="styleStr">样式字符串</param>
- /// <param name="instants">样式转换器</param>
- /// <returns></returns>
- public static ICell SetCellCss(this ICell cell, CellStyleCss instants = null, string styleStr = null)
- {
- instants = instants ?? CellStyleCss.Instants;
- return instants.Css(cell, styleStr);
- }
- /// <summary>
- /// 设置单元格的值
- /// </summary>
- /// <param name="cell"></param>
- /// <param name="obj"></param>
- /// <returns></returns>
- public static ICell SetValue(this ICell cell, string obj)
- {
- return SetValue<string>(cell, obj);
- }
- /// <summary>
- /// 设置单元格的值
- /// </summary>
- /// <typeparam name="T">值类型(sting,bool,double,DateTime)</typeparam>
- /// <param name="cell"></param>
- /// <param name="obj"></param>
- /// <returns></returns>
- public static ICell SetValue<T>(this ICell cell, object obj)
- {
- Type type = typeof(T);
- if (type == typeof(string))
- {
- cell.SetCellValue(obj.ToString());
- }
- else if (type == typeof(bool) && bool.TryParse(obj.ToString(), out var bValue))
- {
- cell.SetCellValue(bValue);
- }
- else if ((type == typeof(int) || type == typeof(double) || type == typeof(decimal)) && double.TryParse(obj.ToString(), out var iValue))
- {
- cell.SetCellValue(iValue);
- }
- else if (type == typeof(DateTime) && DateTime.TryParse(obj.ToString(), out var dValue))
- {
- cell.SetCellValue(dValue);
- }
- return cell;
- }
- #region DATA-FORMAT
- public static ICellStyle SetCellDateTime(this ICell cell, DateTime value, string formatStr = "yyyy-MM-dd HH:mm:ss")
- {
- cell.SetCellValue(value);
- cell.SetDataFormat(formatStr);
- return cell.CellStyle;
- }
- public static ICellStyle SetCellString(this ICell cell, double value, string formatStr = "@")
- {
- cell.SetCellValue(value);
- cell.SetDataFormat(formatStr);
- return cell.CellStyle;
- }
- public static ICellStyle SetCellDouble(this ICell cell, double value, string formatStr = "0.00")
- {
- cell.SetCellValue(value);
- cell.SetDataFormat(formatStr);
- return cell.CellStyle;
- }
- public static ICellStyle SetCellCurrency(this ICell cell, double value, string formatStr = "¥#,###.##")
- {
- cell.SetCellValue(value);
- cell.SetDataFormat(formatStr);
- return cell.CellStyle;
- }
- public static ICellStyle SetCellPercent(this ICell cell, double value, string formatStr = "0.00%")
- {
- cell.SetCellValue(value);
- cell.SetDataFormat(formatStr);
- return cell.CellStyle;
- }
- public static void SetDataFormat(this ICell cell, string formatStr = "¥#,##0.00")
- {
- var cellStyle = cell.CellStyle ?? GetCellStyle(cell);
- var format = cell.Sheet.Workbook.CreateDataFormat();
- cellStyle.DataFormat = format.GetFormat(formatStr);
- cell.SetCellStyle(cellStyle);
- }
- #endregion DATA-FORMAT
- #region 合并单元格
- /// <summary>
- /// 合并单元格
- /// </summary>
- /// <param name="sheet"></param>
- /// <param name="firstRow">开始行数(从1开始计数)</param>
- /// <param name="lastRow">结束行数(从1开始计数)</param>
- /// <param name="firstColumn">开始列数(从1开始计数)</param>
- /// <param name="lastColumn">结束列数(从1开始计数)</param>
- /// <returns></returns>
- public static ISheet MergedRegion(this ISheet sheet, int firstRow, int lastRow, int firstColumn, int lastColumn)
- {
- var region = GetCellRegion(firstRow, lastRow, firstColumn, lastColumn);
- sheet.MergedRegion(region);
- return sheet;
- }
- public static ISheet MergedRegion(this ISheet sheet, CellRangeAddress region)
- {
- sheet.AddMergedRegion(region);
- //SetRegionBorder(sheet, region, borderTypeStr, borderColorStr);
- return sheet;
- }
- public static CellRangeAddress GetCellRegion(this int firstRow, int lastRow, int firstColumn, int lastColumn)
- {
- firstRow = firstRow <= 0 ? 1 : firstRow;
- firstColumn = firstColumn <= 0 ? 1 : firstColumn;
- lastRow = lastRow <= 0 ? 1 : lastRow;
- lastColumn = lastColumn <= 0 ? 1 : lastColumn;
- return new CellRangeAddress(firstRow - 1, lastRow - 1, firstColumn - 1, lastColumn - 1);
- }
- public static ISheet SetRegionBorder(this CellRangeAddress region, ISheet sheet, string borderTypeStr, string borderColorStr)
- {
- ICellStyle style = sheet.Workbook.CreateCellStyle();
- borderTypeStr = borderTypeStr ?? "Thin";
- borderColorStr = borderColorStr ?? "Black";
- style.BorderColors(borderColorStr);
- style.BorderTypes(borderTypeStr);
- for (int i = region.FirstRow; i <= region.LastRow; i++)
- {
- IRow row = sheet.GenerateRow(i + 1);
- if (i == region.FirstRow)
- {
- }
- else if (i == region.LastRow)
- {
- }
- var leftCell = row.GenerateCell(region.FirstColumn + 1);
- leftCell.CellStyle.BorderLeft = style.BorderLeft;
- leftCell.CellStyle.LeftBorderColor = style.LeftBorderColor;
- var rightCell = row.GenerateCell(region.LastColumn + 1);
- rightCell.CellStyle.BorderRight = style.BorderRight;
- rightCell.CellStyle.RightBorderColor = style.RightBorderColor;
- //for (int j = region.FirstColumn; j <= region.LastColumn; j++)
- //{
- // ICell singleCell = row.GenerateCell((short)j);
- //}
- }
- return sheet;
- }
- #endregion 合并单元格
- public static ICellStyle GetCellStyle(this ICell cell)
- {
- return cell.CellStyle ?? cell.Sheet.Workbook.CreateCellStyle();
- }
- public static void SetCellStyle(this ICell cell, ICellStyle cellStyle)
- {
- cell.CellStyle = cellStyle;
- }
- /// <summary>
- /// 保存工作簿
- /// </summary>
- /// <param name="sheet"></param>
- /// <param name="filePath"></param>
- /// <param name="fileName"></param>
- /// <returns></returns>
- public static string SaveWorkBook(this ISheet sheet, string filePath, string fileName)
- {
- try
- {
- if (!Directory.Exists(filePath))
- {
- Directory.CreateDirectory(filePath);
- }
- filePath = filePath.EndsWith("\\") ? filePath : filePath + "\\";
- FileStream file = new FileStream(filePath + fileName, FileMode.Create);
- sheet.Workbook.Write(file);
- file.Close();
- return "";
- }
- catch (Exception e)
- {
- return e.Message;
- }
- }
- /// <summary>
- /// 保存工作簿
- /// </summary>
- /// <param name="workbook"></param>
- /// <param name="filePath"></param>
- /// <param name="fileName"></param>
- /// <returns></returns>
- public static string SaveWorkBook(this IWorkbook workbook, string filePath, string fileName)
- {
- try
- {
- if (!Directory.Exists(filePath))
- {
- Directory.CreateDirectory(filePath);
- }
- filePath = filePath.EndsWith("\\") ? filePath : filePath + "\\";
- FileStream file = new FileStream(filePath + fileName, FileMode.Create);
- workbook.Write(file);
- file.Close();
- return "";
- }
- catch (Exception e)
- {
- return e.Message;
- }
- }
- }
- public class CellStyleCss
- {
- public static CellStyleCss Instants => new CellStyleCss()
- {
- //缩写
- DefaultStyle = $"bgc:{ColorType.White.ToString()};" +
- $"warp:{HorizontalAlignment.Left.ToString()};" +
- $"align:{HorizontalAlignment.Left.ToString()};" +
- $"v-align:{VerticalAlignment.Center.ToString()};" +
- //$"b:{BorderStyle.None.ToString()};" +
- //$"bc:{ColorType.Black.ToString()};" +
- "inden:0;" +
- "df:@;"
- };
- public void SetDefaultStyle(string styleStr)
- {
- var dic = new SortedDictionary<string, string>();
- DefaultStyle = dic.GetCleanStyle("");
- DefaultStyle = dic.GetCleanStyle(styleStr);
- }
- /// <summary>
- /// 默认样式css
- /// </summary>
- private string DefaultStyle { get; set; }
- //标准写法
- //private static readonly string DefaultFontStyle = "font-color:black;" +
- // "font-name:Arial;" +
- // "font-size:10;" +
- // "font-weight:normal;" +
- // "font-underline:none;" +
- // "font-italic:false;" +
- // "font-strikeout:false;" +
- // "font-superscript:none;"+
- // "background-color:white;"+
- // "text-align:none;"+
- // "vertical-align:none;"+
- // "data-format:none;border-type:Thin";
- /// <summary>
- /// 把css样式设置给单元格
- /// </summary>
- /// <param name="cell"></param>
- /// <param name="styleStr"></param>
- /// <returns></returns>
- public ICell Css(ICell cell, string styleStr = null)
- {
- var dic = new SortedDictionary<string, string>();
- var sortedCss = dic.GetCleanStyle(DefaultStyle);
- if (!string.IsNullOrEmpty(styleStr))
- sortedCss = dic.GetCleanStyle(styleStr);
- var cssKey = $"CellStyle_{sortedCss.Md5()}";
- var workbook = cell.Sheet.Workbook;
- ICellStyle cellStyle = workbook.GetCellStyle(cssKey);
- if (cellStyle == null)
- {
- cellStyle = workbook.GetCellStyle(dic, cell);
- workbook.AttachedCellStyle(cssKey, cellStyle);
- }
- cell.CellStyle = cellStyle;
- //cell.CellStyle = workbook.GetCellStyle(dic, cell);
- return cell;//返回单元格方便流水式编程
- }
- public ICellStyle GetCssStyle(IWorkbook workbook, string styleStr)
- {
- var dic = new SortedDictionary<string, string>();
- var sortedCss = dic.GetCleanStyle(DefaultStyle);
- if (!string.IsNullOrEmpty(styleStr))
- sortedCss = dic.GetCleanStyle(styleStr);
- var cssKey = $"CellStyle_{sortedCss.Md5()}";
- var cellStyle = workbook.GetCellStyle(cssKey);
- if (cellStyle == null)
- {
- cellStyle = workbook.GetCellStyle(dic);
- workbook.AttachedCellStyle(cssKey, cellStyle);
- }
- return cellStyle;
- }
- }
- internal static class CellStyleRender
- {
- #region 解析css样式
- /// <summary>
- /// 默认字体样式
- /// </summary>
- private static string DefaultFontStyle { get; } = $"fc:{ColorType.Black.ToString()};" +//font-color
- "fn:宋体;" +//font-name
- "fs:12;" +//font-size
- "fw:normal;" +//font-weight
- "fu:none;" +//font-underline
- "fi:false;" +//font-italic
- "fst:false;" +//font-strikeout
- "fss:none;";//font-superscript
- #region 设置样式
- /// <summary>
- /// 缓存
- /// </summary>
- private static readonly ConditionalWeakTable<IWorkbook, Dictionary<string, ICellStyle>> Table =
- new ConditionalWeakTable<IWorkbook, Dictionary<string, ICellStyle>>();
- /// <summary>
- /// 获取CellStyle
- /// </summary>
- /// <param name="workbook"></param>
- /// <param name="dic"></param>
- /// <param name="cell"></param>
- /// <returns></returns>
- public static ICellStyle GetCellStyle(this IWorkbook workbook, SortedDictionary<string, string> dic, ICell cell = null)
- {
- ICellStyle cellStyle = workbook.CreateCellStyle();
- //if (cell != null)
- //{
- // cellStyle.CloneStyleFrom(cell.CellStyle);
- //}
- var fontStyles = dic.Where(w => w.Key.StartsWith("font-")).ToArray();
- var fontDic = new SortedDictionary<string, string>();
- foreach (var kv in fontStyles)
- {
- fontDic.Add(kv.Key, kv.Value);
- }
- var font = workbook.GetFont(fontDic);
- cellStyle.SetFont(font);//TODO 在基于style.xls基础的样式上增加css时,会造成原字体设置的丢失
- var xdic = dic.Where(w => !w.Key.StartsWith("font-")).ToArray();
- foreach (var kvp in xdic)
- {
- FireCssAccess(cellStyle, workbook, kvp);
- }
- return cellStyle;
- }
- /// <summary>
- /// 从缓存读取CellStyle
- /// </summary>
- /// <param name="workbook"></param>
- /// <param name="propertyName"></param>
- /// <returns></returns>
- public static ICellStyle GetCellStyle(this IWorkbook workbook, string propertyName)
- {
- if (!Table.TryGetValue(workbook, out var values)) return null;
- if (values.TryGetValue(propertyName, out var temp))
- return temp;
- return null;
- }
- /// <summary>
- /// 缓存CellStyle
- /// </summary>
- /// <param name="workbook"></param>
- /// <param name="propertyName"></param>
- /// <param name="value"></param>
- public static void AttachedCellStyle(this IWorkbook workbook, string propertyName, ICellStyle value)
- {
- if (!Table.TryGetValue(workbook, out var values))
- {
- values = new Dictionary<string, ICellStyle>();
- Table.Add(workbook, values);
- }
- values[propertyName] = value;
- }
- /// <summary>
- /// Md5 key
- /// </summary>
- /// <param name="input"></param>
- /// <returns></returns>
- public static string Md5(this string input)
- {
- if (input == null)
- input = string.Empty;
- byte[] data = Encoding.UTF8.GetBytes(input.Trim().ToLowerInvariant());
- using (var md5 = new MD5CryptoServiceProvider())
- {
- data = md5.ComputeHash(data);
- }
- var ret = new StringBuilder();
- foreach (byte b in data)
- {
- ret.Append(b.ToString("x2").ToLowerInvariant());
- }
- return ret.ToString();
- }
- /// <summary>
- /// 设置不是字体的样式
- /// </summary>
- /// <param name="style"></param>
- /// <param name="workbook"></param>
- /// <param name="kvp"></param>
- private static void FireCssAccess(ICellStyle style, IWorkbook workbook, KeyValuePair<string, string> kvp)
- {
- switch (kvp.Key)
- {
- case "WrapText":
- style.TextWrap(kvp.Value);
- break;
- case "Indention":
- style.TextIndention(kvp.Value);
- break;
- case "text-align":
- style.TextAlign(kvp.Value);
- break;
- case "vertical-align":
- style.VerticalAlign(kvp.Value);
- break;
- case "background-color":
- style.BackgroundColor(kvp.Value);
- break;
- case "border-type":
- style.BorderTypes(kvp.Value);
- break;
- case "top-border-type":
- style.BorderTopTypes(kvp.Value);
- break;
- case "right-border-type":
- style.BorderRightTypes(kvp.Value);
- break;
- case "bottom-border-type":
- style.BorderBottomTypes(kvp.Value);
- break;
- case "left-border-type":
- style.BorderLeftTypes(kvp.Value);
- break;
- case "border-color":
- style.BorderColors(kvp.Value);
- break;
- case "top-border-color":
- style.BorderTopColors(kvp.Value);
- break;
- case "right-border-color":
- style.BorderRightColors(kvp.Value);
- break;
- case "bottom-border-color":
- style.BorderBottomColors(kvp.Value);
- break;
- case "left-border-color":
- style.BorderLeftColors(kvp.Value);
- break;
- case "data-format":
- style.DataFormat(workbook, kvp.Value);
- break;
- }
- }
- /// <summary>
- /// 获取字体样式
- /// </summary>
- /// <param name="workbook"></param>
- /// <param name="fontdic"></param>
- /// <returns></returns>
- private static IFont GetFont(this IWorkbook workbook, SortedDictionary<string, string> fontdic)
- {
- var weight = fontdic.FontWeight();
- var color = fontdic.FontColor();
- var size = fontdic.FontSize();
- var name = fontdic.FontName();
- var underline = fontdic.FontUnderline();
- var italic = fontdic.FontItalic();
- var strikeout = fontdic.FontStrikeout();
- var offset = fontdic.ConvertToSuperScript();
- var findHeight = (short)(size * 20);
- var font = workbook.FindFont(weight, color, findHeight, name, italic, strikeout, offset, underline);
- if (font == null)
- {
- font = workbook.CreateFont();
- font.Boldweight = weight;
- font.Color = color;
- font.FontHeightInPoints = size;
- font.FontName = name;
- font.Underline = underline;
- font.IsItalic = italic;
- font.IsStrikeout = strikeout;
- font.TypeOffset = offset;
- }
- return font;
- }
- #endregion 设置样式
- #region 获取样式
- /// <summary>
- /// 默认设置
- /// </summary>
- /// <param name="dic"></param>
- public static void InitStyleDic(this SortedDictionary<string, string> dic)
- {
- var cssItems = GetCssItems(DefaultFontStyle);
- foreach (var cssitem in cssItems)
- {
- var kvp = GetCssKeyValue(cssitem);
- if (dic.ContainsKey(kvp.Key))
- dic[kvp.Key] = kvp.Value; //覆盖相同key的值
- else
- dic.Add(kvp.Key, kvp.Value);
- }
- }
- /// <summary>
- /// 获取样式简洁字符串
- /// </summary>
- /// <param name="dic"></param>
- /// <param name="style"></param>
- /// <returns></returns>
- public static string GetCleanStyle(this SortedDictionary<string, string> dic, string style)
- {
- style = Regex.Replace(style.Trim(), "\\s+", " ");
- style = Regex.Replace(style, "\\s;\\s", ";");
- style = Regex.Replace(style, "\\s:\\s", ":");
- InitStyleDic(dic);
- var cssItems = GetCssItems(style.TrimEnd(';'));
- foreach (var cssitem in cssItems)
- {
- var kvp = GetCssKeyValue(cssitem);
- if (dic.ContainsKey(kvp.Key))
- dic[kvp.Key] = kvp.Value; //覆盖相同key的值
- else
- dic.Add(kvp.Key, kvp.Value);
- }
- var sortedCss = string.Join(";", dic.Select(s => $"{s.Key}:{s.Value}").ToArray());
- return sortedCss;
- }
- /// <summary>
- /// 获取样式数组
- /// </summary>
- /// <param name="style"></param>
- /// <returns></returns>
- private static string[] GetCssItems(string style)
- {
- var cssItems = Regex.Split(style, ";");
- cssItems = cssItems.Where(w => !string.IsNullOrWhiteSpace(w)).ToArray();
- return cssItems;
- }
- /// <summary>
- /// 获取css样式
- /// </summary>
- /// <param name="css"></param>
- /// <returns></returns>
- private static KeyValuePair<string, string> GetCssKeyValue(string css)
- {
- var cssKeyValueArray = Regex.Split(css, ":").ToArray();
- var cssKey = cssKeyValueArray[0].StandardCssKey();
- var cssValue = cssKey == "font-name" ? cssKeyValueArray[1] : cssKeyValueArray[1].ToUpper(); //字体不应变大写
- var kv = new KeyValuePair<string, string>(cssKey, cssValue);
- return kv;
- }
- #endregion 获取样式
- #region 转换Css的 Key
- /// <summary>
- /// 缩写Key 转换成标准Key
- /// </summary>
- /// <param name="csskey"></param>
- /// <returns></returns>
- private static string StandardCssKey(this string csskey)
- {
- if (CssKeyDic.ContainsKey(csskey))
- {
- var sKey = CssKeyDic[csskey];
- return sKey;
- }
- return csskey;
- }
- /// <summary>
- /// key 转换字典
- /// </summary>
- private static Dictionary<string, string> CssKeyDic => new Dictionary<string, string>
- {
- {"color", "font-color"},
- {"fc", "font-color"},
- {"fw", "font-weight"},
- {"fn", "font-name"},
- {"fs", "font-size"},
- {"italic", "font-italic"},
- {"fi", "font-italic"},
- {"underline", "font-underline"},
- {"fu", "font-underline"},
- {"u", "font-underline"},
- {"deleteline", "font-strikeout"},
- {"d-line", "font-strikeout"},
- {"strikeout", "font-strikeout"},
- {"fst", "font-strikeout"},
- {"d", "font-strikeout"},
- {"font-offset", "font-superscript"},
- {"superscript", "font-superscript"},
- {"fss", "font-superscript"},
- {"ss", "font-superscript"},
- {"bg-color", "background-color"},
- {"bg-c", "background-color"},
- {"bgc", "background-color"},
- {"align", "text-align"},
- {"wrap", "WrapText"},
- {"inden", "Indention"},
- {"in", "Indention"},
- {"v-align", "vertical-align"},
- {"b-t", "border-type"},
- {"b", "border-type"},
- {"bt", "top-border-type"},
- {"br", "right-border-type"},
- {"bb", "bottom-border-type"},
- {"bl", "left-border-type"},
- {"b-c", "border-color"},
- {"bc", "border-color"},
- {"btc", "top-border-color"},
- {"brc", "right-border-color"},
- {"bbc", "bottom-border-color"},
- {"blc", "left-border-color"},
- {"format", "data-format"},
- {"df", "data-format"}
- };
- #endregion 转换Css的 Key
- #region 样式转换
- #region font-weight
- private static short FontWeight(this SortedDictionary<string, string> fontdic)
- {
- switch (fontdic["font-weight"])
- {
- case "NORMAL":
- return 400;
- case "BOLD":
- return 700;
- default:
- return 0;
- }
- }
- #endregion font-weight
- #region font-name
- private static string FontName(this SortedDictionary<string, string> fontdic)
- {
- return fontdic["font-name"];
- }
- #endregion font-name
- #region font-size
- private static short FontSize(this SortedDictionary<string, string> fontdic)
- {
- return short.TryParse(fontdic["font-size"], out var value) ? value : (short)10;
- }
- #endregion font-size
- #region font-color
- private static short FontColor(this SortedDictionary<string, string> fontdic)
- {
- var color = fontdic["font-color"].ConvertToColor();
- return color;
- }
- #endregion font-color
- #region font-italic
- private static bool FontItalic(this SortedDictionary<string, string> fontdic)
- {
- return fontdic["font-italic"] == "TRUE";
- }
- #endregion font-italic
- #region font-strikeout
- /// <summary>
- /// 删除线
- /// </summary>
- /// <param name="fontdic"></param>
- /// <returns></returns>
- private static bool FontStrikeout(this SortedDictionary<string, string> fontdic)
- {
- return fontdic["font-strikeout"] == "TRUE";
- }
- #endregion font-strikeout
- #region WrapText
- private static void TextWrap(this ICellStyle style, string v)
- {
- style.WrapText = v.ToUpper() == "TRUE";
- }
- #endregion WrapText
- #region TextIndention
- /// <summary>
- /// 缩进
- /// </summary>
- /// <param name="style"></param>
- /// <param name="v"></param>
- private static void TextIndention(this ICellStyle style, string v)
- {
- if (short.TryParse(v, out var value))
- {
- style.Indention = value;
- }
- }
- #endregion TextIndention
- #region text-align
- private static void TextAlign(this ICellStyle style, string v)
- {
- style.Alignment = v.ConvertToHorizontalAlignment();
- }
- #endregion text-align
- #region vertical-align
- private static void VerticalAlign(this ICellStyle style, string v)
- {
- style.VerticalAlignment = v.ConvertToVerticalAlignment();
- }
- #endregion vertical-align
- #region boder-type / boder-color
- internal static void BorderTypes(this ICellStyle style, string v)
- {
- if (string.IsNullOrEmpty(v))
- return;
- string[] borderTypeNames = { string.Empty, string.Empty, string.Empty, string.Empty };
- v = v.ToUpper();
- var vs = v.Split(' ');
- switch (vs.Length)
- {
- case 1:
- borderTypeNames[0] = borderTypeNames[1] = borderTypeNames[2] = borderTypeNames[3] = vs[0];
- break;
- case 2:
- borderTypeNames[0] = borderTypeNames[2] = vs[0];
- borderTypeNames[1] = borderTypeNames[3] = vs[1];
- break;
- case 3:
- borderTypeNames[0] = vs[0];
- borderTypeNames[1] = borderTypeNames[3] = vs[1];
- borderTypeNames[2] = vs[2];
- break;
- case 4:
- borderTypeNames[0] = vs[0];
- borderTypeNames[1] = vs[1];
- borderTypeNames[2] = vs[2];
- borderTypeNames[3] = vs[3];
- break;
- }
- var borderTopTypeName = borderTypeNames[0];
- var borderRightTypeName = borderTypeNames[1];
- var borderBottomTypeName = borderTypeNames[2];
- var borderLeftTypeName = borderTypeNames[3];
- if (!string.IsNullOrWhiteSpace(borderTopTypeName))
- style.BorderTop = borderTopTypeName.ConvertToBorderStyle();
- if (!string.IsNullOrWhiteSpace(borderRightTypeName))
- style.BorderRight = borderRightTypeName.ConvertToBorderStyle();
- if (!string.IsNullOrWhiteSpace(borderBottomTypeName))
- style.BorderBottom = borderBottomTypeName.ConvertToBorderStyle();
- if (!string.IsNullOrWhiteSpace(borderLeftTypeName))
- style.BorderLeft = borderLeftTypeName.ConvertToBorderStyle();
- }
- private static void BorderTopTypes(this ICellStyle style, string v)
- {
- if (!string.IsNullOrWhiteSpace(v))
- style.BorderTop = v.ConvertToBorderStyle();
- }
- private static void BorderBottomTypes(this ICellStyle style, string v)
- {
- if (!string.IsNullOrWhiteSpace(v))
- style.BorderBottom = v.ConvertToBorderStyle();
- }
- private static void BorderLeftTypes(this ICellStyle style, string v)
- {
- if (!string.IsNullOrWhiteSpace(v))
- style.BorderLeft = v.ConvertToBorderStyle();
- }
- private static void BorderRightTypes(this ICellStyle style, string v)
- {
- if (!string.IsNullOrWhiteSpace(v))
- style.BorderRight = v.ConvertToBorderStyle();
- }
- internal static void BorderColors(this ICellStyle style, string v)
- {
- if (string.IsNullOrEmpty(v))
- return;
- string[] borderColors = { string.Empty, string.Empty, string.Empty, string.Empty };
- v = v.ToUpper();
- var vs = v.Split(' ');
- switch (vs.Length)
- {
- case 1:
- borderColors[0] = borderColors[1] = borderColors[2] = borderColors[3] = vs[0];
- break;
- case 2:
- borderColors[0] = borderColors[2] = vs[0];
- borderColors[1] = borderColors[3] = vs[1];
- break;
- case 3:
- borderColors[0] = vs[0];
- borderColors[1] = borderColors[3] = vs[1];
- borderColors[2] = vs[2];
- break;
- case 4:
- borderColors[0] = vs[0];
- borderColors[1] = vs[1];
- borderColors[2] = vs[2];
- borderColors[3] = vs[3];
- break;
- }
- var borderTopColor = borderColors[0];
- var borderRightColor = borderColors[1];
- var borderBottomColor = borderColors[2];
- var borderLeftColor = borderColors[3];
- if (!string.IsNullOrWhiteSpace(borderTopColor))
- style.TopBorderColor = borderTopColor.ConvertToColor();
- if (!string.IsNullOrWhiteSpace(borderRightColor))
- style.RightBorderColor = borderRightColor.ConvertToColor();
- if (!string.IsNullOrWhiteSpace(borderBottomColor))
- style.BottomBorderColor = borderBottomColor.ConvertToColor();
- if (!string.IsNullOrWhiteSpace(borderLeftColor))
- style.LeftBorderColor = borderLeftColor.ConvertToColor();
- }
- private static void BorderTopColors(this ICellStyle style, string v)
- {
- if (!string.IsNullOrWhiteSpace(v))
- style.TopBorderColor = v.ConvertToColor();
- }
- private static void BorderBottomColors(this ICellStyle style, string v)
- {
- if (!string.IsNullOrWhiteSpace(v))
- style.BottomBorderColor = v.ConvertToColor();
- }
- private static void BorderLeftColors(this ICellStyle style, string v)
- {
- if (!string.IsNullOrWhiteSpace(v))
- style.LeftBorderColor = v.ConvertToColor();
- }
- private static void BorderRightColors(this ICellStyle style, string v)
- {
- if (!string.IsNullOrWhiteSpace(v))
- style.RightBorderColor = v.ConvertToColor();
- }
- #endregion boder-type / boder-color
- #region data-format
- private static void DataFormat(this ICellStyle style, IWorkbook workbook, string v)
- {
- if (string.IsNullOrEmpty(v))
- return;
- var df = workbook.CreateDataFormat();
- style.DataFormat = df.GetFormat(v);
- }
- #endregion data-format
- #region BackgroundColor
- private static void BackgroundColor(this ICellStyle style, string v)
- {
- if (string.IsNullOrEmpty(v))
- return;
- style.FillPattern = FillPattern.SolidForeground;
- style.FillForegroundColor = v.ConvertToColor();
- }
- #endregion BackgroundColor
- private static FontSuperScript ConvertToSuperScript(this SortedDictionary<string, string> fontdic)
- {
- var v = fontdic["font-superscript"];
- switch (v)
- {
- case "SUPER":
- return FontSuperScript.Super;
- case "SUB":
- return FontSuperScript.Sub;
- default:
- return FontSuperScript.None;
- }
- }
- private static FontUnderlineType FontUnderline(this SortedDictionary<string, string> fontdic)
- {
- var v = fontdic["font-underline"];
- switch (v)
- {
- case "SINGLE":
- return FontUnderlineType.Single;
- case "DOUBLE":
- return FontUnderlineType.Double;
- case "SINGLEACCOUNTING":
- case "SINGLE_ACCOUNTING":
- return FontUnderlineType.SingleAccounting;
- case "DOUBLEACCOUNTING":
- case "DOUBLE_ACCOUNTING":
- return FontUnderlineType.DoubleAccounting;
- default:
- return FontUnderlineType.None;
- }
- }
- public static short ConvertToColor(this string v)
- {
- if (string.IsNullOrEmpty(v))
- return 32767;
- switch (v.ToUpper())
- {
- case "AQUA":
- return (short)ColorType.Aqua;
- case "AUTOMATIC":
- return (short)ColorType.Automatic;
- case "BLACK":
- return (short)ColorType.Black;
- case "BLUE":
- return (short)ColorType.Blue;
- case "BLUE_GREY":
- case "BLUEGREY":
- return (short)ColorType.BlueGrey;
- case "BRIGHT_GREEN":
- case "BRIGHTGREEN":
- return (short)ColorType.BrightGreen;
- case "BROWN":
- return (short)ColorType.Brown;
- case "CORAL":
- return (short)ColorType.Coral;
- case "CORNFLOWER_BLUE":
- case "CORNFLOWERBLUE":
- return (short)ColorType.CornflowerBlue;
- case "DARK_BLUE":
- case "DARKBLUE":
- return (short)ColorType.DarkBlue;
- case "DARK_GREEN":
- case "DARKGREEN":
- return (short)ColorType.DarkGreen;
- case "DARK_RED":
- case "DARKRED":
- return (short)ColorType.DarkRed;
- case "DARK_TEAL":
- case "DARKTEAL":
- return (short)ColorType.DarkTeal;
- case "DARK_YELLOW":
- case "DARKYELLOW":
- return (short)ColorType.DarkYellow;
- case "GOLD":
- return (short)ColorType.Gold;
- case "GREEN":
- return (short)ColorType.Green;
- case "GREY_25_PERCENT":
- case "GREY25PERCENT":
- return (short)ColorType.Grey25Percent;
- case "GREY_40_PERCENT":
- case "GREY40PERCENT":
- return (short)ColorType.Grey40Percent;
- case "GREY_50_PERCENT":
- case "GREY50PERCENT":
- return (short)ColorType.Grey50Percent;
- case "GREY_80_PERCENT":
- case "GREY80PERCENT":
- return (short)ColorType.Grey80Percent;
- case "INDIGO":
- return (short)ColorType.Indigo;
- case "LAVENDER":
- return (short)ColorType.Lavender;
- case "LEMON_CHIFFON":
- case "LEMONCHIFFON":
- return (short)ColorType.LemonChiffon;
- case "LIGHT_BLUE":
- case "LIGHTBLUE":
- return (short)ColorType.LightBlue;
- case "LIGHT_CORNFLOWERBLUE":
- case "LIGHTCORNFLOWERBLUE":
- return (short)ColorType.LightCornflowerBlue;
- case "LIGHT_GREEN":
- case "LIGHTGREEN":
- return (short)ColorType.LightGreen;
- case "LIGHT_ORANGE":
- case "LIGHTORANGE":
- return (short)ColorType.LightOrange;
- case "LIGHT_TURQUOISE":
- case "LIGHTTURQUOISE":
- return (short)ColorType.LightTurquoise;
- case "LIGHT_YELLOW":
- case "LIGHTYELLOW":
- return (short)ColorType.LightYellow;
- case "LIME":
- return (short)ColorType.Lime;
- case "MAROON":
- return (short)ColorType.Maroon;
- case "OLIVE_GREEN":
- case "OLIVEGREEN":
- return (short)ColorType.OliveGreen;
- case "ORANGE":
- return (short)ColorType.Orange;
- case "ORCHID":
- return (short)ColorType.Orchid;
- case "PALE_BLUE":
- case "PALEBLUE":
- return (short)ColorType.PaleBlue;
- case "PINK":
- return (short)ColorType.Pink;
- case "PLUM":
- return (short)ColorType.Plum;
- case "RED":
- return (short)ColorType.Red;
- case "ROSE":
- return (short)ColorType.Rose;
- case "ROYAL_BLUE":
- case "ROYALBLUE":
- return (short)ColorType.RoyalBlue;
- case "SEA_GREEN":
- case "SEAGREEN":
- return (short)ColorType.SeaGreen;
- case "SKY_BLUE":
- case "SKYBLUE":
- return (short)ColorType.SkyBlue;
- case "TAN":
- return (short)ColorType.Tan;
- case "TEAL":
- return (short)ColorType.Teal;
- case "TURQUOISE":
- return (short)ColorType.Turquoise;
- case "VIOLET":
- return (short)ColorType.Violet;
- case "WHITE":
- return (short)ColorType.White;
- case "YELLOW":
- return (short)ColorType.Yellow;
- default:
- return 32767;
- }
- }
- private static HorizontalAlignment ConvertToHorizontalAlignment(this string v)
- {
- if (string.IsNullOrEmpty(v))
- return HorizontalAlignment.General;
- switch (v.ToUpper())
- {
- case "LEFT":
- return HorizontalAlignment.Left;
- case "CENTER":
- return HorizontalAlignment.Center;
- case "CENTERSELECTION":
- case "CENTER_SELECTION":
- return HorizontalAlignment.CenterSelection;
- case "RIGHT":
- return HorizontalAlignment.Right;
- case "DISTRIBUTED":
- return HorizontalAlignment.Distributed;
- case "FILL":
- return HorizontalAlignment.Fill;
- case "JUSTIFY":
- return HorizontalAlignment.Justify;
- default:
- return HorizontalAlignment.General;
- }
- }
- private static VerticalAlignment ConvertToVerticalAlignment(this string v)
- {
- if (string.IsNullOrEmpty(v))
- return VerticalAlignment.Justify;
- switch (v.ToUpper())
- {
- case "TOP":
- return VerticalAlignment.Top;
- case "CENTER":
- return VerticalAlignment.Center;
- case "BOTTOM":
- return VerticalAlignment.Bottom;
- case "DISTRIBUTED":
- return VerticalAlignment.Distributed;
- default:
- return VerticalAlignment.Justify;
- }
- }
- public static BorderStyle ConvertToBorderStyle(this string v)
- {
- if (string.IsNullOrEmpty(v))
- return BorderStyle.None;
- switch (v.ToUpper())
- {
- case "THIN":
- return BorderStyle.Thin;
- case "MEDIUM":
- return BorderStyle.Medium;
- case "DASHED":
- return BorderStyle.Dashed;
- case "HAIR":
- return BorderStyle.Hair;
- case "THICK":
- return BorderStyle.Thick;
- case "DOUBLE":
- return BorderStyle.Double;
- case "DOTTED":
- return BorderStyle.Dotted;
- case "MEDIUMDASHED":
- case "MEDIUM_DASHED":
- return BorderStyle.MediumDashed;
- case "DASHDOT":
- case "DASH_DOT":
- return BorderStyle.DashDot;
- case "MEDIUMDASHDOT":
- case "MEDIUM_DASH_DOT":
- return BorderStyle.MediumDashDot;
- case "DASHDOTDOT":
- case "DASH_DOT_DOT":
- return BorderStyle.DashDotDot;
- case "MEDIUMDASHDOTDOT":
- case "MEDIUM_DASH_DOT_DOT":
- return BorderStyle.MediumDashDotDot;
- case "SLANTEDDASHDOT":
- case "SLANTED_DASH_DOT":
- return BorderStyle.SlantedDashDot;
- default:
- return BorderStyle.None;
- }
- }
- #endregion 样式转换
- #endregion 解析css样式
- }
- public enum ColorType
- {
- Black = HSSFColor.Black.Index,
- Brown = HSSFColor.Brown.Index,
- OliveGreen = HSSFColor.OliveGreen.Index,
- DarkGreen = HSSFColor.DarkGreen.Index,
- DarkTeal = HSSFColor.DarkTeal.Index,
- DarkBlue = HSSFColor.DarkBlue.Index,
- Indigo = HSSFColor.Indigo.Index,
- Grey80Percent = HSSFColor.Grey80Percent.Index,
- Orange = HSSFColor.Orange.Index,
- DarkYellow = HSSFColor.DarkYellow.Index,
- Green = HSSFColor.Green.Index,
- Teal = HSSFColor.Teal.Index,
- Blue = HSSFColor.Blue.Index,
- BlueGrey = HSSFColor.BlueGrey.Index,
- Grey50Percent = HSSFColor.Grey50Percent.Index,
- Red = HSSFColor.Red.Index,
- LightOrange = HSSFColor.LightOrange.Index,
- Lime = HSSFColor.Lime.Index,
- SeaGreen = HSSFColor.SeaGreen.Index,
- Aqua = HSSFColor.Aqua.Index,
- LightBlue = HSSFColor.LightBlue.Index,
- Violet = HSSFColor.Violet.Index,
- Grey40Percent = HSSFColor.Grey40Percent.Index,
- Pink = HSSFColor.Pink.Index,
- Gold = HSSFColor.Gold.Index,
- Yellow = HSSFColor.Yellow.Index,
- BrightGreen = HSSFColor.BrightGreen.Index,
- Turquoise = HSSFColor.Turquoise.Index,
- DarkRed = HSSFColor.DarkRed.Index,
- SkyBlue = HSSFColor.SkyBlue.Index,
- Plum = HSSFColor.Plum.Index,
- Grey25Percent = HSSFColor.Grey25Percent.Index,
- Rose = HSSFColor.Rose.Index,
- LightYellow = HSSFColor.LightYellow.Index,
- LightGreen = HSSFColor.LightGreen.Index,
- LightTurquoise = HSSFColor.LightTurquoise.Index,
- PaleBlue = HSSFColor.PaleBlue.Index,
- Lavender = HSSFColor.Lavender.Index,
- White = HSSFColor.White.Index,
- CornflowerBlue = HSSFColor.CornflowerBlue.Index,
- LemonChiffon = HSSFColor.LemonChiffon.Index,
- Maroon = HSSFColor.Maroon.Index,
- Orchid = HSSFColor.Orchid.Index,
- Coral = HSSFColor.Coral.Index,
- RoyalBlue = HSSFColor.RoyalBlue.Index,
- LightCornflowerBlue = HSSFColor.LightCornflowerBlue.Index,
- Tan = HSSFColor.Tan.Index,
- Automatic = HSSFColor.Automatic.Index
- }
- public static class TransExp<TIn, TOut>
- {
- private static readonly Func<TIn, TOut> Cache = GetFunc();
- private static Func<TIn, TOut> GetFunc()
- {
- ParameterExpression parameterExpression = Expression.Parameter(typeof(TIn), "p");
- List<MemberBinding> memberBindingList = new List<MemberBinding>();
- foreach (var item in typeof(TOut).GetProperties())
- {
- if (!item.CanWrite)
- continue;
- MemberExpression property = Expression.Property(parameterExpression, typeof(TIn).GetProperty(item.Name) ?? throw new InvalidOperationException());
- MemberBinding memberBinding = Expression.Bind(item, property);
- memberBindingList.Add(memberBinding);
- }
- MemberInitExpression memberInitExpression = Expression.MemberInit(Expression.New(typeof(TOut)), memberBindingList.ToArray());
- Expression<Func<TIn, TOut>> lambda = Expression.Lambda<Func<TIn, TOut>>(memberInitExpression, new ParameterExpression[] { parameterExpression });
- return lambda.Compile();
- }
- public static TOut Trans(TIn tIn)
- {
- return Cache(tIn);
- }
- }
|