ExcelHelper.cs 81 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295
  1. using Abp.Logging;
  2. using NPOI.HSSF.UserModel;
  3. using NPOI.HSSF.Util;
  4. using NPOI.SS.UserModel;
  5. using NPOI.SS.Util;
  6. using NPOI.XSSF.UserModel;
  7. using System.Collections;
  8. using System.Linq.Expressions;
  9. using System.Reflection;
  10. using System.Runtime.CompilerServices;
  11. using System.Security.Cryptography;
  12. using System.Text;
  13. using System.Text.RegularExpressions;
  14. namespace VberZero.Tools.FileHelpers;
  15. public static class ExcelHelper
  16. {
  17. #region Excel导入
  18. /// <summary>
  19. /// 从Excel取数据并记录到List集合里
  20. /// </summary>
  21. /// <param name="cellHeader">单元头的值和名称:{ { "UserName", "姓名" }, { "Age", "年龄" } };</param>
  22. /// <param name="filePath">保存文件绝对路径</param>
  23. /// <param name="errMsg">错误信息</param>
  24. /// <param name="startIndex">数据行开始序列,默认为1(即第二列,从0开始)</param>
  25. /// <returns>转换后的List对象集合</returns>
  26. public static List<T> ExcelToEntityList<T>(this Dictionary<string, string> cellHeader, string filePath, out string errMsg, int startIndex = 1) where T : new()
  27. {
  28. List<T> enlist = new List<T>();
  29. var errorMsg = new StringBuilder();
  30. try
  31. {
  32. if (Regex.IsMatch(filePath, ".xls$")) // 2003
  33. {
  34. enlist = Excel2003ToEntityList<T>(cellHeader, filePath, out errorMsg, startIndex);
  35. }
  36. else if (Regex.IsMatch(filePath, ".xlsx$")) // 2007
  37. {
  38. enlist = Excel2007ToEntityList<T>(cellHeader, filePath, out errorMsg, startIndex);
  39. }
  40. }
  41. catch (Exception ex)
  42. {
  43. errMsg = ex.Message;
  44. LogHelper.LogException(ex);
  45. return default;
  46. }
  47. errMsg = errorMsg.ToString();
  48. return enlist;
  49. }
  50. /// <summary>
  51. /// 从Excel2003取数据并记录到List集合里
  52. /// </summary>
  53. /// <param name="cellHeader">单元头的Key和Value:{ { "UserName", "姓名" }, { "Age", "年龄" } };</param>
  54. /// <param name="filePath">保存文件绝对路径</param>
  55. /// <param name="errorMsg">错误信息</param>
  56. /// <param name="startIndex"></param>
  57. /// <returns>转换好的List对象集合</returns>
  58. private static List<T> Excel2003ToEntityList<T>(this Dictionary<string, string> cellHeader, string filePath, out StringBuilder errorMsg, int startIndex = 1) where T : new()
  59. {
  60. errorMsg = new StringBuilder(); // 错误信息,Excel转换到实体对象时,会有格式的错误信息
  61. List<T> enlist = new List<T>(); // 转换后的集合
  62. try
  63. {
  64. using (FileStream fs = File.OpenRead(filePath))
  65. {
  66. HSSFWorkbook workbook = new HSSFWorkbook(fs);
  67. HSSFSheet sheet = (HSSFSheet)workbook.GetSheetAt(0); // 获取此文件第一个Sheet页
  68. for (int rowIndex = startIndex; rowIndex <= sheet.LastRowNum; rowIndex++)
  69. {
  70. // 1.判断当前行是否空行,若空行就不在进行读取下一行操作,结束Excel读取操作
  71. IRow row = sheet.GetRow(rowIndex);
  72. if (row == null)
  73. {
  74. break;
  75. }
  76. // 2.每一个Excel row转换为一个实体对象
  77. T en = new T();
  78. ExcelRowToEntity(cellHeader, row, rowIndex, en, ref errorMsg);
  79. enlist.Add(en);
  80. }
  81. }
  82. return enlist;
  83. }
  84. catch (Exception ex)
  85. {
  86. LogHelper.LogException(ex);
  87. return default;
  88. }
  89. }
  90. /// <summary>
  91. /// 从Excel2007取数据并记录到List集合里
  92. /// </summary>
  93. /// <param name="cellHeader">单元头的Key和Value:{ { "UserName", "姓名" }, { "Age", "年龄" } };</param>
  94. /// <param name="filePath">保存文件绝对路径</param>
  95. /// <param name="errorMsg">错误信息</param>
  96. /// <param name="startIndex">数据行开始序列,默认为1(即第二列,从0开始)</param>
  97. /// <returns>转换好的List对象集合</returns>
  98. private static List<T> Excel2007ToEntityList<T>(this Dictionary<string, string> cellHeader, string filePath, out StringBuilder errorMsg, int startIndex = 1)
  99. where T : new()
  100. {
  101. errorMsg = new StringBuilder(); // 错误信息,Excel转换到实体对象时,会有格式的错误信息
  102. List<T> enlist = new List<T>(); // 转换后的集合
  103. try
  104. {
  105. using (FileStream fs = File.OpenRead(filePath))
  106. {
  107. XSSFWorkbook workbook = new XSSFWorkbook(fs);
  108. XSSFSheet sheet = (XSSFSheet)workbook.GetSheetAt(0); // 获取此文件第一个Sheet页
  109. for (int rowIndex = startIndex; rowIndex <= sheet.LastRowNum; rowIndex++)
  110. {
  111. // 1.判断当前行是否空行,若空行就不在进行读取下一行操作,结束Excel读取操作
  112. IRow row = sheet.GetRow(rowIndex);
  113. if (row == null)
  114. {
  115. break;
  116. }
  117. // 2.每一个Excel row转换为一个实体对象
  118. T en = new T();
  119. ExcelRowToEntity(cellHeader, row, rowIndex, en, ref errorMsg);
  120. enlist.Add(en);
  121. }
  122. }
  123. return enlist;
  124. }
  125. catch (Exception ex)
  126. {
  127. LogHelper.LogException(ex);
  128. return default(List<T>);
  129. }
  130. }
  131. #endregion Excel导入
  132. #region Excel导出
  133. /// <summary>
  134. /// 实体类集合导出到EXCEL2003
  135. /// </summary>
  136. /// <param name="cellHeader">单元头的Key和Value:{ { "UserName", "姓名" }, { "Age", "年龄" } };</param>
  137. /// <param name="enList">数据源</param>
  138. /// <param name="sheetName">工作表名称</param>
  139. /// <param name="filePath">文件的下载地址</param>
  140. /// <returns></returns>
  141. public static string EntityListToExcel2003(this Dictionary<string, string> cellHeader, IList enList, string sheetName, string filePath)
  142. {
  143. var lcRetVal = "error@";
  144. try
  145. {
  146. string fileName = $"D-{sheetName}-{DateTime.Now:yyyyMMddHHmmssfff}.xls"; // 文件名称
  147. filePath = filePath.StartsWith("/") ? filePath : ("/" + filePath);
  148. filePath = filePath.EndsWith("/") ? filePath : (filePath + "/");
  149. string path = $"{AppDomain.CurrentDomain.BaseDirectory}{filePath}";
  150. if (!Directory.Exists(path))
  151. Directory.CreateDirectory(path);
  152. // 2.解析单元格头部,设置单元头的中文名称
  153. HSSFWorkbook workbook = new HSSFWorkbook(); // 工作簿
  154. ISheet sheet = workbook.CreateSheet(sheetName); // 工作表
  155. IRow row = sheet.CreateRow(0);
  156. List<string> keys = cellHeader.Keys.ToList();
  157. for (int i = 0; i < keys.Count; i++)
  158. {
  159. row.CreateCell(i).SetCellValue(cellHeader[keys[i]]); // 列名为Key的值
  160. }
  161. // 3.List对象的值赋值到Excel的单元格里
  162. int rowIndex = 1; // 从第二行开始赋值(第一行已设置为单元头)
  163. object propertyValue = null;
  164. foreach (var en in enList)
  165. {
  166. IRow rowTmp = sheet.CreateRow(rowIndex);
  167. for (int i = 0; i < keys.Count; i++) // 根据指定的属性名称,获取对象指定属性的值
  168. {
  169. string cellValue = "";
  170. PropertyInfo propertyInfo = null; // 属性的信息
  171. // 3.1 若属性头的名称包含'.',就表示是子类里的属性,那么就要遍历子类,eg:UserEn.UserName
  172. string[] propertyArray = keys[i].Split(new[] { "." }, StringSplitOptions.RemoveEmptyEntries);
  173. if (keys[i].IndexOf(".", StringComparison.Ordinal) >= 0)
  174. {
  175. // 3.1.1 解析子类属性(这里只解析1层子类,多层子类未处理)
  176. string subClassName = propertyArray[0]; // '.'前面的为子类的名称
  177. string subClassPropertyName = propertyArray[1]; // '.'后面的为子类的属性名称
  178. PropertyInfo subClassInfo = en.GetType().GetProperty(subClassName); // 获取子类的类型
  179. if (subClassInfo != null)
  180. {
  181. // 3.1.2 获取子类的实例
  182. var subClassEn = en.GetType().GetProperty(subClassName)?.GetValue(en, null);
  183. // 3.1.3 根据属性名称获取子类里的属性类型
  184. propertyInfo = subClassInfo.PropertyType.GetProperty(subClassPropertyName);
  185. if (propertyInfo != null)
  186. {
  187. propertyValue = propertyInfo.GetValue(subClassEn, null); // 获取子类属性的值
  188. }
  189. }
  190. }
  191. else
  192. {
  193. // 3.2 若不是子类的属性,直接根据属性名称获取对象对应的属性
  194. propertyInfo = en.GetType().GetProperty(keys[i]);
  195. if (propertyInfo != null)
  196. {
  197. propertyValue = propertyInfo.GetValue(en, null);
  198. }
  199. }
  200. // 3.3 属性值经过转换赋值给单元格值
  201. if (propertyValue != null)
  202. {
  203. if (propertyInfo?.PropertyType.Name == "int" || propertyInfo?.PropertyType.Name == "float" || propertyInfo?.PropertyType.Name == "double" || propertyInfo?.PropertyType.Name == "decimal")
  204. {
  205. var value = Convert.ToDouble(propertyValue);
  206. rowTmp.CreateCell(i).SetCellValue(value);
  207. }
  208. else if (propertyInfo?.PropertyType.Name == "bool")
  209. {
  210. var value = Convert.ToBoolean(propertyValue);
  211. rowTmp.CreateCell(i).SetCellValue(value);
  212. }
  213. else
  214. {
  215. cellValue = propertyValue.ToString();
  216. // 3.3.1 对时间初始值赋值为空
  217. if (cellValue.Trim() == "0001/1/1 0:00:00" || cellValue.Trim() == "0001/1/1 23:59:59")
  218. {
  219. cellValue = "";
  220. }
  221. // 3.4 填充到Excel的单元格里
  222. rowTmp.CreateCell(i).SetCellValue(cellValue);
  223. }
  224. }
  225. else
  226. {
  227. // 3.4 填充到Excel的单元格里
  228. rowTmp.CreateCell(i).SetCellValue(cellValue);
  229. }
  230. }
  231. rowIndex++;
  232. }
  233. // 4.生成文件
  234. FileStream file = new FileStream($"{path}{fileName}", FileMode.Create);
  235. workbook.Write(file);
  236. file.Close();
  237. lcRetVal = $"{filePath}{fileName}";
  238. }
  239. catch (Exception ex)
  240. {
  241. lcRetVal += ex.Message;
  242. LogHelper.LogException(ex);
  243. }
  244. return lcRetVal;
  245. }
  246. public static string EntityListToExcel2003(this HSSFWorkbook workbook,
  247. string sheetName, string filePath)
  248. {
  249. var lcRetVal = "error@";
  250. try
  251. {
  252. string fileName = $"D-{sheetName}-{DateTime.Now:yyyyMMddHHmmssfff}.xls"; // 文件名称
  253. filePath = filePath.StartsWith("/") ? filePath : ("/" + filePath);
  254. filePath = filePath.EndsWith("/") ? filePath : (filePath + "/");
  255. string path = $"{AppDomain.CurrentDomain.BaseDirectory}{filePath}";
  256. if (!Directory.Exists(path))
  257. Directory.CreateDirectory(path);
  258. FileStream file = new FileStream($"{path}{fileName}", FileMode.Create);
  259. workbook.Write(file);
  260. file.Close();
  261. lcRetVal = $"{filePath}{fileName}";
  262. }
  263. catch (Exception ex)
  264. {
  265. lcRetVal += ex.Message;
  266. LogHelper.LogException(ex);
  267. }
  268. return lcRetVal;
  269. }
  270. public static HSSFWorkbook EntityListToExcel2003Book(this Dictionary<string, string> cellHeader, IList enList,
  271. string sheetName)
  272. {
  273. HSSFWorkbook workbook = new HSSFWorkbook(); // 工作簿
  274. ISheet sheet = workbook.CreateSheet(sheetName); // 工作表
  275. IRow row = sheet.CreateRow(0);
  276. List<string> keys = cellHeader.Keys.ToList();
  277. for (int i = 0; i < keys.Count; i++)
  278. {
  279. row.CreateCell(i).SetCellValue(cellHeader[keys[i]]); // 列名为Key的值
  280. }
  281. // 3.List对象的值赋值到Excel的单元格里
  282. int rowIndex = 1; // 从第二行开始赋值(第一行已设置为单元头)
  283. foreach (var en in enList)
  284. {
  285. IRow rowTmp = sheet.CreateRow(rowIndex);
  286. for (int i = 0; i < keys.Count; i++) // 根据指定的属性名称,获取对象指定属性的值
  287. {
  288. string cellValue = ""; // 单元格的值
  289. object propertyValue = null; // 属性的值
  290. PropertyInfo propertyInfo; // 属性的信息
  291. // 3.1 若属性头的名称包含'.',就表示是子类里的属性,那么就要遍历子类,eg:UserEn.UserName
  292. if (keys[i].IndexOf(".", StringComparison.Ordinal) >= 0)
  293. {
  294. // 3.1.1 解析子类属性(这里只解析1层子类,多层子类未处理)
  295. string[] propertyArray = keys[i].Split(new[] { "." }, StringSplitOptions.RemoveEmptyEntries);
  296. string subClassName = propertyArray[0]; // '.'前面的为子类的名称
  297. string subClassPropertyName = propertyArray[1]; // '.'后面的为子类的属性名称
  298. PropertyInfo subClassInfo = en.GetType().GetProperty(subClassName); // 获取子类的类型
  299. if (subClassInfo != null)
  300. {
  301. // 3.1.2 获取子类的实例
  302. var subClassEn = en.GetType().GetProperty(subClassName)?.GetValue(en, null);
  303. // 3.1.3 根据属性名称获取子类里的属性类型
  304. propertyInfo = subClassInfo.PropertyType.GetProperty(subClassPropertyName);
  305. if (propertyInfo != null)
  306. {
  307. propertyValue = propertyInfo.GetValue(subClassEn, null); // 获取子类属性的值
  308. }
  309. }
  310. }
  311. else
  312. {
  313. // 3.2 若不是子类的属性,直接根据属性名称获取对象对应的属性
  314. propertyInfo = en.GetType().GetProperty(keys[i]);
  315. if (propertyInfo != null)
  316. {
  317. propertyValue = propertyInfo.GetValue(en, null);
  318. }
  319. }
  320. // 3.3 属性值经过转换赋值给单元格值
  321. if (propertyValue != null)
  322. {
  323. cellValue = propertyValue.ToString();
  324. // 3.3.1 对时间初始值赋值为空
  325. if (cellValue.Trim() == "0001/1/1 0:00:00" || cellValue.Trim() == "0001/1/1 23:59:59")
  326. {
  327. cellValue = "";
  328. }
  329. }
  330. // 3.4 填充到Excel的单元格里
  331. rowTmp.CreateCell(i).SetCellValue(cellValue);
  332. }
  333. rowIndex++;
  334. }
  335. return workbook;
  336. }
  337. /// <summary>
  338. /// 实体类集合导出到EXCEL2007
  339. /// </summary>
  340. /// <param name="cellHeader">单元头的Key和Value:{ { "UserName", "姓名" }, { "Age", "年龄" } };</param>
  341. /// <param name="enList">数据源</param>
  342. /// <param name="sheetName">工作表名称</param>
  343. /// <param name="filePath">文件的下载地址</param>
  344. /// <returns></returns>
  345. public static string EntityListToExcel2007(this Dictionary<string, string> cellHeader, IList enList, string sheetName, string filePath)
  346. {
  347. var lcRetVal = "error@";
  348. try
  349. {
  350. string fileName = $"D-{sheetName}-{DateTime.Now:yyyyMMddHHmmssfff}.xlsx"; // 文件名称
  351. filePath = filePath.StartsWith("/") ? filePath : ("/" + filePath);
  352. filePath = filePath.EndsWith("/") ? filePath : (filePath + "/");
  353. string path = $"{AppDomain.CurrentDomain.BaseDirectory}{filePath}";
  354. if (!Directory.Exists(path))
  355. Directory.CreateDirectory(path);
  356. // 2.解析单元格头部,设置单元头的中文名称
  357. XSSFWorkbook workbook = new XSSFWorkbook(); // 工作簿
  358. ISheet sheet = workbook.CreateSheet(sheetName); // 工作表
  359. IRow row = sheet.CreateRow(0);
  360. List<string> keys = cellHeader.Keys.ToList();
  361. for (int i = 0; i < keys.Count; i++)
  362. {
  363. row.CreateCell(i).SetCellValue(cellHeader[keys[i]]); // 列名为Key的值
  364. }
  365. // 3.List对象的值赋值到Excel的单元格里
  366. int rowIndex = 1; // 从第二行开始赋值(第一行已设置为单元头)
  367. foreach (var en in enList)
  368. {
  369. IRow rowTmp = sheet.CreateRow(rowIndex);
  370. for (int i = 0; i < keys.Count; i++) // 根据指定的属性名称,获取对象指定属性的值
  371. {
  372. string cellValue = ""; // 单元格的值
  373. object propertyValue = null; // 属性的值
  374. PropertyInfo propertyInfo = null; // 属性的信息
  375. // 3.1 若属性头的名称包含'.',就表示是子类里的属性,那么就要遍历子类,eg:UserEn.UserName
  376. if (keys[i].IndexOf(".", StringComparison.Ordinal) >= 0)
  377. {
  378. // 3.1.1 解析子类属性(这里只解析1层子类,多层子类未处理)
  379. string[] propertyArray = keys[i].Split(new[] { "." }, StringSplitOptions.RemoveEmptyEntries);
  380. string subClassName = propertyArray[0]; // '.'前面的为子类的名称
  381. var subClassPropertyName = propertyArray[1]; // '.'后面的为子类的属性名称
  382. PropertyInfo subClassInfo = en.GetType().GetProperty(subClassName); // 获取子类的类型
  383. if (subClassInfo != null)
  384. {
  385. // 3.1.2 获取子类的实例
  386. var subClassEn = en.GetType().GetProperty(subClassName)?.GetValue(en, null);
  387. // 3.1.3 根据属性名称获取子类里的属性类型
  388. propertyInfo = subClassInfo.PropertyType.GetProperty(subClassPropertyName);
  389. if (propertyInfo != null)
  390. {
  391. propertyValue = propertyInfo.GetValue(subClassEn, null); // 获取子类属性的值
  392. }
  393. }
  394. }
  395. else
  396. {
  397. // 3.2 若不是子类的属性,直接根据属性名称获取对象对应的属性
  398. propertyInfo = en.GetType().GetProperty(keys[i]);
  399. if (propertyInfo != null)
  400. {
  401. propertyValue = propertyInfo.GetValue(en, null);
  402. }
  403. }
  404. // 3.3 属性值经过转换赋值给单元格值
  405. if (propertyValue != null)
  406. {
  407. 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")
  408. {
  409. var value = Convert.ToDouble(propertyValue);
  410. rowTmp.CreateCell(i).SetCellValue(value);
  411. }
  412. else if (propertyInfo.PropertyType.Name.ToLower() == "bool")
  413. {
  414. var value = Convert.ToBoolean(propertyValue);
  415. rowTmp.CreateCell(i).SetCellValue(value);
  416. }
  417. else if (propertyInfo.PropertyType.Name.ToLower() == "nullable`1")
  418. {
  419. if (propertyInfo.PropertyType.FullName == null)
  420. {
  421. }
  422. else if (propertyInfo.PropertyType.FullName.ToLower().Contains("datetime"))
  423. {
  424. cellValue = propertyValue.ToString();
  425. // 3.3.1 对时间初始值赋值为空
  426. if (cellValue.Trim() == "0001/1/1 0:00:00" || cellValue.Trim() == "0001/1/1 23:59:59")
  427. {
  428. cellValue = "";
  429. }
  430. rowTmp.CreateCell(i).SetCellValue(cellValue);
  431. }
  432. else if (propertyInfo.PropertyType.FullName.ToLower().Contains("int"))
  433. {
  434. var value = (int)Convert.ChangeType(propertyValue, typeof(int));
  435. rowTmp.CreateCell(i).SetCellValue(value);
  436. }
  437. 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"))
  438. {
  439. var value = (float)Convert.ChangeType(propertyValue, typeof(float));
  440. rowTmp.CreateCell(i).SetCellValue(value);
  441. }
  442. else if (propertyInfo.PropertyType.FullName.ToLower().Contains("bool"))
  443. {
  444. var value = (bool)Convert.ChangeType(propertyValue, typeof(bool));
  445. rowTmp.CreateCell(i).SetCellValue(value);
  446. }
  447. }
  448. else
  449. {
  450. cellValue = propertyValue.ToString();
  451. // 3.3.1 对时间初始值赋值为空
  452. if (cellValue.Trim() == "0001/1/1 0:00:00" || cellValue.Trim() == "0001/1/1 23:59:59")
  453. {
  454. cellValue = "";
  455. }
  456. // 3.4 填充到Excel的单元格里
  457. rowTmp.CreateCell(i).SetCellValue(cellValue);
  458. }
  459. }
  460. else
  461. {
  462. // 3.4 填充到Excel的单元格里
  463. rowTmp.CreateCell(i).SetCellValue(cellValue);
  464. }
  465. }
  466. rowIndex++;
  467. }
  468. // 4.生成文件
  469. FileStream file = new FileStream($"{path}{fileName}", FileMode.Create);
  470. workbook.Write(file);
  471. file.Close();
  472. lcRetVal = $"{filePath}{fileName}";
  473. }
  474. catch (Exception ex)
  475. {
  476. lcRetVal += ex.Message;
  477. LogHelper.LogException(ex);
  478. }
  479. return lcRetVal;
  480. }
  481. #endregion Excel导出
  482. #region Common
  483. /// <summary>
  484. /// Excel row转换为实体对象
  485. /// </summary>
  486. /// <typeparam name="T"></typeparam>
  487. /// <param name="cellHeader">单元头的Key和Value:{ { "UserName", "姓名" }, { "Age", "年龄" } };</param>
  488. /// <param name="row">Excel row</param>
  489. /// <param name="rowIndex">row index</param>
  490. /// <param name="en">实体</param>
  491. /// <param name="errorMsg">错误信息</param>
  492. private static void ExcelRowToEntity<T>(Dictionary<string, string> cellHeader, IRow row, int rowIndex, T en, ref StringBuilder errorMsg)
  493. {
  494. List<string> keys = cellHeader.Keys.ToList(); // 要赋值的实体对象属性名称
  495. string errStr = ""; // 当前行转换时,是否有错误信息,格式为:第1行数据转换异常:XXX列;
  496. for (int i = 0; i < keys.Count; i++)
  497. {
  498. // 1.若属性头的名称包含'.',就表示是子类里的属性,那么就要遍历子类,eg:UserEn.TrueName
  499. if (keys[i].IndexOf(".", StringComparison.Ordinal) >= 0)
  500. {
  501. // 1)解析子类属性
  502. string[] propertyArray = keys[i].Split(new[] { "." }, StringSplitOptions.RemoveEmptyEntries);
  503. string subClassName = propertyArray[0]; // '.'前面的为子类的名称
  504. string classPropertyName = propertyArray[1]; // '.'后面的为子类的属性名称
  505. PropertyInfo subClassInfo = en.GetType().GetProperty(subClassName); // 获取子类的类型
  506. if (subClassInfo != null)
  507. {
  508. // 2)获取子类的实例
  509. var subClassEn = en.GetType().GetProperty(subClassName)?.GetValue(en, null);
  510. // 3)根据属性名称获取子类里的属性信息
  511. PropertyInfo propertyInfo = subClassInfo.PropertyType.GetProperty(classPropertyName);
  512. if (propertyInfo != null)
  513. {
  514. try
  515. {
  516. // Excel单元格的值转换为对象属性的值,若类型不对,记录出错信息
  517. propertyInfo.SetValue(subClassEn, GetExcelCellToProperty(propertyInfo.PropertyType, row.GetCell(i)), null);
  518. }
  519. catch (Exception ex)
  520. {
  521. LogHelper.LogException(ex);
  522. if (errStr.Length == 0)
  523. {
  524. errStr = "第" + rowIndex + "行数据转换异常:";
  525. }
  526. errStr += cellHeader[keys[i]] + "列;";
  527. }
  528. }
  529. }
  530. }
  531. else
  532. {
  533. // 2.给指定的属性赋值
  534. PropertyInfo propertyInfo = en.GetType().GetProperty(keys[i]);
  535. if (propertyInfo != null)
  536. {
  537. try
  538. {
  539. // Excel单元格的值转换为对象属性的值,若类型不对,记录出错信息
  540. propertyInfo.SetValue(en, GetExcelCellToProperty(propertyInfo.PropertyType, row.GetCell(i)), null);
  541. }
  542. catch (Exception ex)
  543. {
  544. LogHelper.LogException(ex);
  545. if (errStr.Length == 0)
  546. {
  547. errStr = "第" + rowIndex + "行数据转换异常:";
  548. }
  549. errStr += cellHeader[keys[i]] + "列;";
  550. }
  551. }
  552. }
  553. }
  554. // 若有错误信息,就添加到错误信息里
  555. if (errStr.Length > 0)
  556. {
  557. errorMsg.AppendLine(errStr);
  558. }
  559. }
  560. /// <summary>
  561. /// Excel Cell转换为实体的属性值
  562. /// </summary>
  563. /// <param name="distanceType">目标对象类型</param>
  564. /// <param name="sourceCell">对象属性的值</param>
  565. private static object GetExcelCellToProperty(Type distanceType, ICell sourceCell)
  566. {
  567. object rs = distanceType.IsValueType ? Activator.CreateInstance(distanceType) : null;
  568. // 1.判断传递的单元格是否为空
  569. if (sourceCell == null || string.IsNullOrEmpty(sourceCell.ToString()))
  570. {
  571. return rs;
  572. }
  573. // 2.Excel文本和数字单元格转换,在Excel里文本和数字是不能进行转换,所以这里预先存值
  574. object sourceValue = null;
  575. switch (sourceCell.CellType)
  576. {
  577. case CellType.Blank:
  578. break;
  579. case CellType.Boolean:
  580. sourceValue = sourceCell.BooleanCellValue;
  581. break;
  582. case CellType.Error:
  583. break;
  584. case CellType.Formula:
  585. break;
  586. case CellType.Numeric:
  587. sourceValue = sourceCell.NumericCellValue;
  588. break;
  589. case CellType.String:
  590. sourceValue = sourceCell.StringCellValue;
  591. break;
  592. case CellType.Unknown:
  593. break;
  594. default:
  595. sourceValue = sourceCell.StringCellValue;
  596. break;
  597. }
  598. string valueDataType = distanceType.Name;
  599. // 在这里进行特定类型的处理
  600. switch (valueDataType.ToLower()) // 以防出错,全部小写
  601. {
  602. case "string":
  603. rs = sourceValue?.ToString();
  604. break;
  605. case "int":
  606. case "int32":
  607. rs = (int)Convert.ChangeType(sourceValue?.ToString() ?? "", distanceType);
  608. break;
  609. case "int16":
  610. rs = (short)Convert.ChangeType(sourceValue?.ToString() ?? "", distanceType);
  611. break;
  612. case "int64":
  613. rs = (long)Convert.ChangeType(sourceValue?.ToString() ?? "", distanceType);
  614. break;
  615. case "float":
  616. case "single":
  617. rs = (float)Convert.ChangeType(sourceValue?.ToString() ?? "", distanceType);
  618. break;
  619. case "double":
  620. rs = (double)Convert.ChangeType(sourceValue?.ToString() ?? "", distanceType);
  621. break;
  622. case "decimal":
  623. rs = (decimal)Convert.ChangeType(sourceValue?.ToString() ?? "", distanceType);
  624. break;
  625. case "datetime":
  626. rs = (DateTime)Convert.ChangeType(sourceValue?.ToString() ?? "", distanceType);
  627. break;
  628. case "guid":
  629. rs = (Guid)Convert.ChangeType(sourceValue?.ToString() ?? "", distanceType);
  630. break;
  631. case "nullable`1":
  632. if (distanceType.FullName == null)
  633. {
  634. rs = "";
  635. }
  636. else if (distanceType.FullName.ToLower().Contains("datetime"))
  637. {
  638. rs = (DateTime)Convert.ChangeType(sourceValue?.ToString() ?? "", typeof(DateTime));
  639. }
  640. else if ((bool)distanceType.FullName?.ToLower().Contains("int"))
  641. {
  642. rs = (int)Convert.ChangeType(sourceValue?.ToString() ?? "", typeof(int));
  643. }
  644. 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"))
  645. {
  646. rs = (float)Convert.ChangeType(sourceValue?.ToString() ?? "", typeof(float));
  647. }
  648. else if ((bool)distanceType.FullName?.ToLower().Contains("bool"))
  649. {
  650. rs = (bool)Convert.ChangeType(sourceValue?.ToString() ?? "", typeof(bool));
  651. }
  652. break;
  653. }
  654. return rs;
  655. }
  656. #endregion Common
  657. public static IWorkbook CreateWorkBook(string filePath = "")
  658. {
  659. return filePath.ToLower().EndsWith(".xlsx") ? CreateWorkBook07(filePath) :
  660. filePath.ToLower().EndsWith(".xls") ? (IWorkbook)CreateWorkBook03(filePath) : new XSSFWorkbook();
  661. }
  662. public static HSSFWorkbook CreateWorkBook03(string filePath = null)
  663. {
  664. if (string.IsNullOrEmpty(filePath))
  665. {
  666. return new HSSFWorkbook();
  667. }
  668. FileStream file = new FileStream(filePath, FileMode.Open);
  669. return new HSSFWorkbook(file);
  670. }
  671. public static XSSFWorkbook CreateWorkBook07(string filePath = null)
  672. {
  673. if (string.IsNullOrEmpty(filePath))
  674. {
  675. return new XSSFWorkbook();
  676. }
  677. return new XSSFWorkbook(filePath);
  678. }
  679. /// <summary>
  680. /// 创建Sheet
  681. /// </summary>
  682. /// <param name="sheetName"></param>
  683. /// <param name="defaultWidth"></param>
  684. /// <param name="defaultHeight"></param>
  685. /// <param name="is07"></param>
  686. /// <param name="workbook"></param>
  687. /// <returns></returns>
  688. public static ISheet GenerateSheet(this string sheetName, int defaultWidth = 20, int defaultHeight = 20, bool is07 = true, IWorkbook workbook = null)
  689. {
  690. workbook = is07 ? workbook ?? CreateWorkBook07() : workbook ?? CreateWorkBook03();
  691. var sheet = workbook.CreateSheet(sheetName);
  692. sheet.DefaultColumnWidth = defaultWidth;
  693. sheet.DefaultRowHeight = (short)(defaultHeight * 20);
  694. return sheet;
  695. }
  696. /// <summary>
  697. /// 创建行
  698. /// </summary>
  699. /// <param name="sheet">表</param>
  700. /// <param name="rowIndex">第几行(从1开始计数)</param>
  701. /// <returns></returns>
  702. public static IRow GenerateRow(this ISheet sheet, int rowIndex)
  703. {
  704. rowIndex = rowIndex <= 0 ? 1 : rowIndex;
  705. var row = sheet.GetRow(rowIndex - 1) ?? sheet.CreateRow(rowIndex - 1);
  706. return row;
  707. }
  708. /// <summary>
  709. /// 插入行
  710. /// </summary>
  711. /// <param name="sheet">表</param>
  712. /// <param name="startRow">第几行后开始插入(源行)</param>
  713. /// <param name="count">插入的行数</param>
  714. /// <returns></returns>
  715. public static void InsertRows(this ISheet sheet, int startRow, int count)
  716. {
  717. var rowSource = sheet.GenerateRow(startRow);
  718. sheet.ShiftRows(startRow, sheet.LastRowNum, count, true, false);
  719. if (rowSource == null)
  720. return;
  721. var rowStyle = rowSource.RowStyle;
  722. for (int i = startRow + 1; i <= startRow + count; i++)
  723. {
  724. var rowInsert = sheet.GenerateRow(i);
  725. rowInsert.RowStyle = rowStyle;
  726. rowInsert.Height = rowSource.Height;
  727. for (int col = 1; col <= rowSource.LastCellNum; col++)
  728. {
  729. var cellSource = rowSource.GenerateCell(col);
  730. var cellInsert = rowInsert.GenerateCell(col);
  731. var cellStyle = cellSource.CellStyle;
  732. //设置单元格样式    
  733. if (cellStyle != null)
  734. cellInsert.CellStyle = cellSource.CellStyle;
  735. }
  736. }
  737. }
  738. /// <summary>
  739. /// 创建单元格
  740. /// </summary>
  741. /// <param name="sheet">表</param>
  742. /// <param name="rowIndex">第几行(从1开始计数)</param>
  743. /// <param name="columnIndex">第几列(从1开始计数)</param>
  744. /// <param name="val">值</param>
  745. /// <param name="cellType">单元格类型</param>
  746. /// <returns></returns>
  747. public static ICell GenerateCell(this ISheet sheet, int rowIndex, int columnIndex, string val = null, CellType cellType = CellType.String)
  748. {
  749. var row = sheet.GenerateRow(rowIndex);
  750. var cell = GenerateCell(row, columnIndex, val, cellType);
  751. return cell;
  752. }
  753. /// <summary>
  754. /// 创建单元格
  755. /// </summary>
  756. /// <param name="row">行</param>
  757. /// <param name="columnIndex">第几列(从1开始计数)</param>
  758. /// <param name="val">值</param>
  759. /// <param name="cellType">单元格类型</param>
  760. /// <returns></returns>
  761. public static ICell GenerateCell(this IRow row, int columnIndex, string val = null, CellType cellType = CellType.String)
  762. {
  763. columnIndex = columnIndex <= 0 ? 1 : columnIndex;
  764. var cell = row.GetCell(columnIndex - 1) ?? row.CreateCell(columnIndex - 1);
  765. if (!string.IsNullOrEmpty(val))
  766. {
  767. cell.SetCellValue(val);
  768. cell.SetCellType(cellType);
  769. }
  770. return cell;
  771. }
  772. /// <summary>
  773. /// 设置列宽度
  774. /// </summary>
  775. /// <param name="sheet">表</param>
  776. /// <param name="columnIndex">第几列(从1开始计数)</param>
  777. /// <param name="width">宽度</param>
  778. /// <returns></returns>
  779. public static ISheet SetCellWidth(this ISheet sheet, int columnIndex, int width)
  780. {
  781. sheet.SetColumnWidth(columnIndex - 1, width * 256);
  782. return sheet;
  783. }
  784. /// <summary>
  785. /// 设置列宽度
  786. /// </summary>
  787. /// <param name="row">行</param>
  788. /// <param name="columnIndex">第几列(从1开始计数)</param>
  789. /// <param name="width">宽度</param>
  790. /// <returns></returns>
  791. public static IRow SetCellWidth(this IRow row, int columnIndex, int width)
  792. {
  793. row.Sheet.SetColumnWidth(columnIndex - 1, width * 256);
  794. return row;
  795. }
  796. /// <summary>
  797. /// 设置列宽度
  798. /// </summary>
  799. /// <param name="cell">单元格</param>
  800. /// <param name="columnIndex">第几列(从1开始计数)</param>
  801. /// <param name="width">宽度</param>
  802. /// <returns></returns>
  803. public static ICell SetCellWidth(this ICell cell, int columnIndex, int width)
  804. {
  805. cell.Sheet.SetColumnWidth(columnIndex - 1, width * 256);
  806. return cell;
  807. }
  808. /// <summary>
  809. /// 设置行高度
  810. /// </summary>
  811. /// <param name="sheet">表</param>
  812. /// <param name="rowIndex">第几行(从1开始计数)</param>
  813. /// <param name="height">宽度</param>
  814. /// <returns></returns>
  815. public static ISheet SetRowHeight(this ISheet sheet, int rowIndex, int height)
  816. {
  817. sheet.GetRow(rowIndex - 1).SetRowHeight(height);
  818. return sheet;
  819. }
  820. /// <summary>
  821. /// 设置行高度
  822. /// </summary>
  823. /// <param name="row">行</param>
  824. /// <param name="height">宽度</param>
  825. /// <returns></returns>
  826. public static IRow SetRowHeight(this IRow row, int height)
  827. {
  828. row.Height = (short)(height * 20);
  829. return row;
  830. }
  831. /// <summary>
  832. /// 设置行高度
  833. /// </summary>
  834. /// <param name="cell">单元格</param>
  835. /// <param name="height">宽度</param>
  836. /// <returns></returns>
  837. public static ICell SetRowHeight(this ICell cell, int height)
  838. {
  839. cell.Row.SetRowHeight(height);
  840. return cell;
  841. }
  842. /// <summary>
  843. /// 设置单元格样式
  844. /// </summary>
  845. /// <param name="cell"></param>
  846. /// <param name="styleStr">样式字符串</param>
  847. /// <param name="instants">样式转换器</param>
  848. /// <returns></returns>
  849. public static ICell SetCellCss(this ICell cell, CellStyleCss instants = null, string styleStr = null)
  850. {
  851. instants = instants ?? CellStyleCss.Instants;
  852. return instants.Css(cell, styleStr);
  853. }
  854. /// <summary>
  855. /// 设置单元格的值
  856. /// </summary>
  857. /// <param name="cell"></param>
  858. /// <param name="obj"></param>
  859. /// <returns></returns>
  860. public static ICell SetValue(this ICell cell, string obj)
  861. {
  862. return SetValue<string>(cell, obj);
  863. }
  864. /// <summary>
  865. /// 设置单元格的值
  866. /// </summary>
  867. /// <typeparam name="T">值类型(sting,bool,double,DateTime)</typeparam>
  868. /// <param name="cell"></param>
  869. /// <param name="obj"></param>
  870. /// <returns></returns>
  871. public static ICell SetValue<T>(this ICell cell, object obj)
  872. {
  873. Type type = typeof(T);
  874. if (type == typeof(string))
  875. {
  876. cell.SetCellValue(obj.ToString());
  877. }
  878. else if (type == typeof(bool) && bool.TryParse(obj.ToString(), out var bValue))
  879. {
  880. cell.SetCellValue(bValue);
  881. }
  882. else if ((type == typeof(int) || type == typeof(double) || type == typeof(decimal)) && double.TryParse(obj.ToString(), out var iValue))
  883. {
  884. cell.SetCellValue(iValue);
  885. }
  886. else if (type == typeof(DateTime) && DateTime.TryParse(obj.ToString(), out var dValue))
  887. {
  888. cell.SetCellValue(dValue);
  889. }
  890. return cell;
  891. }
  892. #region DATA-FORMAT
  893. public static ICellStyle SetCellDateTime(this ICell cell, DateTime value, string formatStr = "yyyy-MM-dd HH:mm:ss")
  894. {
  895. cell.SetCellValue(value);
  896. cell.SetDataFormat(formatStr);
  897. return cell.CellStyle;
  898. }
  899. public static ICellStyle SetCellString(this ICell cell, double value, string formatStr = "@")
  900. {
  901. cell.SetCellValue(value);
  902. cell.SetDataFormat(formatStr);
  903. return cell.CellStyle;
  904. }
  905. public static ICellStyle SetCellDouble(this ICell cell, double value, string formatStr = "0.00")
  906. {
  907. cell.SetCellValue(value);
  908. cell.SetDataFormat(formatStr);
  909. return cell.CellStyle;
  910. }
  911. public static ICellStyle SetCellCurrency(this ICell cell, double value, string formatStr = "¥#,###.##")
  912. {
  913. cell.SetCellValue(value);
  914. cell.SetDataFormat(formatStr);
  915. return cell.CellStyle;
  916. }
  917. public static ICellStyle SetCellPercent(this ICell cell, double value, string formatStr = "0.00%")
  918. {
  919. cell.SetCellValue(value);
  920. cell.SetDataFormat(formatStr);
  921. return cell.CellStyle;
  922. }
  923. public static void SetDataFormat(this ICell cell, string formatStr = "¥#,##0.00")
  924. {
  925. var cellStyle = cell.CellStyle ?? GetCellStyle(cell);
  926. var format = cell.Sheet.Workbook.CreateDataFormat();
  927. cellStyle.DataFormat = format.GetFormat(formatStr);
  928. cell.SetCellStyle(cellStyle);
  929. }
  930. #endregion DATA-FORMAT
  931. #region 合并单元格
  932. /// <summary>
  933. /// 合并单元格
  934. /// </summary>
  935. /// <param name="sheet"></param>
  936. /// <param name="firstRow">开始行数(从1开始计数)</param>
  937. /// <param name="lastRow">结束行数(从1开始计数)</param>
  938. /// <param name="firstColumn">开始列数(从1开始计数)</param>
  939. /// <param name="lastColumn">结束列数(从1开始计数)</param>
  940. /// <returns></returns>
  941. public static ISheet MergedRegion(this ISheet sheet, int firstRow, int lastRow, int firstColumn, int lastColumn)
  942. {
  943. var region = GetCellRegion(firstRow, lastRow, firstColumn, lastColumn);
  944. sheet.MergedRegion(region);
  945. return sheet;
  946. }
  947. public static ISheet MergedRegion(this ISheet sheet, CellRangeAddress region)
  948. {
  949. sheet.AddMergedRegion(region);
  950. //SetRegionBorder(sheet, region, borderTypeStr, borderColorStr);
  951. return sheet;
  952. }
  953. public static CellRangeAddress GetCellRegion(this int firstRow, int lastRow, int firstColumn, int lastColumn)
  954. {
  955. firstRow = firstRow <= 0 ? 1 : firstRow;
  956. firstColumn = firstColumn <= 0 ? 1 : firstColumn;
  957. lastRow = lastRow <= 0 ? 1 : lastRow;
  958. lastColumn = lastColumn <= 0 ? 1 : lastColumn;
  959. return new CellRangeAddress(firstRow - 1, lastRow - 1, firstColumn - 1, lastColumn - 1);
  960. }
  961. public static ISheet SetRegionBorder(this CellRangeAddress region, ISheet sheet, string borderTypeStr, string borderColorStr)
  962. {
  963. ICellStyle style = sheet.Workbook.CreateCellStyle();
  964. borderTypeStr = borderTypeStr ?? "Thin";
  965. borderColorStr = borderColorStr ?? "Black";
  966. style.BorderColors(borderColorStr);
  967. style.BorderTypes(borderTypeStr);
  968. for (int i = region.FirstRow; i <= region.LastRow; i++)
  969. {
  970. IRow row = sheet.GenerateRow(i + 1);
  971. if (i == region.FirstRow)
  972. {
  973. }
  974. else if (i == region.LastRow)
  975. {
  976. }
  977. var leftCell = row.GenerateCell(region.FirstColumn + 1);
  978. leftCell.CellStyle.BorderLeft = style.BorderLeft;
  979. leftCell.CellStyle.LeftBorderColor = style.LeftBorderColor;
  980. var rightCell = row.GenerateCell(region.LastColumn + 1);
  981. rightCell.CellStyle.BorderRight = style.BorderRight;
  982. rightCell.CellStyle.RightBorderColor = style.RightBorderColor;
  983. //for (int j = region.FirstColumn; j <= region.LastColumn; j++)
  984. //{
  985. // ICell singleCell = row.GenerateCell((short)j);
  986. //}
  987. }
  988. return sheet;
  989. }
  990. #endregion 合并单元格
  991. public static ICellStyle GetCellStyle(this ICell cell)
  992. {
  993. return cell.CellStyle ?? cell.Sheet.Workbook.CreateCellStyle();
  994. }
  995. public static void SetCellStyle(this ICell cell, ICellStyle cellStyle)
  996. {
  997. cell.CellStyle = cellStyle;
  998. }
  999. /// <summary>
  1000. /// 保存工作簿
  1001. /// </summary>
  1002. /// <param name="sheet"></param>
  1003. /// <param name="filePath"></param>
  1004. /// <param name="fileName"></param>
  1005. /// <returns></returns>
  1006. public static string SaveWorkBook(this ISheet sheet, string filePath, string fileName)
  1007. {
  1008. try
  1009. {
  1010. if (!Directory.Exists(filePath))
  1011. {
  1012. Directory.CreateDirectory(filePath);
  1013. }
  1014. filePath = filePath.EndsWith("\\") ? filePath : filePath + "\\";
  1015. FileStream file = new FileStream(filePath + fileName, FileMode.Create);
  1016. sheet.Workbook.Write(file);
  1017. file.Close();
  1018. return "";
  1019. }
  1020. catch (Exception e)
  1021. {
  1022. return e.Message;
  1023. }
  1024. }
  1025. /// <summary>
  1026. /// 保存工作簿
  1027. /// </summary>
  1028. /// <param name="workbook"></param>
  1029. /// <param name="filePath"></param>
  1030. /// <param name="fileName"></param>
  1031. /// <returns></returns>
  1032. public static string SaveWorkBook(this IWorkbook workbook, string filePath, string fileName)
  1033. {
  1034. try
  1035. {
  1036. if (!Directory.Exists(filePath))
  1037. {
  1038. Directory.CreateDirectory(filePath);
  1039. }
  1040. filePath = filePath.EndsWith("\\") ? filePath : filePath + "\\";
  1041. FileStream file = new FileStream(filePath + fileName, FileMode.Create);
  1042. workbook.Write(file);
  1043. file.Close();
  1044. return "";
  1045. }
  1046. catch (Exception e)
  1047. {
  1048. return e.Message;
  1049. }
  1050. }
  1051. }
  1052. public class CellStyleCss
  1053. {
  1054. public static CellStyleCss Instants => new CellStyleCss()
  1055. {
  1056. //缩写
  1057. DefaultStyle = $"bgc:{ColorType.White.ToString()};" +
  1058. $"warp:{HorizontalAlignment.Left.ToString()};" +
  1059. $"align:{HorizontalAlignment.Left.ToString()};" +
  1060. $"v-align:{VerticalAlignment.Center.ToString()};" +
  1061. //$"b:{BorderStyle.None.ToString()};" +
  1062. //$"bc:{ColorType.Black.ToString()};" +
  1063. "inden:0;" +
  1064. "df:@;"
  1065. };
  1066. public void SetDefaultStyle(string styleStr)
  1067. {
  1068. var dic = new SortedDictionary<string, string>();
  1069. DefaultStyle = dic.GetCleanStyle("");
  1070. DefaultStyle = dic.GetCleanStyle(styleStr);
  1071. }
  1072. /// <summary>
  1073. /// 默认样式css
  1074. /// </summary>
  1075. private string DefaultStyle { get; set; }
  1076. //标准写法
  1077. //private static readonly string DefaultFontStyle = "font-color:black;" +
  1078. // "font-name:Arial;" +
  1079. // "font-size:10;" +
  1080. // "font-weight:normal;" +
  1081. // "font-underline:none;" +
  1082. // "font-italic:false;" +
  1083. // "font-strikeout:false;" +
  1084. // "font-superscript:none;"+
  1085. // "background-color:white;"+
  1086. // "text-align:none;"+
  1087. // "vertical-align:none;"+
  1088. // "data-format:none;border-type:Thin";
  1089. /// <summary>
  1090. /// 把css样式设置给单元格
  1091. /// </summary>
  1092. /// <param name="cell"></param>
  1093. /// <param name="styleStr"></param>
  1094. /// <returns></returns>
  1095. public ICell Css(ICell cell, string styleStr = null)
  1096. {
  1097. var dic = new SortedDictionary<string, string>();
  1098. var sortedCss = dic.GetCleanStyle(DefaultStyle);
  1099. if (!string.IsNullOrEmpty(styleStr))
  1100. sortedCss = dic.GetCleanStyle(styleStr);
  1101. var cssKey = $"CellStyle_{sortedCss.Md5()}";
  1102. var workbook = cell.Sheet.Workbook;
  1103. ICellStyle cellStyle = workbook.GetCellStyle(cssKey);
  1104. if (cellStyle == null)
  1105. {
  1106. cellStyle = workbook.GetCellStyle(dic, cell);
  1107. workbook.AttachedCellStyle(cssKey, cellStyle);
  1108. }
  1109. cell.CellStyle = cellStyle;
  1110. //cell.CellStyle = workbook.GetCellStyle(dic, cell);
  1111. return cell;//返回单元格方便流水式编程
  1112. }
  1113. public ICellStyle GetCssStyle(IWorkbook workbook, string styleStr)
  1114. {
  1115. var dic = new SortedDictionary<string, string>();
  1116. var sortedCss = dic.GetCleanStyle(DefaultStyle);
  1117. if (!string.IsNullOrEmpty(styleStr))
  1118. sortedCss = dic.GetCleanStyle(styleStr);
  1119. var cssKey = $"CellStyle_{sortedCss.Md5()}";
  1120. var cellStyle = workbook.GetCellStyle(cssKey);
  1121. if (cellStyle == null)
  1122. {
  1123. cellStyle = workbook.GetCellStyle(dic);
  1124. workbook.AttachedCellStyle(cssKey, cellStyle);
  1125. }
  1126. return cellStyle;
  1127. }
  1128. }
  1129. internal static class CellStyleRender
  1130. {
  1131. #region 解析css样式
  1132. /// <summary>
  1133. /// 默认字体样式
  1134. /// </summary>
  1135. private static string DefaultFontStyle { get; } = $"fc:{ColorType.Black.ToString()};" +//font-color
  1136. "fn:宋体;" +//font-name
  1137. "fs:12;" +//font-size
  1138. "fw:normal;" +//font-weight
  1139. "fu:none;" +//font-underline
  1140. "fi:false;" +//font-italic
  1141. "fst:false;" +//font-strikeout
  1142. "fss:none;";//font-superscript
  1143. #region 设置样式
  1144. /// <summary>
  1145. /// 缓存
  1146. /// </summary>
  1147. private static readonly ConditionalWeakTable<IWorkbook, Dictionary<string, ICellStyle>> Table =
  1148. new ConditionalWeakTable<IWorkbook, Dictionary<string, ICellStyle>>();
  1149. /// <summary>
  1150. /// 获取CellStyle
  1151. /// </summary>
  1152. /// <param name="workbook"></param>
  1153. /// <param name="dic"></param>
  1154. /// <param name="cell"></param>
  1155. /// <returns></returns>
  1156. public static ICellStyle GetCellStyle(this IWorkbook workbook, SortedDictionary<string, string> dic, ICell cell = null)
  1157. {
  1158. ICellStyle cellStyle = workbook.CreateCellStyle();
  1159. //if (cell != null)
  1160. //{
  1161. // cellStyle.CloneStyleFrom(cell.CellStyle);
  1162. //}
  1163. var fontStyles = dic.Where(w => w.Key.StartsWith("font-")).ToArray();
  1164. var fontDic = new SortedDictionary<string, string>();
  1165. foreach (var kv in fontStyles)
  1166. {
  1167. fontDic.Add(kv.Key, kv.Value);
  1168. }
  1169. var font = workbook.GetFont(fontDic);
  1170. cellStyle.SetFont(font);//TODO 在基于style.xls基础的样式上增加css时,会造成原字体设置的丢失
  1171. var xdic = dic.Where(w => !w.Key.StartsWith("font-")).ToArray();
  1172. foreach (var kvp in xdic)
  1173. {
  1174. FireCssAccess(cellStyle, workbook, kvp);
  1175. }
  1176. return cellStyle;
  1177. }
  1178. /// <summary>
  1179. /// 从缓存读取CellStyle
  1180. /// </summary>
  1181. /// <param name="workbook"></param>
  1182. /// <param name="propertyName"></param>
  1183. /// <returns></returns>
  1184. public static ICellStyle GetCellStyle(this IWorkbook workbook, string propertyName)
  1185. {
  1186. if (!Table.TryGetValue(workbook, out var values)) return null;
  1187. if (values.TryGetValue(propertyName, out var temp))
  1188. return temp;
  1189. return null;
  1190. }
  1191. /// <summary>
  1192. /// 缓存CellStyle
  1193. /// </summary>
  1194. /// <param name="workbook"></param>
  1195. /// <param name="propertyName"></param>
  1196. /// <param name="value"></param>
  1197. public static void AttachedCellStyle(this IWorkbook workbook, string propertyName, ICellStyle value)
  1198. {
  1199. if (!Table.TryGetValue(workbook, out var values))
  1200. {
  1201. values = new Dictionary<string, ICellStyle>();
  1202. Table.Add(workbook, values);
  1203. }
  1204. values[propertyName] = value;
  1205. }
  1206. /// <summary>
  1207. /// Md5 key
  1208. /// </summary>
  1209. /// <param name="input"></param>
  1210. /// <returns></returns>
  1211. public static string Md5(this string input)
  1212. {
  1213. if (input == null)
  1214. input = string.Empty;
  1215. byte[] data = Encoding.UTF8.GetBytes(input.Trim().ToLowerInvariant());
  1216. using (var md5 = new MD5CryptoServiceProvider())
  1217. {
  1218. data = md5.ComputeHash(data);
  1219. }
  1220. var ret = new StringBuilder();
  1221. foreach (byte b in data)
  1222. {
  1223. ret.Append(b.ToString("x2").ToLowerInvariant());
  1224. }
  1225. return ret.ToString();
  1226. }
  1227. /// <summary>
  1228. /// 设置不是字体的样式
  1229. /// </summary>
  1230. /// <param name="style"></param>
  1231. /// <param name="workbook"></param>
  1232. /// <param name="kvp"></param>
  1233. private static void FireCssAccess(ICellStyle style, IWorkbook workbook, KeyValuePair<string, string> kvp)
  1234. {
  1235. switch (kvp.Key)
  1236. {
  1237. case "WrapText":
  1238. style.TextWrap(kvp.Value);
  1239. break;
  1240. case "Indention":
  1241. style.TextIndention(kvp.Value);
  1242. break;
  1243. case "text-align":
  1244. style.TextAlign(kvp.Value);
  1245. break;
  1246. case "vertical-align":
  1247. style.VerticalAlign(kvp.Value);
  1248. break;
  1249. case "background-color":
  1250. style.BackgroundColor(kvp.Value);
  1251. break;
  1252. case "border-type":
  1253. style.BorderTypes(kvp.Value);
  1254. break;
  1255. case "top-border-type":
  1256. style.BorderTopTypes(kvp.Value);
  1257. break;
  1258. case "right-border-type":
  1259. style.BorderRightTypes(kvp.Value);
  1260. break;
  1261. case "bottom-border-type":
  1262. style.BorderBottomTypes(kvp.Value);
  1263. break;
  1264. case "left-border-type":
  1265. style.BorderLeftTypes(kvp.Value);
  1266. break;
  1267. case "border-color":
  1268. style.BorderColors(kvp.Value);
  1269. break;
  1270. case "top-border-color":
  1271. style.BorderTopColors(kvp.Value);
  1272. break;
  1273. case "right-border-color":
  1274. style.BorderRightColors(kvp.Value);
  1275. break;
  1276. case "bottom-border-color":
  1277. style.BorderBottomColors(kvp.Value);
  1278. break;
  1279. case "left-border-color":
  1280. style.BorderLeftColors(kvp.Value);
  1281. break;
  1282. case "data-format":
  1283. style.DataFormat(workbook, kvp.Value);
  1284. break;
  1285. }
  1286. }
  1287. /// <summary>
  1288. /// 获取字体样式
  1289. /// </summary>
  1290. /// <param name="workbook"></param>
  1291. /// <param name="fontdic"></param>
  1292. /// <returns></returns>
  1293. private static IFont GetFont(this IWorkbook workbook, SortedDictionary<string, string> fontdic)
  1294. {
  1295. var weight = fontdic.FontWeight();
  1296. var color = fontdic.FontColor();
  1297. var size = fontdic.FontSize();
  1298. var name = fontdic.FontName();
  1299. var underline = fontdic.FontUnderline();
  1300. var italic = fontdic.FontItalic();
  1301. var strikeout = fontdic.FontStrikeout();
  1302. var offset = fontdic.ConvertToSuperScript();
  1303. var findHeight = (short)(size * 20);
  1304. var font = workbook.FindFont(weight, color, findHeight, name, italic, strikeout, offset, underline);
  1305. if (font == null)
  1306. {
  1307. font = workbook.CreateFont();
  1308. font.Boldweight = weight;
  1309. font.Color = color;
  1310. font.FontHeightInPoints = size;
  1311. font.FontName = name;
  1312. font.Underline = underline;
  1313. font.IsItalic = italic;
  1314. font.IsStrikeout = strikeout;
  1315. font.TypeOffset = offset;
  1316. }
  1317. return font;
  1318. }
  1319. #endregion 设置样式
  1320. #region 获取样式
  1321. /// <summary>
  1322. /// 默认设置
  1323. /// </summary>
  1324. /// <param name="dic"></param>
  1325. public static void InitStyleDic(this SortedDictionary<string, string> dic)
  1326. {
  1327. var cssItems = GetCssItems(DefaultFontStyle);
  1328. foreach (var cssitem in cssItems)
  1329. {
  1330. var kvp = GetCssKeyValue(cssitem);
  1331. if (dic.ContainsKey(kvp.Key))
  1332. dic[kvp.Key] = kvp.Value; //覆盖相同key的值
  1333. else
  1334. dic.Add(kvp.Key, kvp.Value);
  1335. }
  1336. }
  1337. /// <summary>
  1338. /// 获取样式简洁字符串
  1339. /// </summary>
  1340. /// <param name="dic"></param>
  1341. /// <param name="style"></param>
  1342. /// <returns></returns>
  1343. public static string GetCleanStyle(this SortedDictionary<string, string> dic, string style)
  1344. {
  1345. style = Regex.Replace(style.Trim(), "\\s+", " ");
  1346. style = Regex.Replace(style, "\\s;\\s", ";");
  1347. style = Regex.Replace(style, "\\s:\\s", ":");
  1348. InitStyleDic(dic);
  1349. var cssItems = GetCssItems(style.TrimEnd(';'));
  1350. foreach (var cssitem in cssItems)
  1351. {
  1352. var kvp = GetCssKeyValue(cssitem);
  1353. if (dic.ContainsKey(kvp.Key))
  1354. dic[kvp.Key] = kvp.Value; //覆盖相同key的值
  1355. else
  1356. dic.Add(kvp.Key, kvp.Value);
  1357. }
  1358. var sortedCss = string.Join(";", dic.Select(s => $"{s.Key}:{s.Value}").ToArray());
  1359. return sortedCss;
  1360. }
  1361. /// <summary>
  1362. /// 获取样式数组
  1363. /// </summary>
  1364. /// <param name="style"></param>
  1365. /// <returns></returns>
  1366. private static string[] GetCssItems(string style)
  1367. {
  1368. var cssItems = Regex.Split(style, ";");
  1369. cssItems = cssItems.Where(w => !string.IsNullOrWhiteSpace(w)).ToArray();
  1370. return cssItems;
  1371. }
  1372. /// <summary>
  1373. /// 获取css样式
  1374. /// </summary>
  1375. /// <param name="css"></param>
  1376. /// <returns></returns>
  1377. private static KeyValuePair<string, string> GetCssKeyValue(string css)
  1378. {
  1379. var cssKeyValueArray = Regex.Split(css, ":").ToArray();
  1380. var cssKey = cssKeyValueArray[0].StandardCssKey();
  1381. var cssValue = cssKey == "font-name" ? cssKeyValueArray[1] : cssKeyValueArray[1].ToUpper(); //字体不应变大写
  1382. var kv = new KeyValuePair<string, string>(cssKey, cssValue);
  1383. return kv;
  1384. }
  1385. #endregion 获取样式
  1386. #region 转换Css的 Key
  1387. /// <summary>
  1388. /// 缩写Key 转换成标准Key
  1389. /// </summary>
  1390. /// <param name="csskey"></param>
  1391. /// <returns></returns>
  1392. private static string StandardCssKey(this string csskey)
  1393. {
  1394. if (CssKeyDic.ContainsKey(csskey))
  1395. {
  1396. var sKey = CssKeyDic[csskey];
  1397. return sKey;
  1398. }
  1399. return csskey;
  1400. }
  1401. /// <summary>
  1402. /// key 转换字典
  1403. /// </summary>
  1404. private static Dictionary<string, string> CssKeyDic => new Dictionary<string, string>
  1405. {
  1406. {"color", "font-color"},
  1407. {"fc", "font-color"},
  1408. {"fw", "font-weight"},
  1409. {"fn", "font-name"},
  1410. {"fs", "font-size"},
  1411. {"italic", "font-italic"},
  1412. {"fi", "font-italic"},
  1413. {"underline", "font-underline"},
  1414. {"fu", "font-underline"},
  1415. {"u", "font-underline"},
  1416. {"deleteline", "font-strikeout"},
  1417. {"d-line", "font-strikeout"},
  1418. {"strikeout", "font-strikeout"},
  1419. {"fst", "font-strikeout"},
  1420. {"d", "font-strikeout"},
  1421. {"font-offset", "font-superscript"},
  1422. {"superscript", "font-superscript"},
  1423. {"fss", "font-superscript"},
  1424. {"ss", "font-superscript"},
  1425. {"bg-color", "background-color"},
  1426. {"bg-c", "background-color"},
  1427. {"bgc", "background-color"},
  1428. {"align", "text-align"},
  1429. {"wrap", "WrapText"},
  1430. {"inden", "Indention"},
  1431. {"in", "Indention"},
  1432. {"v-align", "vertical-align"},
  1433. {"b-t", "border-type"},
  1434. {"b", "border-type"},
  1435. {"bt", "top-border-type"},
  1436. {"br", "right-border-type"},
  1437. {"bb", "bottom-border-type"},
  1438. {"bl", "left-border-type"},
  1439. {"b-c", "border-color"},
  1440. {"bc", "border-color"},
  1441. {"btc", "top-border-color"},
  1442. {"brc", "right-border-color"},
  1443. {"bbc", "bottom-border-color"},
  1444. {"blc", "left-border-color"},
  1445. {"format", "data-format"},
  1446. {"df", "data-format"}
  1447. };
  1448. #endregion 转换Css的 Key
  1449. #region 样式转换
  1450. #region font-weight
  1451. private static short FontWeight(this SortedDictionary<string, string> fontdic)
  1452. {
  1453. switch (fontdic["font-weight"])
  1454. {
  1455. case "NORMAL":
  1456. return 400;
  1457. case "BOLD":
  1458. return 700;
  1459. default:
  1460. return 0;
  1461. }
  1462. }
  1463. #endregion font-weight
  1464. #region font-name
  1465. private static string FontName(this SortedDictionary<string, string> fontdic)
  1466. {
  1467. return fontdic["font-name"];
  1468. }
  1469. #endregion font-name
  1470. #region font-size
  1471. private static short FontSize(this SortedDictionary<string, string> fontdic)
  1472. {
  1473. return short.TryParse(fontdic["font-size"], out var value) ? value : (short)10;
  1474. }
  1475. #endregion font-size
  1476. #region font-color
  1477. private static short FontColor(this SortedDictionary<string, string> fontdic)
  1478. {
  1479. var color = fontdic["font-color"].ConvertToColor();
  1480. return color;
  1481. }
  1482. #endregion font-color
  1483. #region font-italic
  1484. private static bool FontItalic(this SortedDictionary<string, string> fontdic)
  1485. {
  1486. return fontdic["font-italic"] == "TRUE";
  1487. }
  1488. #endregion font-italic
  1489. #region font-strikeout
  1490. /// <summary>
  1491. /// 删除线
  1492. /// </summary>
  1493. /// <param name="fontdic"></param>
  1494. /// <returns></returns>
  1495. private static bool FontStrikeout(this SortedDictionary<string, string> fontdic)
  1496. {
  1497. return fontdic["font-strikeout"] == "TRUE";
  1498. }
  1499. #endregion font-strikeout
  1500. #region WrapText
  1501. private static void TextWrap(this ICellStyle style, string v)
  1502. {
  1503. style.WrapText = v.ToUpper() == "TRUE";
  1504. }
  1505. #endregion WrapText
  1506. #region TextIndention
  1507. /// <summary>
  1508. /// 缩进
  1509. /// </summary>
  1510. /// <param name="style"></param>
  1511. /// <param name="v"></param>
  1512. private static void TextIndention(this ICellStyle style, string v)
  1513. {
  1514. if (short.TryParse(v, out var value))
  1515. {
  1516. style.Indention = value;
  1517. }
  1518. }
  1519. #endregion TextIndention
  1520. #region text-align
  1521. private static void TextAlign(this ICellStyle style, string v)
  1522. {
  1523. style.Alignment = v.ConvertToHorizontalAlignment();
  1524. }
  1525. #endregion text-align
  1526. #region vertical-align
  1527. private static void VerticalAlign(this ICellStyle style, string v)
  1528. {
  1529. style.VerticalAlignment = v.ConvertToVerticalAlignment();
  1530. }
  1531. #endregion vertical-align
  1532. #region boder-type / boder-color
  1533. internal static void BorderTypes(this ICellStyle style, string v)
  1534. {
  1535. if (string.IsNullOrEmpty(v))
  1536. return;
  1537. string[] borderTypeNames = { string.Empty, string.Empty, string.Empty, string.Empty };
  1538. v = v.ToUpper();
  1539. var vs = v.Split(' ');
  1540. switch (vs.Length)
  1541. {
  1542. case 1:
  1543. borderTypeNames[0] = borderTypeNames[1] = borderTypeNames[2] = borderTypeNames[3] = vs[0];
  1544. break;
  1545. case 2:
  1546. borderTypeNames[0] = borderTypeNames[2] = vs[0];
  1547. borderTypeNames[1] = borderTypeNames[3] = vs[1];
  1548. break;
  1549. case 3:
  1550. borderTypeNames[0] = vs[0];
  1551. borderTypeNames[1] = borderTypeNames[3] = vs[1];
  1552. borderTypeNames[2] = vs[2];
  1553. break;
  1554. case 4:
  1555. borderTypeNames[0] = vs[0];
  1556. borderTypeNames[1] = vs[1];
  1557. borderTypeNames[2] = vs[2];
  1558. borderTypeNames[3] = vs[3];
  1559. break;
  1560. }
  1561. var borderTopTypeName = borderTypeNames[0];
  1562. var borderRightTypeName = borderTypeNames[1];
  1563. var borderBottomTypeName = borderTypeNames[2];
  1564. var borderLeftTypeName = borderTypeNames[3];
  1565. if (!string.IsNullOrWhiteSpace(borderTopTypeName))
  1566. style.BorderTop = borderTopTypeName.ConvertToBorderStyle();
  1567. if (!string.IsNullOrWhiteSpace(borderRightTypeName))
  1568. style.BorderRight = borderRightTypeName.ConvertToBorderStyle();
  1569. if (!string.IsNullOrWhiteSpace(borderBottomTypeName))
  1570. style.BorderBottom = borderBottomTypeName.ConvertToBorderStyle();
  1571. if (!string.IsNullOrWhiteSpace(borderLeftTypeName))
  1572. style.BorderLeft = borderLeftTypeName.ConvertToBorderStyle();
  1573. }
  1574. private static void BorderTopTypes(this ICellStyle style, string v)
  1575. {
  1576. if (!string.IsNullOrWhiteSpace(v))
  1577. style.BorderTop = v.ConvertToBorderStyle();
  1578. }
  1579. private static void BorderBottomTypes(this ICellStyle style, string v)
  1580. {
  1581. if (!string.IsNullOrWhiteSpace(v))
  1582. style.BorderBottom = v.ConvertToBorderStyle();
  1583. }
  1584. private static void BorderLeftTypes(this ICellStyle style, string v)
  1585. {
  1586. if (!string.IsNullOrWhiteSpace(v))
  1587. style.BorderLeft = v.ConvertToBorderStyle();
  1588. }
  1589. private static void BorderRightTypes(this ICellStyle style, string v)
  1590. {
  1591. if (!string.IsNullOrWhiteSpace(v))
  1592. style.BorderRight = v.ConvertToBorderStyle();
  1593. }
  1594. internal static void BorderColors(this ICellStyle style, string v)
  1595. {
  1596. if (string.IsNullOrEmpty(v))
  1597. return;
  1598. string[] borderColors = { string.Empty, string.Empty, string.Empty, string.Empty };
  1599. v = v.ToUpper();
  1600. var vs = v.Split(' ');
  1601. switch (vs.Length)
  1602. {
  1603. case 1:
  1604. borderColors[0] = borderColors[1] = borderColors[2] = borderColors[3] = vs[0];
  1605. break;
  1606. case 2:
  1607. borderColors[0] = borderColors[2] = vs[0];
  1608. borderColors[1] = borderColors[3] = vs[1];
  1609. break;
  1610. case 3:
  1611. borderColors[0] = vs[0];
  1612. borderColors[1] = borderColors[3] = vs[1];
  1613. borderColors[2] = vs[2];
  1614. break;
  1615. case 4:
  1616. borderColors[0] = vs[0];
  1617. borderColors[1] = vs[1];
  1618. borderColors[2] = vs[2];
  1619. borderColors[3] = vs[3];
  1620. break;
  1621. }
  1622. var borderTopColor = borderColors[0];
  1623. var borderRightColor = borderColors[1];
  1624. var borderBottomColor = borderColors[2];
  1625. var borderLeftColor = borderColors[3];
  1626. if (!string.IsNullOrWhiteSpace(borderTopColor))
  1627. style.TopBorderColor = borderTopColor.ConvertToColor();
  1628. if (!string.IsNullOrWhiteSpace(borderRightColor))
  1629. style.RightBorderColor = borderRightColor.ConvertToColor();
  1630. if (!string.IsNullOrWhiteSpace(borderBottomColor))
  1631. style.BottomBorderColor = borderBottomColor.ConvertToColor();
  1632. if (!string.IsNullOrWhiteSpace(borderLeftColor))
  1633. style.LeftBorderColor = borderLeftColor.ConvertToColor();
  1634. }
  1635. private static void BorderTopColors(this ICellStyle style, string v)
  1636. {
  1637. if (!string.IsNullOrWhiteSpace(v))
  1638. style.TopBorderColor = v.ConvertToColor();
  1639. }
  1640. private static void BorderBottomColors(this ICellStyle style, string v)
  1641. {
  1642. if (!string.IsNullOrWhiteSpace(v))
  1643. style.BottomBorderColor = v.ConvertToColor();
  1644. }
  1645. private static void BorderLeftColors(this ICellStyle style, string v)
  1646. {
  1647. if (!string.IsNullOrWhiteSpace(v))
  1648. style.LeftBorderColor = v.ConvertToColor();
  1649. }
  1650. private static void BorderRightColors(this ICellStyle style, string v)
  1651. {
  1652. if (!string.IsNullOrWhiteSpace(v))
  1653. style.RightBorderColor = v.ConvertToColor();
  1654. }
  1655. #endregion boder-type / boder-color
  1656. #region data-format
  1657. private static void DataFormat(this ICellStyle style, IWorkbook workbook, string v)
  1658. {
  1659. if (string.IsNullOrEmpty(v))
  1660. return;
  1661. var df = workbook.CreateDataFormat();
  1662. style.DataFormat = df.GetFormat(v);
  1663. }
  1664. #endregion data-format
  1665. #region BackgroundColor
  1666. private static void BackgroundColor(this ICellStyle style, string v)
  1667. {
  1668. if (string.IsNullOrEmpty(v))
  1669. return;
  1670. style.FillPattern = FillPattern.SolidForeground;
  1671. style.FillForegroundColor = v.ConvertToColor();
  1672. }
  1673. #endregion BackgroundColor
  1674. private static FontSuperScript ConvertToSuperScript(this SortedDictionary<string, string> fontdic)
  1675. {
  1676. var v = fontdic["font-superscript"];
  1677. switch (v)
  1678. {
  1679. case "SUPER":
  1680. return FontSuperScript.Super;
  1681. case "SUB":
  1682. return FontSuperScript.Sub;
  1683. default:
  1684. return FontSuperScript.None;
  1685. }
  1686. }
  1687. private static FontUnderlineType FontUnderline(this SortedDictionary<string, string> fontdic)
  1688. {
  1689. var v = fontdic["font-underline"];
  1690. switch (v)
  1691. {
  1692. case "SINGLE":
  1693. return FontUnderlineType.Single;
  1694. case "DOUBLE":
  1695. return FontUnderlineType.Double;
  1696. case "SINGLEACCOUNTING":
  1697. case "SINGLE_ACCOUNTING":
  1698. return FontUnderlineType.SingleAccounting;
  1699. case "DOUBLEACCOUNTING":
  1700. case "DOUBLE_ACCOUNTING":
  1701. return FontUnderlineType.DoubleAccounting;
  1702. default:
  1703. return FontUnderlineType.None;
  1704. }
  1705. }
  1706. public static short ConvertToColor(this string v)
  1707. {
  1708. if (string.IsNullOrEmpty(v))
  1709. return 32767;
  1710. switch (v.ToUpper())
  1711. {
  1712. case "AQUA":
  1713. return (short)ColorType.Aqua;
  1714. case "AUTOMATIC":
  1715. return (short)ColorType.Automatic;
  1716. case "BLACK":
  1717. return (short)ColorType.Black;
  1718. case "BLUE":
  1719. return (short)ColorType.Blue;
  1720. case "BLUE_GREY":
  1721. case "BLUEGREY":
  1722. return (short)ColorType.BlueGrey;
  1723. case "BRIGHT_GREEN":
  1724. case "BRIGHTGREEN":
  1725. return (short)ColorType.BrightGreen;
  1726. case "BROWN":
  1727. return (short)ColorType.Brown;
  1728. case "CORAL":
  1729. return (short)ColorType.Coral;
  1730. case "CORNFLOWER_BLUE":
  1731. case "CORNFLOWERBLUE":
  1732. return (short)ColorType.CornflowerBlue;
  1733. case "DARK_BLUE":
  1734. case "DARKBLUE":
  1735. return (short)ColorType.DarkBlue;
  1736. case "DARK_GREEN":
  1737. case "DARKGREEN":
  1738. return (short)ColorType.DarkGreen;
  1739. case "DARK_RED":
  1740. case "DARKRED":
  1741. return (short)ColorType.DarkRed;
  1742. case "DARK_TEAL":
  1743. case "DARKTEAL":
  1744. return (short)ColorType.DarkTeal;
  1745. case "DARK_YELLOW":
  1746. case "DARKYELLOW":
  1747. return (short)ColorType.DarkYellow;
  1748. case "GOLD":
  1749. return (short)ColorType.Gold;
  1750. case "GREEN":
  1751. return (short)ColorType.Green;
  1752. case "GREY_25_PERCENT":
  1753. case "GREY25PERCENT":
  1754. return (short)ColorType.Grey25Percent;
  1755. case "GREY_40_PERCENT":
  1756. case "GREY40PERCENT":
  1757. return (short)ColorType.Grey40Percent;
  1758. case "GREY_50_PERCENT":
  1759. case "GREY50PERCENT":
  1760. return (short)ColorType.Grey50Percent;
  1761. case "GREY_80_PERCENT":
  1762. case "GREY80PERCENT":
  1763. return (short)ColorType.Grey80Percent;
  1764. case "INDIGO":
  1765. return (short)ColorType.Indigo;
  1766. case "LAVENDER":
  1767. return (short)ColorType.Lavender;
  1768. case "LEMON_CHIFFON":
  1769. case "LEMONCHIFFON":
  1770. return (short)ColorType.LemonChiffon;
  1771. case "LIGHT_BLUE":
  1772. case "LIGHTBLUE":
  1773. return (short)ColorType.LightBlue;
  1774. case "LIGHT_CORNFLOWERBLUE":
  1775. case "LIGHTCORNFLOWERBLUE":
  1776. return (short)ColorType.LightCornflowerBlue;
  1777. case "LIGHT_GREEN":
  1778. case "LIGHTGREEN":
  1779. return (short)ColorType.LightGreen;
  1780. case "LIGHT_ORANGE":
  1781. case "LIGHTORANGE":
  1782. return (short)ColorType.LightOrange;
  1783. case "LIGHT_TURQUOISE":
  1784. case "LIGHTTURQUOISE":
  1785. return (short)ColorType.LightTurquoise;
  1786. case "LIGHT_YELLOW":
  1787. case "LIGHTYELLOW":
  1788. return (short)ColorType.LightYellow;
  1789. case "LIME":
  1790. return (short)ColorType.Lime;
  1791. case "MAROON":
  1792. return (short)ColorType.Maroon;
  1793. case "OLIVE_GREEN":
  1794. case "OLIVEGREEN":
  1795. return (short)ColorType.OliveGreen;
  1796. case "ORANGE":
  1797. return (short)ColorType.Orange;
  1798. case "ORCHID":
  1799. return (short)ColorType.Orchid;
  1800. case "PALE_BLUE":
  1801. case "PALEBLUE":
  1802. return (short)ColorType.PaleBlue;
  1803. case "PINK":
  1804. return (short)ColorType.Pink;
  1805. case "PLUM":
  1806. return (short)ColorType.Plum;
  1807. case "RED":
  1808. return (short)ColorType.Red;
  1809. case "ROSE":
  1810. return (short)ColorType.Rose;
  1811. case "ROYAL_BLUE":
  1812. case "ROYALBLUE":
  1813. return (short)ColorType.RoyalBlue;
  1814. case "SEA_GREEN":
  1815. case "SEAGREEN":
  1816. return (short)ColorType.SeaGreen;
  1817. case "SKY_BLUE":
  1818. case "SKYBLUE":
  1819. return (short)ColorType.SkyBlue;
  1820. case "TAN":
  1821. return (short)ColorType.Tan;
  1822. case "TEAL":
  1823. return (short)ColorType.Teal;
  1824. case "TURQUOISE":
  1825. return (short)ColorType.Turquoise;
  1826. case "VIOLET":
  1827. return (short)ColorType.Violet;
  1828. case "WHITE":
  1829. return (short)ColorType.White;
  1830. case "YELLOW":
  1831. return (short)ColorType.Yellow;
  1832. default:
  1833. return 32767;
  1834. }
  1835. }
  1836. private static HorizontalAlignment ConvertToHorizontalAlignment(this string v)
  1837. {
  1838. if (string.IsNullOrEmpty(v))
  1839. return HorizontalAlignment.General;
  1840. switch (v.ToUpper())
  1841. {
  1842. case "LEFT":
  1843. return HorizontalAlignment.Left;
  1844. case "CENTER":
  1845. return HorizontalAlignment.Center;
  1846. case "CENTERSELECTION":
  1847. case "CENTER_SELECTION":
  1848. return HorizontalAlignment.CenterSelection;
  1849. case "RIGHT":
  1850. return HorizontalAlignment.Right;
  1851. case "DISTRIBUTED":
  1852. return HorizontalAlignment.Distributed;
  1853. case "FILL":
  1854. return HorizontalAlignment.Fill;
  1855. case "JUSTIFY":
  1856. return HorizontalAlignment.Justify;
  1857. default:
  1858. return HorizontalAlignment.General;
  1859. }
  1860. }
  1861. private static VerticalAlignment ConvertToVerticalAlignment(this string v)
  1862. {
  1863. if (string.IsNullOrEmpty(v))
  1864. return VerticalAlignment.Justify;
  1865. switch (v.ToUpper())
  1866. {
  1867. case "TOP":
  1868. return VerticalAlignment.Top;
  1869. case "CENTER":
  1870. return VerticalAlignment.Center;
  1871. case "BOTTOM":
  1872. return VerticalAlignment.Bottom;
  1873. case "DISTRIBUTED":
  1874. return VerticalAlignment.Distributed;
  1875. default:
  1876. return VerticalAlignment.Justify;
  1877. }
  1878. }
  1879. public static BorderStyle ConvertToBorderStyle(this string v)
  1880. {
  1881. if (string.IsNullOrEmpty(v))
  1882. return BorderStyle.None;
  1883. switch (v.ToUpper())
  1884. {
  1885. case "THIN":
  1886. return BorderStyle.Thin;
  1887. case "MEDIUM":
  1888. return BorderStyle.Medium;
  1889. case "DASHED":
  1890. return BorderStyle.Dashed;
  1891. case "HAIR":
  1892. return BorderStyle.Hair;
  1893. case "THICK":
  1894. return BorderStyle.Thick;
  1895. case "DOUBLE":
  1896. return BorderStyle.Double;
  1897. case "DOTTED":
  1898. return BorderStyle.Dotted;
  1899. case "MEDIUMDASHED":
  1900. case "MEDIUM_DASHED":
  1901. return BorderStyle.MediumDashed;
  1902. case "DASHDOT":
  1903. case "DASH_DOT":
  1904. return BorderStyle.DashDot;
  1905. case "MEDIUMDASHDOT":
  1906. case "MEDIUM_DASH_DOT":
  1907. return BorderStyle.MediumDashDot;
  1908. case "DASHDOTDOT":
  1909. case "DASH_DOT_DOT":
  1910. return BorderStyle.DashDotDot;
  1911. case "MEDIUMDASHDOTDOT":
  1912. case "MEDIUM_DASH_DOT_DOT":
  1913. return BorderStyle.MediumDashDotDot;
  1914. case "SLANTEDDASHDOT":
  1915. case "SLANTED_DASH_DOT":
  1916. return BorderStyle.SlantedDashDot;
  1917. default:
  1918. return BorderStyle.None;
  1919. }
  1920. }
  1921. #endregion 样式转换
  1922. #endregion 解析css样式
  1923. }
  1924. public enum ColorType
  1925. {
  1926. Black = HSSFColor.Black.Index,
  1927. Brown = HSSFColor.Brown.Index,
  1928. OliveGreen = HSSFColor.OliveGreen.Index,
  1929. DarkGreen = HSSFColor.DarkGreen.Index,
  1930. DarkTeal = HSSFColor.DarkTeal.Index,
  1931. DarkBlue = HSSFColor.DarkBlue.Index,
  1932. Indigo = HSSFColor.Indigo.Index,
  1933. Grey80Percent = HSSFColor.Grey80Percent.Index,
  1934. Orange = HSSFColor.Orange.Index,
  1935. DarkYellow = HSSFColor.DarkYellow.Index,
  1936. Green = HSSFColor.Green.Index,
  1937. Teal = HSSFColor.Teal.Index,
  1938. Blue = HSSFColor.Blue.Index,
  1939. BlueGrey = HSSFColor.BlueGrey.Index,
  1940. Grey50Percent = HSSFColor.Grey50Percent.Index,
  1941. Red = HSSFColor.Red.Index,
  1942. LightOrange = HSSFColor.LightOrange.Index,
  1943. Lime = HSSFColor.Lime.Index,
  1944. SeaGreen = HSSFColor.SeaGreen.Index,
  1945. Aqua = HSSFColor.Aqua.Index,
  1946. LightBlue = HSSFColor.LightBlue.Index,
  1947. Violet = HSSFColor.Violet.Index,
  1948. Grey40Percent = HSSFColor.Grey40Percent.Index,
  1949. Pink = HSSFColor.Pink.Index,
  1950. Gold = HSSFColor.Gold.Index,
  1951. Yellow = HSSFColor.Yellow.Index,
  1952. BrightGreen = HSSFColor.BrightGreen.Index,
  1953. Turquoise = HSSFColor.Turquoise.Index,
  1954. DarkRed = HSSFColor.DarkRed.Index,
  1955. SkyBlue = HSSFColor.SkyBlue.Index,
  1956. Plum = HSSFColor.Plum.Index,
  1957. Grey25Percent = HSSFColor.Grey25Percent.Index,
  1958. Rose = HSSFColor.Rose.Index,
  1959. LightYellow = HSSFColor.LightYellow.Index,
  1960. LightGreen = HSSFColor.LightGreen.Index,
  1961. LightTurquoise = HSSFColor.LightTurquoise.Index,
  1962. PaleBlue = HSSFColor.PaleBlue.Index,
  1963. Lavender = HSSFColor.Lavender.Index,
  1964. White = HSSFColor.White.Index,
  1965. CornflowerBlue = HSSFColor.CornflowerBlue.Index,
  1966. LemonChiffon = HSSFColor.LemonChiffon.Index,
  1967. Maroon = HSSFColor.Maroon.Index,
  1968. Orchid = HSSFColor.Orchid.Index,
  1969. Coral = HSSFColor.Coral.Index,
  1970. RoyalBlue = HSSFColor.RoyalBlue.Index,
  1971. LightCornflowerBlue = HSSFColor.LightCornflowerBlue.Index,
  1972. Tan = HSSFColor.Tan.Index,
  1973. Automatic = HSSFColor.Automatic.Index
  1974. }
  1975. public static class TransExp<TIn, TOut>
  1976. {
  1977. private static readonly Func<TIn, TOut> Cache = GetFunc();
  1978. private static Func<TIn, TOut> GetFunc()
  1979. {
  1980. ParameterExpression parameterExpression = Expression.Parameter(typeof(TIn), "p");
  1981. List<MemberBinding> memberBindingList = new List<MemberBinding>();
  1982. foreach (var item in typeof(TOut).GetProperties())
  1983. {
  1984. if (!item.CanWrite)
  1985. continue;
  1986. MemberExpression property = Expression.Property(parameterExpression, typeof(TIn).GetProperty(item.Name) ?? throw new InvalidOperationException());
  1987. MemberBinding memberBinding = Expression.Bind(item, property);
  1988. memberBindingList.Add(memberBinding);
  1989. }
  1990. MemberInitExpression memberInitExpression = Expression.MemberInit(Expression.New(typeof(TOut)), memberBindingList.ToArray());
  1991. Expression<Func<TIn, TOut>> lambda = Expression.Lambda<Func<TIn, TOut>>(memberInitExpression, new ParameterExpression[] { parameterExpression });
  1992. return lambda.Compile();
  1993. }
  1994. public static TOut Trans(TIn tIn)
  1995. {
  1996. return Cache(tIn);
  1997. }
  1998. }