DataExportHelper.cs 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617
  1. using DllEapEntity.Dtos;
  2. using DllEapEntity.OFILM;
  3. using DllUfpEntity;
  4. using NPOI.HSSF.UserModel;
  5. using NPOI.HSSF.Util;
  6. using NPOI.SS.UserModel;
  7. using NPOI.SS.Util;
  8. using NPOI.XSSF.UserModel;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.ComponentModel;
  12. using System.Data;
  13. using System.Drawing;
  14. using System.IO;
  15. using System.Linq;
  16. using System.Reflection;
  17. using System.Text;
  18. using System.Threading.Tasks;
  19. namespace DllEapCommon.NPOI
  20. {
  21. /// <summary>
  22. /// 列表导出到Excel
  23. /// </summary>
  24. public class DataExportHelper
  25. {
  26. /// <summary>
  27. /// 导出机台时段产量数据
  28. /// </summary>
  29. /// <param name="dto"></param>
  30. /// <param name="errorinfo"></param>
  31. /// <returns></returns>
  32. public static byte[] ExportRunDataToExcel(AntdComplexTableDto dto, ref string errorinfo)
  33. {
  34. var dt = dto.Datas;
  35. if (dt == null || dt.Rows.Count <= 0)
  36. {
  37. errorinfo = "待导出数据为空";
  38. return null;
  39. }
  40. var workbook = new HSSFWorkbook();
  41. var sheet = workbook.CreateSheet("sheet1");
  42. var currentRow = 0;
  43. var currentCol = 0;
  44. var baseHeaderWidth = 800;
  45. var baseRowWidth = 800;
  46. var headerMargin = 900;
  47. var rowMargin = 500;
  48. var contentFontSize = 11;
  49. var palette = workbook.GetCustomPalette();
  50. palette.SetColorAtIndex((short)60, 255, 242, 204);
  51. palette.SetColorAtIndex((short)61, 226, 239, 218);
  52. palette.SetColorAtIndex((short)62, 230, 247, 255);
  53. palette.SetColorAtIndex((short)63, 217, 217, 217);
  54. // 表格内容样式
  55. ICellStyle style = CreateStyle(workbook, (short)9, contentFontSize);
  56. ICellStyle headerStyle = CreateStyle(workbook, palette.FindColor(226, 239, 218).Indexed, 12, true);
  57. ICellStyle coloredStyle = CreateStyle(workbook, palette.FindColor(230, 247, 255).Indexed, contentFontSize);
  58. // 头部
  59. var headerRow = sheet.CreateRow(currentRow);
  60. var currHeaderWidth = 0;
  61. foreach (var col in dto.Columns)
  62. {
  63. currHeaderWidth = baseHeaderWidth * col.ColName.Length + headerMargin;
  64. sheet.SetColumnWidth(currentCol, currHeaderWidth);
  65. ICell headerCell = headerRow.CreateCell(currentCol);
  66. if (col.ColName == "MacRowSpan" || col.ColName == "ModelRowSpan")
  67. continue;
  68. DateTime tempDate;
  69. var cellValue = col.ColName;
  70. if (DateTime.TryParse(col.ColName, out tempDate))
  71. {
  72. cellValue = tempDate.ToString("yyyy-MM-dd HH:mm");
  73. sheet.SetColumnWidth(currentCol, 200 * col.ColName.Length + 700);
  74. }
  75. headerCell.SetCellValue(cellValue);
  76. headerCell.CellStyle = headerStyle;
  77. headerRow.Cells.Add(headerCell);
  78. currentCol++;
  79. }
  80. // Body
  81. currentRow = 1;
  82. var startMacRow = 0;
  83. var endMacRow = 0;
  84. var startModelRow = 0;
  85. var endModelRow = 0;
  86. var macName = string.Empty;
  87. var modelName = string.Empty;
  88. var regions = new List<CellRangeAddress>();
  89. foreach (DataRow row in dt.Rows)
  90. {
  91. IRow bodyRow = sheet.CreateRow(currentRow);
  92. currentCol = 0;
  93. foreach (DataColumn col in dt.Columns)
  94. {
  95. if (col.ColumnName == "MacRowSpan" || col.ColumnName == "ModelRowSpan")
  96. continue;
  97. ICell cell = bodyRow.CreateCell(currentCol);
  98. double tempDouble;
  99. var cellValue = row[col.ColumnName].ToString();
  100. if (double.TryParse(cellValue, out tempDouble))
  101. {
  102. if (row["指标"].ToString() == "产量" || row["指标"].ToString() == "故障次数")
  103. {
  104. cellValue = row[col.ColumnName].ToString();
  105. }
  106. else if (row["指标"].ToString() == "故障时长")
  107. {
  108. cellValue = Math.Round(tempDouble, 2).ToString();
  109. }
  110. else
  111. {
  112. cellValue = Math.Round(tempDouble * 100, 2).ToString() + "%";
  113. }
  114. }
  115. cell.SetCellValue(cellValue);
  116. cell.CellStyle = style;
  117. if (col.ColumnName != "设备ID" && col.ColumnName != "设备类型" && (row["指标"].ToString() == "产量" || row["指标"].ToString() == "稼动率"))
  118. {
  119. cell.CellStyle = coloredStyle;
  120. }
  121. if (col.ColumnName == "设备ID" || col.ColumnName == "设备类型")
  122. {
  123. var singleWidth = 300;
  124. var width = singleWidth * (row[col.ColumnName].ToString().Length) + rowMargin;
  125. if (width > currHeaderWidth)
  126. {
  127. sheet.SetColumnWidth(currentCol, width);
  128. currHeaderWidth = width;
  129. }
  130. }
  131. else if (col.ColumnName == "指标")
  132. {
  133. var singleWidth = 600;
  134. var width = singleWidth * (row[col.ColumnName].ToString().Length) + rowMargin;
  135. if (width > currHeaderWidth)
  136. {
  137. sheet.SetColumnWidth(currentCol, width);
  138. currHeaderWidth = width;
  139. }
  140. }
  141. bodyRow.Cells.Add(cell);
  142. currentCol++;
  143. }
  144. if (row["设备类型"].ToString() == modelName && currentRow != dt.Rows.Count)
  145. {
  146. endModelRow++;
  147. }
  148. else
  149. {
  150. if (!string.IsNullOrEmpty(modelName))
  151. {
  152. if (currentRow == dt.Rows.Count)
  153. {
  154. endModelRow++;
  155. }
  156. regions.Add(new CellRangeAddress(startModelRow, endModelRow, 0, 0));
  157. }
  158. startModelRow = currentRow;
  159. endModelRow = currentRow;
  160. modelName = row["设备类型"].ToString();
  161. }
  162. if (row["设备ID"].ToString() == macName && currentRow != dt.Rows.Count)
  163. {
  164. endMacRow++;
  165. }
  166. else
  167. {
  168. if (!string.IsNullOrEmpty(macName))
  169. {
  170. if (currentRow == dt.Rows.Count)
  171. {
  172. endMacRow++;
  173. }
  174. regions.Add(new CellRangeAddress(startMacRow, endMacRow, 1, 1));
  175. }
  176. startMacRow = currentRow;
  177. endMacRow = currentRow;
  178. macName = row["设备ID"].ToString();
  179. }
  180. currentRow++;
  181. }
  182. if (regions != null && regions.Count > 0)
  183. {
  184. foreach (var item in regions)
  185. {
  186. sheet.AddMergedRegion(item);
  187. }
  188. }
  189. MemoryStream ms = new MemoryStream();
  190. workbook.Write(ms);
  191. var buffer = ms.ToArray();
  192. return buffer;
  193. }
  194. public static IWorkbook SULToExcel(List<DllEapEntity.SystemUpdataLogDto> dto)
  195. {
  196. IWorkbook workbook = new HSSFWorkbook();
  197. ISheet sheet = workbook.CreateSheet("Sheet1");
  198. List<string> column = new List<string> { "版本号", "更新时间", "更新环境", "更新类型", "更新内容", "业务对接部门", "对接人", "更新人","测试结果","测试人员","备注" };
  199. int rowIndex = 1, piIndex = 0;
  200. var headerCellStyle = CreateStyle(workbook, fontSize: 11, bold: true);
  201. var cellStyle = CreateStyle(workbook, fontSize: 11);
  202. var prenCellStyle = CreateStyle(workbook, fontSize: 11, type: "0");
  203. var headerRow = sheet.CreateRow(0);
  204. foreach (var item in column)
  205. {
  206. var headerCell = headerRow.CreateCell(piIndex, CellType.String);
  207. headerCell.SetCellValue(item);
  208. headerCell.CellStyle = headerCellStyle;
  209. sheet.SetColumnWidth(piIndex, 800 * item.Length + 700);
  210. piIndex++;
  211. }
  212. foreach (var item in dto)
  213. {
  214. int length = 0;
  215. int count = item.list.Count();
  216. while (length < count)
  217. {
  218. var row = sheet.CreateRow(rowIndex);
  219. piIndex = 0;
  220. while (piIndex<column.Count())
  221. {
  222. var cell = row.CreateCell(piIndex);
  223. switch (piIndex)
  224. {
  225. case 0:
  226. cell.SetCellValue(item.Version);
  227. cell.CellStyle = cellStyle;
  228. break;
  229. case 1:
  230. cell.SetCellValue(item.FDate);
  231. cell.CellStyle = cellStyle;
  232. break;
  233. case 2:
  234. if (item.Environment == 0)
  235. {
  236. cell.SetCellValue("测试版");
  237. }
  238. else
  239. {
  240. cell.SetCellValue("正式版");
  241. }
  242. cell.CellStyle = cellStyle;
  243. break;
  244. case 3:
  245. if (item.list[length].UpdateType == 0)
  246. {
  247. cell.SetCellValue("新增");
  248. }
  249. else
  250. {
  251. cell.SetCellValue("修改");
  252. }
  253. cell.CellStyle = cellStyle;
  254. break;
  255. case 4:
  256. cell.SetCellValue(item.list[length].Content);
  257. cell.CellStyle = cellStyle;
  258. break;
  259. case 5:
  260. cell.SetCellValue(item.list[length].Department);
  261. cell.CellStyle = cellStyle;
  262. break;
  263. case 6:
  264. cell.SetCellValue(item.list[length].Employee);
  265. cell.CellStyle = cellStyle;
  266. break;
  267. case 7:
  268. cell.SetCellValue(item.list[length].Developer);
  269. cell.CellStyle = cellStyle;
  270. break;
  271. case 8:
  272. cell.SetCellValue(item.list[length].Result);
  273. cell.CellStyle = cellStyle;
  274. break;
  275. case 9:
  276. cell.SetCellValue(item.list[length].TE);
  277. cell.CellStyle = cellStyle;
  278. break;
  279. case 10:
  280. cell.SetCellValue(item.list[length].Remark);
  281. cell.CellStyle = cellStyle;
  282. break;
  283. default:
  284. break;
  285. }
  286. piIndex++;
  287. }
  288. length++;
  289. rowIndex++;
  290. }
  291. if (length>1)
  292. {
  293. for (int i = 0; i < 3; i++)
  294. {
  295. sheet.AddMergedRegion(new CellRangeAddress(rowIndex - length, rowIndex - 1, i, i));
  296. }
  297. }
  298. }
  299. return workbook;
  300. }
  301. public static IWorkbook ExportWeek(IEnumerable<CountTotal> data, ref string errorinfo)
  302. {
  303. IWorkbook workbook = new HSSFWorkbook();
  304. ISheet sheet = workbook.CreateSheet("Sheet1");
  305. List<string> column = new List<string> { "园区", "楼层", "制程代码", "设备类型", "设备ID", "指标" };
  306. for (int i = 0; i < 24; i++)
  307. {
  308. column.Add(i.ToString("00") + ":00:00");
  309. }
  310. //IRow headerRow = sheet.CreateRow(0);
  311. int rowIndex = 1, piIndex = 0;
  312. var headerCellStyle = CreateStyle(workbook, fontSize: 11, bold: true);
  313. var cellStyle = CreateStyle(workbook, fontSize: 11);
  314. var prenCellStyle = CreateStyle(workbook, fontSize: 11, type: "0");
  315. var headerRow = sheet.CreateRow(0);
  316. foreach (var item in column)
  317. {
  318. var headerCell = headerRow.CreateCell(piIndex,CellType.String);
  319. headerCell.SetCellValue(item);
  320. headerCell.CellStyle = headerCellStyle;
  321. sheet.SetColumnWidth(piIndex, 800 * item.Length + 700);
  322. piIndex++;
  323. }
  324. foreach (var item in data)
  325. {
  326. int length = 0;
  327. while (length < 6)
  328. {
  329. var row = sheet.CreateRow(rowIndex);
  330. int i = 0;
  331. while(i<column.Count)
  332. {
  333. var dataCell = row.CreateCell(i);
  334. switch (i)
  335. {
  336. case 0:
  337. dataCell.SetCellValue(item.Factory);
  338. dataCell.CellStyle=cellStyle;
  339. break;
  340. case 1:
  341. dataCell.SetCellValue(item.Floor);
  342. dataCell.CellStyle = cellStyle;
  343. break;
  344. case 2:
  345. dataCell.SetCellValue(item.PCode);
  346. dataCell.CellStyle = cellStyle;
  347. break;
  348. case 3:
  349. dataCell.SetCellValue(item.Model);
  350. dataCell.CellStyle = cellStyle;
  351. break;
  352. case 4:
  353. dataCell.SetCellValue(item.FCode);
  354. dataCell.CellStyle = cellStyle;
  355. break;
  356. case 5:
  357. dataCell.SetCellValue(item.Datas[length].Index);
  358. dataCell.CellStyle = cellStyle;
  359. break;
  360. default:
  361. switch (length)
  362. {
  363. case 0:
  364. case 4:
  365. dataCell.SetCellValue(Convert.ToInt32(item.Datas[length].data[i - 6]));
  366. dataCell.CellStyle = cellStyle;
  367. break;
  368. case 1:
  369. case 2:
  370. case 3:
  371. dataCell.SetCellValue(Convert.ToDouble(item.Datas[length].data[i - 6].Replace("%", "")) /100);
  372. dataCell.CellStyle = prenCellStyle;
  373. break;
  374. case 5:
  375. dataCell.SetCellValue(Convert.ToDouble(item.Datas[length].data[i - 6]));
  376. dataCell.CellStyle = cellStyle;
  377. break;
  378. default:
  379. break;
  380. }
  381. break;
  382. }
  383. i++;
  384. }
  385. rowIndex++;
  386. length++;
  387. }
  388. if (item.Factory == "汇总")
  389. {
  390. sheet.AddMergedRegion(new CellRangeAddress(rowIndex-6, rowIndex-1, 0,4));
  391. }
  392. else
  393. {
  394. for (int i = 0; i < 5; i++)
  395. {
  396. sheet.AddMergedRegion(new CellRangeAddress(rowIndex-6, rowIndex-1 , i, i));
  397. }
  398. }
  399. }
  400. return workbook;
  401. }
  402. /// <summary>
  403. /// 导出预测数据
  404. /// </summary>
  405. /// <param name="dto"></param>
  406. /// <param name="errorinfo"></param>
  407. /// <returns></returns>
  408. public static byte[] ExportForecastToExcel(AntdComplexTableDto dto, ref string errorinfo)
  409. {
  410. var dt = dto.Datas;
  411. if (dt == null || dt.Rows.Count <= 0)
  412. {
  413. errorinfo = "待导出数据为空";
  414. return null;
  415. }
  416. var workbook = new HSSFWorkbook();
  417. var sheet = workbook.CreateSheet("sheet1");
  418. var currentRow = 0;
  419. var currentCol = 0;
  420. var baseHeaderWidth = 800;
  421. var baseRowWidth = 800;
  422. var headerMargin = 900;
  423. var rowMargin = 500;
  424. var contentFontSize = 11;
  425. var palette = workbook.GetCustomPalette();
  426. palette.SetColorAtIndex((short)60, 255, 242, 204);
  427. palette.SetColorAtIndex((short)61, 226, 239, 218);
  428. palette.SetColorAtIndex((short)62, 230, 247, 255);
  429. palette.SetColorAtIndex((short)63, 217, 217, 217);
  430. // 表格内容样式
  431. ICellStyle style = CreateStyle(workbook, (short)9, contentFontSize);
  432. ICellStyle headerStyle = CreateStyle(workbook, palette.FindColor(226, 239, 218).Indexed, 12, true);
  433. ICellStyle coloredStyle = CreateStyle(workbook, palette.FindColor(230, 247, 255).Indexed, contentFontSize);
  434. // 头部
  435. var headerRow = sheet.CreateRow(currentRow);
  436. var currHeaderWidth = 0;
  437. foreach (var col in dto.Columns)
  438. {
  439. currHeaderWidth = baseHeaderWidth * col.ColName.Length + headerMargin;
  440. sheet.SetColumnWidth(currentCol, currHeaderWidth);
  441. ICell headerCell = headerRow.CreateCell(currentCol);
  442. if (col.ColName == "ParkRowSpan" || col.ColName == "ModelRowSpan")
  443. continue;
  444. DateTime tempDate;
  445. var cellValue = col.ColName;
  446. if (DateTime.TryParse(col.ColName, out tempDate))
  447. {
  448. cellValue = tempDate.ToString("yyyy-MM-dd HH:mm");
  449. sheet.SetColumnWidth(currentCol, 200 * col.ColName.Length + 700);
  450. }
  451. headerCell.SetCellValue(cellValue);
  452. headerCell.CellStyle = headerStyle;
  453. headerRow.Cells.Add(headerCell);
  454. currentCol++;
  455. }
  456. // Body
  457. currentRow = 1;
  458. var startMacRow = 0;
  459. var endMacRow = 0;
  460. var startModelRow = 0;
  461. var endModelRow = 0;
  462. var regions = new List<CellRangeAddress>();
  463. ICellStyle cellStylePercent = workbook.CreateCellStyle();
  464. cellStylePercent.DataFormat = HSSFDataFormat.GetBuiltinFormat("0.00%");
  465. foreach (DataRow row in dt.Rows)
  466. {
  467. IRow bodyRow = sheet.CreateRow(currentRow);
  468. currentCol = 0;
  469. foreach (DataColumn col in dt.Columns)
  470. {
  471. if (col.ColumnName == "ParkRowSpan" || col.ColumnName == "ModelRowSpan")
  472. continue;
  473. ICell cell = bodyRow.CreateCell(currentCol);
  474. double tempDouble;
  475. var cellValue = row[col.ColumnName].ToString();
  476. if (double.TryParse(cellValue, out tempDouble) || cellValue.Contains("%"))
  477. {
  478. cellValue = row[col.ColumnName].ToString();
  479. if (!cellValue.Contains("%"))
  480. {
  481. cell.SetCellValue(tempDouble);
  482. }
  483. else
  484. {
  485. cell.SetCellValue(Convert.ToDouble(cellValue.Replace("%", "")) / 100);
  486. cell.CellStyle = cellStylePercent;
  487. }
  488. }
  489. else
  490. {
  491. cell.SetCellValue(cellValue);
  492. }
  493. //cell.CellStyle = style;
  494. bodyRow.Cells.Add(cell);
  495. currentCol++;
  496. }
  497. regions.Add(new CellRangeAddress(startMacRow, endMacRow, 1, 1));
  498. currentRow++;
  499. }
  500. MemoryStream ms = new MemoryStream();
  501. workbook.Write(ms);
  502. var buffer = ms.ToArray();
  503. return buffer;
  504. }
  505. /// <summary>
  506. /// 导出机台效率数据
  507. /// </summary>
  508. /// <param name="models"></param>
  509. /// <param name="modeltotal"></param>
  510. /// <param name="total"></param>
  511. /// <param name="errorinfo"></param>
  512. /// <returns></returns>
  513. public static byte[] ExportMachineEfficiencies(IEnumerable<MachineEfDto> models, IEnumerable<MachineEfDto> modeltotal, int total, ref string errorinfo)
  514. {
  515. try
  516. {
  517. string[] cloumnsOne = new string[] { "园区", "厂房", "楼层", "制程代码", "设备类型", "设备编号", "稼动率", "待机率", "故障率", "生产", "故障率汇总", "总时长(Min)", "运行时长(Min)", "待机时长(Min)", "故障时长(Min)", "故障时长(Min)生产", "故障时长(Min)汇总", "设备当前状态" };
  518. string[] cloumnsTwo = new string[] { "园区", "厂房", "楼层", "制程代码", "设备类型", "设备编号", "稼动率", "待机率", "ME", "生产", "汇总", "总时长(Min)", "运行时长(Min)", "待机时长(Min)", "ME", "生产", "汇总", "设备当前状态" };
  519. string[] clorow = new string[] { "FactoryName", "PlantName", "FloorName", "PCode", "ModelName", "MacCode", "RunrRate", "IdleRate", "ErrorRateTwo", "ErrorRateOne", "ErrorRate", "AllTime", "RunTime", "IdleTime", "ErrorTimeTwo", "ErrorTimeOne", "ErrorTime", "StatusName" };
  520. if (models == null)
  521. {
  522. errorinfo = "待导出数据为空";
  523. return null;
  524. }
  525. var workbook = new HSSFWorkbook();
  526. var sheet = workbook.CreateSheet("sheet1");
  527. var currentRow = 0;
  528. var currentCol = 0;
  529. var baseHeaderWidth = 800;
  530. var headerMargin = 900;
  531. var contentFontSize = 11;
  532. var palette = workbook.GetCustomPalette();
  533. palette.SetColorAtIndex((short)60, 255, 242, 204);
  534. palette.SetColorAtIndex((short)61, 226, 239, 218);
  535. palette.SetColorAtIndex((short)62, 230, 247, 255);
  536. palette.SetColorAtIndex((short)63, 217, 217, 217);
  537. // 表格内容样式
  538. ICellStyle style = CreateStyle(workbook, (short)9, contentFontSize);
  539. ICellStyle Pstyle= CreateStyle(workbook, (short)9, contentFontSize,type:"True");
  540. ICellStyle headerStyle = CreateStyle(workbook, palette.FindColor(226, 239, 218).Indexed, 12, true);
  541. ICellStyle coloredStyle = CreateStyle(workbook, palette.FindColor(230, 247, 255).Indexed, contentFontSize);
  542. // 头部
  543. var headerRowOne = sheet.CreateRow(0);
  544. var headerRowTwo = sheet.CreateRow(1);
  545. var currHeaderWidth = 0;
  546. foreach (var col in cloumnsOne)
  547. {
  548. currHeaderWidth = baseHeaderWidth * col.Length + headerMargin;
  549. sheet.SetColumnWidth(currentCol, currHeaderWidth);
  550. ICell headerCell = headerRowOne.CreateCell(currentCol);
  551. var cellValue = col;
  552. headerCell.SetCellValue(cellValue);
  553. headerCell.CellStyle = headerStyle;
  554. headerRowOne.Cells.Add(headerCell);
  555. currentCol++;
  556. }
  557. currentCol = 0;
  558. currHeaderWidth = 0;
  559. foreach (var col in cloumnsTwo)
  560. {
  561. currHeaderWidth = baseHeaderWidth * col.Length + headerMargin;
  562. sheet.SetColumnWidth(currentCol, currHeaderWidth);
  563. ICell headerCell = headerRowTwo.CreateCell(currentCol);
  564. var cellValue = col;
  565. headerCell.SetCellValue(cellValue);
  566. headerCell.CellStyle = headerStyle;
  567. headerRowTwo.Cells.Add(headerCell);
  568. currentCol++;
  569. }
  570. #region 合并表头
  571. sheet.AddMergedRegion(new CellRangeAddress(0, 1, 0, 0));
  572. sheet.AddMergedRegion(new CellRangeAddress(0, 1, 1, 1));
  573. sheet.AddMergedRegion(new CellRangeAddress(0, 1, 2, 2));
  574. sheet.AddMergedRegion(new CellRangeAddress(0, 1, 3, 3));
  575. sheet.AddMergedRegion(new CellRangeAddress(0, 1, 4, 4));
  576. sheet.AddMergedRegion(new CellRangeAddress(0, 1, 5, 5));
  577. sheet.AddMergedRegion(new CellRangeAddress(0, 1, 6, 6));
  578. sheet.AddMergedRegion(new CellRangeAddress(0, 1, 7, 7));
  579. sheet.AddMergedRegion(new CellRangeAddress(0, 0, 8, 10));//故障率
  580. sheet.AddMergedRegion(new CellRangeAddress(0, 1, 11, 11));
  581. sheet.AddMergedRegion(new CellRangeAddress(0, 1, 12, 12));
  582. sheet.AddMergedRegion(new CellRangeAddress(0, 1, 13, 13));
  583. sheet.AddMergedRegion(new CellRangeAddress(0, 0, 14, 16));//故障时长
  584. sheet.AddMergedRegion(new CellRangeAddress(0, 1, 17, 17));
  585. #endregion
  586. // Body
  587. currentRow = 2;
  588. var startMacRow = 0;
  589. var endMacRow = 0;
  590. var regions = new List<CellRangeAddress>();
  591. /*ICellStyle cellStylePercent = workbook.CreateCellStyle();
  592. cellStylePercent.DataFormat = HSSFDataFormat.GetBuiltinFormat("0.00%");*/
  593. foreach (var row in modeltotal)
  594. {
  595. IRow bodyRow = sheet.CreateRow(currentRow);
  596. currentCol = 0;
  597. foreach (var col in clorow)
  598. {
  599. ICell cell = bodyRow.CreateCell(currentCol);
  600. if (currentCol == 0)
  601. {
  602. cell.SetCellValue("汇总");
  603. bodyRow.Cells.Add(cell);
  604. cell.CellStyle = style;
  605. currentCol++;
  606. continue;
  607. }
  608. else if (currentCol == 3)
  609. {
  610. cell.SetCellValue("设备数量:" + total);
  611. bodyRow.Cells.Add(cell);
  612. cell.CellStyle = style;
  613. currentCol++;
  614. continue;
  615. }
  616. var cellValue = row.GetValue(col);
  617. if (cellValue.ToString().Contains("%"))
  618. {
  619. cell.SetCellValue(Convert.ToDouble(cellValue.ToString().Replace("%", "")) / 100);
  620. cell.CellStyle = Pstyle;
  621. }
  622. else
  623. {
  624. cell.SetCellValue(cellValue);
  625. cell.CellStyle = style;
  626. }
  627. bodyRow.Cells.Add(cell);
  628. currentCol++;
  629. }
  630. regions.Add(new CellRangeAddress(startMacRow, endMacRow, 1, 1));
  631. currentRow++;
  632. }
  633. //合并单元格
  634. sheet.AddMergedRegion(new CellRangeAddress(2, 2, 0, 2));
  635. sheet.AddMergedRegion(new CellRangeAddress(2, 2, 3, 5));
  636. foreach (var row in models)
  637. {
  638. IRow bodyRow = sheet.CreateRow(currentRow);
  639. currentCol = 0;
  640. #region 汇总行
  641. #endregion
  642. foreach (var col in clorow)
  643. {
  644. ICell cell = bodyRow.CreateCell(currentCol);
  645. var value = row.GetValue(col);
  646. if (value != null)
  647. {
  648. try
  649. {
  650. switch (typeof(MachineEfDto).GetProperty(col).PropertyType.FullName)
  651. {
  652. case "System.DateTime":
  653. var vDate = Convert.ToDateTime(value);
  654. cell.SetCellValue(vDate.ToString("yyyy-MM-dd HH:mm:ss"));
  655. cell.CellStyle = style;
  656. // dataRow.CreateCell(piIndex, CellType.Formula).SetCellValue(vInt);
  657. break;
  658. case "System.Double":
  659. case "System.Demical":
  660. var vDouble = Convert.ToDouble(value);
  661. cell.SetCellValue(vDouble);
  662. cell.CellStyle = style;
  663. break;
  664. case "System.Int32":
  665. case "System.Int64":
  666. var vInt = Convert.ToInt64(value);
  667. cell.SetCellValue(vInt);
  668. cell.CellStyle = style;
  669. break;
  670. case "System.Boolean":
  671. var vBool = Convert.ToBoolean(value);
  672. cell.SetCellValue(vBool);
  673. cell.CellStyle = style;
  674. break;
  675. default:
  676. var Value = value.ToString();
  677. if (value.ToString().Contains("%"))
  678. {
  679. cell.SetCellValue(Convert.ToDouble(value.ToString().Replace("%", "")) / 100);
  680. cell.CellStyle = Pstyle;
  681. }
  682. else
  683. {
  684. cell.SetCellValue(value.ToString());
  685. cell.CellStyle = style;
  686. }
  687. break;
  688. }
  689. }
  690. catch
  691. {
  692. cell.SetCellValue("");
  693. cell.CellStyle = style;
  694. }
  695. }
  696. bodyRow.Cells.Add(cell);
  697. currentCol++;
  698. }
  699. regions.Add(new CellRangeAddress(startMacRow, endMacRow, 1, 1));
  700. currentRow++;
  701. }
  702. MemoryStream ms = new MemoryStream();
  703. workbook.Write(ms);
  704. var buffer = ms.ToArray();
  705. return buffer;
  706. }
  707. catch (Exception ex)
  708. {
  709. return null;
  710. }
  711. }
  712. /// <summary>
  713. /// 创建单元格样式
  714. /// </summary>
  715. /// <param name="workbook"></param>
  716. /// <param name="fillColor"></param>
  717. /// <param name="fontSize"></param>
  718. /// <param name="bold"></param>
  719. /// <returns></returns>
  720. private static ICellStyle CreateStyle(IWorkbook workbook, short fillColor = HSSFColor.White.Index, int fontSize = 10, bool bold = false,string type="")
  721. {
  722. ICellStyle style = workbook.CreateCellStyle();
  723. style.BorderBottom = BorderStyle.Thin;
  724. style.BorderLeft = BorderStyle.Thin;
  725. style.BorderRight = BorderStyle.Thin;
  726. style.BorderTop = BorderStyle.Thin;
  727. //设置单元格的样式:水平对齐居中
  728. style.Alignment = HorizontalAlignment.Center;
  729. //垂直居中
  730. style.VerticalAlignment = VerticalAlignment.Center;
  731. style.WrapText = true;
  732. style.FillForegroundColor = fillColor;
  733. style.FillPattern = FillPattern.SolidForeground;
  734. //新建一个字体样式对象
  735. IFont font = workbook.CreateFont();
  736. //设置字体加粗样式
  737. // font.Boldweight = short.MaxValue;
  738. font.FontHeightInPoints = fontSize;
  739. font.IsBold = bold;
  740. style.SetFont(font);
  741. if (type=="0")
  742. {
  743. style.DataFormat = HSSFDataFormat.GetBuiltinFormat("0.00%");
  744. }
  745. if (type == "1")
  746. {
  747. IDataFormat dataFormatCustom = workbook.CreateDataFormat();
  748. style.DataFormat = dataFormatCustom.GetFormat("yyyy年MM-dd HH:mm:ss");
  749. }
  750. return style;
  751. }
  752. /// <summary>
  753. /// 报表访问导出
  754. /// </summary>
  755. /// <param name="dt"></param>
  756. /// <returns></returns>
  757. public static IWorkbook ExportVisit(DataTable dt)
  758. {
  759. IWorkbook workbook = new HSSFWorkbook();
  760. ISheet sheet = workbook.CreateSheet("Sheet1");
  761. //IRow headerRow = sheet.CreateRow(0);
  762. int rowIndex = 1, piIndex = 0, count = dt.Columns.Count;
  763. var headerCellStyle = CreateStyle(workbook, fontSize: 11, bold: true);
  764. var cellStyle = CreateStyle(workbook, fontSize: 11);
  765. for (int i = 0; i < count; i++)
  766. {
  767. IRow headerRow = sheet.CreateRow(i);
  768. var headerCell = headerRow.CreateCell(0);
  769. headerCell.SetCellValue(dt.Columns[i].ColumnName);
  770. headerCell.CellStyle = headerCellStyle;
  771. var Cell = headerRow.CreateCell(1);
  772. var value = dt.Rows[0].ItemArray[i];
  773. if (i>0)
  774. {
  775. Cell.SetCellValue(Convert.ToDouble(value));
  776. }
  777. else
  778. {
  779. Cell.SetCellValue(value.ToString());
  780. }
  781. Cell.CellStyle = cellStyle;
  782. }
  783. return workbook;
  784. }
  785. public static IWorkbook EntityToExcel(List<Staff> staffs, List<string> list)
  786. {
  787. IWorkbook workbook = new HSSFWorkbook();
  788. ISheet sheet = workbook.CreateSheet("Sheet1");
  789. //IRow headerRow = sheet.CreateRow(0);
  790. int rowIndex = 1, piIndex = 0, count = list.Count;
  791. var headerCellStyle = CreateStyle(workbook, fontSize: 11, bold: true);
  792. var cellStyle = CreateStyle(workbook, fontSize: 11);
  793. IRow headerRow = sheet.CreateRow(0);
  794. foreach (var item in list)
  795. {
  796. var headerCell = headerRow.CreateCell(piIndex);
  797. headerCell.SetCellValue(item);
  798. headerCell.CellStyle = headerCellStyle;
  799. sheet.SetColumnWidth(piIndex, 800 * item.Length + 700);
  800. piIndex++;
  801. }
  802. foreach (var item in staffs)
  803. {
  804. IRow rows = sheet.CreateRow(rowIndex);
  805. for (int i = 0; i < count; i++)
  806. {
  807. var Cell = rows.CreateCell(i);
  808. switch (i+1)
  809. {
  810. case 1:
  811. Cell.SetCellValue(item.FCode);
  812. break;
  813. case 2:
  814. Cell.SetCellValue(item.FName);
  815. break;
  816. case 3:
  817. Cell.SetCellValue(item.RecTime);
  818. break;
  819. case 4:
  820. Cell.SetCellValue(item.RoleNames);
  821. break;
  822. case 5:
  823. Cell.SetCellValue(item.FStatus > 0 ? "正常" : "异常");
  824. break;
  825. case 6:
  826. Cell.SetCellValue(item.Remark);
  827. break;
  828. case 7:
  829. break;
  830. default:
  831. break;
  832. }
  833. Cell.CellStyle = cellStyle;
  834. }
  835. rowIndex++;
  836. }
  837. return workbook;
  838. }
  839. /// <summary>
  840. /// 导出Excel
  841. /// </summary>
  842. /// <typeparam name="T"></typeparam>
  843. /// <param name="datas"></param>
  844. /// <returns></returns>
  845. public static IWorkbook EntityToExcel<T>(List<T> datas) where T : class, new()
  846. {
  847. IWorkbook workbook = new XSSFWorkbook();
  848. ISheet sheet = workbook.CreateSheet("Sheet1");
  849. /*ICellStyle cellStylePercent = workbook.CreateCellStyle();
  850. StylePercent
  851. ICellStyle cellStylePercent = workbook.CreateCellStyle();
  852. cellStylePercent.DataFormat = HSSFDataFormat.GetBuiltinFormat("0.00%");
  853. ICellStyle cellStyleDouble = workbook.CreateCellStyle();
  854. cellStyleDouble.DataFormat = HSSFDataFormat.GetBuiltinFormat("0.000");*/
  855. IRow headerRow = sheet.CreateRow(0);
  856. int rowIndex = 1, piIndex = 0;
  857. Type type = typeof(T);
  858. PropertyInfo[] pis = type.GetProperties();
  859. int pisLen = pis.Length;
  860. PropertyInfo pi = null;
  861. string displayName = string.Empty;
  862. var headerCellStyle = CreateStyle(workbook, fontSize: 11, bold: true);
  863. while (piIndex < pisLen)
  864. {
  865. pi = pis[piIndex];
  866. var pName = pi.GetCustomAttribute<DescriptionAttribute>();
  867. displayName = pName?.Description ?? string.Empty;
  868. if (!displayName.Equals(string.Empty))
  869. {
  870. try
  871. {
  872. var headerCell = headerRow.CreateCell(piIndex);
  873. headerCell.CellStyle = headerCellStyle;
  874. headerCell.SetCellValue(displayName);
  875. // headerRow.CreateCell(piIndex).SetCellValue(displayName);
  876. }
  877. catch (Exception)
  878. {
  879. var headerCell = headerRow.CreateCell(piIndex);
  880. headerCell.SetCellValue("");
  881. }
  882. }
  883. sheet.SetColumnWidth(piIndex, 800 * displayName.Length + 700);
  884. piIndex++;
  885. }
  886. var cellStyle = CreateStyle(workbook, fontSize: 11);
  887. var PercentStyle = CreateStyle(workbook, fontSize: 11,type: "0");
  888. var DatetimeStyle = CreateStyle(workbook, fontSize: 11, type: "1");
  889. foreach (T data in datas)
  890. {
  891. piIndex = 0;
  892. IRow dataRow = sheet.CreateRow(rowIndex);
  893. while (piIndex < pisLen)
  894. {
  895. pi = pis[piIndex];
  896. var value = pi.GetValue(data, null);
  897. ICell dataCell = null;
  898. if (value != null)
  899. {
  900. try
  901. {
  902. switch (pi.PropertyType.FullName)
  903. {
  904. case "System.DateTime":
  905. /* case "System.Nullable":*/
  906. var vDate = Convert.ToDateTime(value);
  907. dataCell = dataRow.CreateCell(piIndex);
  908. dataCell.SetCellValue(vDate.ToString("yyyy-MM-dd HH:mm:ss"));
  909. sheet.SetColumnWidth(piIndex, 5000);
  910. // dataRow.CreateCell(piIndex, CellType.Formula).SetCellValue(vInt);
  911. dataCell.CellStyle = cellStyle;
  912. break;
  913. case "System.Double":
  914. case "System.Demical":
  915. var vDouble = Convert.ToDouble(value);
  916. dataCell = dataRow.CreateCell(piIndex, CellType.Numeric);
  917. dataCell.SetCellValue(vDouble);
  918. dataCell.CellStyle = cellStyle;
  919. break;
  920. case "System.Int32":
  921. case "System.Int64":
  922. var vInt = Convert.ToInt64(value);
  923. dataCell = dataRow.CreateCell(piIndex, CellType.Numeric);
  924. dataCell.SetCellValue(vInt);
  925. dataCell.CellStyle = cellStyle;
  926. break;
  927. case "System.Boolean":
  928. var vBool = Convert.ToBoolean(value);
  929. dataCell = dataRow.CreateCell(piIndex, CellType.Boolean);
  930. dataCell.SetCellValue(vBool);
  931. dataCell.CellStyle = cellStyle;
  932. break;
  933. default:
  934. if (value.ToString().Contains("%"))
  935. {
  936. dataCell = dataRow.CreateCell(piIndex,CellType.Numeric);
  937. dataCell.SetCellValue(Convert.ToDouble(value.ToString().Replace("%", "")) / 100);
  938. dataCell.CellStyle = PercentStyle;
  939. }
  940. else
  941. {
  942. dataCell = dataRow.CreateCell(piIndex, CellType.String);
  943. dataCell.SetCellValue(value.ToString());
  944. dataCell.CellStyle = cellStyle;
  945. }
  946. break;
  947. }
  948. }
  949. catch
  950. {
  951. dataCell = dataRow.CreateCell(piIndex);
  952. dataCell.SetCellValue("");
  953. dataCell.CellStyle = cellStyle;
  954. }
  955. }
  956. else
  957. {
  958. dataCell = dataRow.CreateCell(piIndex);
  959. dataCell.SetCellValue("");
  960. dataCell.CellStyle = cellStyle;
  961. }
  962. piIndex++;
  963. }
  964. rowIndex++;
  965. }
  966. return workbook;
  967. }
  968. public static IWorkbook MtbaToExcel(AntdComplexTableDto roles,int plens)
  969. {
  970. IWorkbook workbook = new XSSFWorkbook();
  971. ISheet sheet = workbook.CreateSheet("Sheet1");
  972. IRow headerRow = sheet.CreateRow(0);
  973. IRow header = sheet.CreateRow(1);
  974. var headerCellStyle = CreateStyle(workbook, fontSize: 11, bold: true);
  975. var total = roles.TotalData.Rows;
  976. var Columns = roles.Columns.ToList();
  977. for (int i = 0; i < Columns.Count; i++)
  978. {
  979. if (!string.IsNullOrEmpty(Columns[i].ColName))
  980. {
  981. var name = Columns[i].ColName;
  982. var headerCell = headerRow.CreateCell(i);
  983. headerCell.CellStyle = headerCellStyle;
  984. headerCell.SetCellValue(name);
  985. }
  986. if (i == 0)
  987. {
  988. var headerCell = header.CreateCell(i);
  989. headerCell.CellStyle = headerCellStyle;
  990. headerCell.SetCellValue("汇总");
  991. }
  992. else if (i > 8)
  993. {
  994. var headerCell = header.CreateCell(i);
  995. headerCell.CellStyle = headerCellStyle;
  996. headerCell.SetCellValue(Convert.ToDouble(total[0][i - 9]));
  997. }
  998. else
  999. {
  1000. var headerCell = header.CreateCell(i);
  1001. headerCell.CellStyle = headerCellStyle;
  1002. headerCell.SetCellValue("");
  1003. }
  1004. sheet.SetColumnWidth(i, 800 * Columns[i].ColName.Length + 700);
  1005. }
  1006. sheet.AddMergedRegion(new CellRangeAddress(1, 1, 0, 8));
  1007. var cellStyle = CreateStyle(workbook, fontSize: 11);
  1008. int index = 0, rowIndex = 2;
  1009. while (index < plens)
  1010. {
  1011. IRow dataRow = sheet.CreateRow(rowIndex);
  1012. for (int i = 0; i < Columns.Count; i++)
  1013. {
  1014. ICell dataCell = null;
  1015. var value = roles.Datas.Rows[index][$@"{Columns[i].ColName}"];
  1016. if (i < 7)
  1017. {
  1018. dataCell = dataRow.CreateCell(i,CellType.String);
  1019. dataCell.SetCellValue(value.ToString());
  1020. }
  1021. else
  1022. {
  1023. var vDouble = Convert.ToDouble(value);
  1024. dataCell = dataRow.CreateCell(i,CellType.Numeric);
  1025. dataCell.SetCellValue(vDouble);
  1026. }
  1027. dataCell.CellStyle = cellStyle;
  1028. }
  1029. index++;
  1030. rowIndex++;
  1031. }
  1032. return workbook;
  1033. }
  1034. /// <summary>
  1035. /// 表头合并的报表导出
  1036. /// </summary>
  1037. /// <typeparam name="T"></typeparam>
  1038. /// <param name="datas">数据源</param>
  1039. /// <param name="list">表头</param>
  1040. /// <param name="key">需要合并位置</param>
  1041. /// <returns></returns>
  1042. public static IWorkbook EntityToExcel<T>(List<T> datas,List<string> list,List<int> key) where T : class, new()
  1043. {
  1044. IWorkbook workbook = new XSSFWorkbook();
  1045. ISheet sheet = workbook.CreateSheet("Sheet1");
  1046. IRow header = sheet.CreateRow(0);
  1047. IRow headerRow= sheet.CreateRow(1);
  1048. /*IDataFormat dataFormat = workbook.CreateDataFormat();
  1049. ICellStyle cellStylePercent = workbook.CreateCellStyle();
  1050. cellStylePercent.DataFormat = dataFormat.GetFormat("0.00%");*/
  1051. int rowIndex = 2, piIndex = 0;
  1052. Type type = typeof(T);
  1053. PropertyInfo[] pis = type.GetProperties();
  1054. int pisLen = pis.Length;
  1055. PropertyInfo pi = null;
  1056. string displayName = string.Empty;
  1057. var headerCellStyle = CreateStyle(workbook, fontSize: 11, bold: true);
  1058. int sum = 0;
  1059. while (piIndex < pisLen)
  1060. {
  1061. pi = pis[piIndex];
  1062. var pName = pi.GetCustomAttribute<DescriptionAttribute>();
  1063. displayName = pName?.Description ?? string.Empty;
  1064. if (list.Count > piIndex && !string.IsNullOrEmpty(list[piIndex]))
  1065. {
  1066. var headerCell = header.CreateCell(piIndex);
  1067. headerCell.CellStyle = headerCellStyle;
  1068. headerCell.SetCellValue(list[piIndex]);
  1069. }
  1070. else
  1071. {
  1072. var headerCell = header.CreateCell(piIndex);
  1073. headerCell.CellStyle = headerCellStyle;
  1074. headerCell.SetCellValue("");
  1075. }
  1076. if (!displayName.Equals(string.Empty))
  1077. {
  1078. try
  1079. {
  1080. var headerCell = headerRow.CreateCell(piIndex);
  1081. headerCell.CellStyle = headerCellStyle;
  1082. headerCell.SetCellValue(displayName);
  1083. // headerRow.CreateCell(piIndex).SetCellValue(displayName);
  1084. }
  1085. catch (Exception)
  1086. {
  1087. var headerCell = headerRow.CreateCell(piIndex);
  1088. headerCell.CellStyle = CreateStyle(workbook);
  1089. headerCell.SetCellValue("");
  1090. }
  1091. }
  1092. sum = sum > displayName.Length ? sum : displayName.Length;
  1093. sheet.SetColumnWidth(piIndex, 800 * displayName.Length + 700);
  1094. piIndex++;
  1095. }
  1096. int index = 0;
  1097. int i = 0;
  1098. while (i < pisLen)
  1099. {
  1100. if (index<key.Count&&i == key[index]){
  1101. sheet.AddMergedRegion(new CellRangeAddress(0, 0, i, key[index + 1]));
  1102. i = key[index + 1];
  1103. index += 2;
  1104. }
  1105. else
  1106. {
  1107. sheet.AddMergedRegion(new CellRangeAddress(0, 1, i, i));
  1108. }
  1109. i++;
  1110. }
  1111. var cellStyle = CreateStyle(workbook, fontSize: 11);
  1112. var PercentStyle = CreateStyle(workbook, fontSize: 11,type:"0");
  1113. var DatetimeStyle = CreateStyle(workbook, fontSize: 11, type: "1");
  1114. foreach (T data in datas)
  1115. {
  1116. piIndex = 0;
  1117. IRow dataRow = sheet.CreateRow(rowIndex);
  1118. while (piIndex < pisLen)
  1119. {
  1120. pi = pis[piIndex];
  1121. var value = pi.GetValue(data, null);
  1122. ICell dataCell = null;
  1123. if (value != null)
  1124. {
  1125. try
  1126. {
  1127. switch (pi.PropertyType.FullName)
  1128. {
  1129. case "System.DateTime":
  1130. var vDate = Convert.ToDateTime(value);
  1131. dataCell = dataRow.CreateCell(piIndex);
  1132. dataCell.SetCellValue(vDate.ToString("yyyy-MM-dd HH:mm:ss"));
  1133. sheet.SetColumnWidth(piIndex, 5000);
  1134. // dataRow.CreateCell(piIndex).SetCellValue(vInt);
  1135. dataCell.CellStyle = DatetimeStyle;
  1136. break;
  1137. case "System.Double":
  1138. case "System.Demical":
  1139. var vDouble = Convert.ToDouble(value);
  1140. dataCell = dataRow.CreateCell(piIndex, CellType.Numeric);
  1141. dataCell.SetCellValue(vDouble);
  1142. dataCell.CellStyle = cellStyle;
  1143. break;
  1144. case "System.Int32":
  1145. case "System.Int64":
  1146. var vInt = Convert.ToInt64(value);
  1147. dataCell = dataRow.CreateCell(piIndex, CellType.Numeric);
  1148. dataCell.SetCellValue(vInt);
  1149. dataCell.CellStyle = cellStyle;
  1150. break;
  1151. case "System.Boolean":
  1152. var vBool = Convert.ToBoolean(value);
  1153. dataCell = dataRow.CreateCell(piIndex, CellType.Boolean);
  1154. dataCell.SetCellValue(vBool);
  1155. dataCell.CellStyle = cellStyle;
  1156. break;
  1157. default:
  1158. /*dataCell = dataRow.CreateCell(piIndex, CellType.Boolean);
  1159. dataCell.SetCellValue(value.ToString());*/
  1160. var number = value.ToString();
  1161. if (number.Contains("%"))
  1162. {
  1163. dataCell = dataRow.CreateCell(piIndex,CellType.Numeric);
  1164. dataCell.SetCellValue(Convert.ToDouble(number.Replace("%", "")) / 100);
  1165. /*dataCell.CellStyle = cellStylePercent;*/
  1166. dataCell.CellStyle = PercentStyle;
  1167. }
  1168. else
  1169. {
  1170. dataCell = dataRow.CreateCell(piIndex, CellType.String);
  1171. dataCell.SetCellValue(number);
  1172. dataCell.CellStyle = cellStyle;
  1173. }
  1174. /* sheet.SetColumnWidth(piIndex, sheet.GetColumnWidth(piIndex) / 256> Encoding.Default.GetBytes(value.ToString()).Length + 1? sheet.GetColumnWidth(piIndex): (Encoding.Default.GetBytes(value.ToString()).Length + 1)*256);
  1175. */
  1176. break;
  1177. }
  1178. }
  1179. catch
  1180. {
  1181. dataCell = dataRow.CreateCell(piIndex);
  1182. dataCell.SetCellValue("");
  1183. dataCell.CellStyle = cellStyle;
  1184. }
  1185. }
  1186. else
  1187. {
  1188. dataCell = dataRow.CreateCell(piIndex);
  1189. dataCell.SetCellValue("");
  1190. dataCell.CellStyle = cellStyle;
  1191. }
  1192. piIndex++;
  1193. }
  1194. rowIndex++;
  1195. }
  1196. return workbook;
  1197. }
  1198. /// <summary>
  1199. /// 导出实体集合到指定的文件路径
  1200. /// </summary>
  1201. /// <typeparam name="T"></typeparam>
  1202. /// <param name="filePath"></param>
  1203. /// <param name="datas"></param>
  1204. /// <returns></returns>
  1205. public static Task EntityExportToFile<T>(string filePath, List<T> datas) where T : class, new()
  1206. {
  1207. var workbook = EntityToExcel(datas);
  1208. var dic = System.IO.Path.GetDirectoryName(filePath);
  1209. if (!System.IO.Directory.Exists(dic))
  1210. {
  1211. System.IO.Directory.CreateDirectory(dic);
  1212. }
  1213. if (System.IO.File.Exists(filePath))
  1214. {
  1215. System.IO.File.Delete(filePath);
  1216. }
  1217. using (FileStream ms = new FileStream(filePath, FileMode.OpenOrCreate))
  1218. {
  1219. workbook.Write(ms);
  1220. }
  1221. return Task.CompletedTask;
  1222. }
  1223. /// <summary>
  1224. /// 导出
  1225. /// </summary>
  1226. /// <param name="schema">导出数据结构</param>
  1227. /// <param name="dataTable">导出数据源</param>
  1228. /// <param name="errorinfo">错误信息</param>
  1229. /// <returns></returns>
  1230. public static byte[] TableToExcel2(StSchema schema, DataTable dataTable, ref string errorinfo)
  1231. {
  1232. if (dataTable == null || dataTable.Rows.Count <= 0)
  1233. {
  1234. errorinfo = "待导出数据为空";
  1235. return null;
  1236. }
  1237. var workbook = new XSSFWorkbook();
  1238. var sheet = workbook.CreateSheet("sheet1");
  1239. var currentRow = 0;
  1240. var currentCol = 0;
  1241. var baseHeaderWidth = 400;
  1242. var baseRowWidth = 500;
  1243. var headerMargin = 900;
  1244. var rowMargin = 400;
  1245. ICellStyle style = workbook.CreateCellStyle();
  1246. style.BorderBottom = BorderStyle.Thin;
  1247. style.BorderLeft = BorderStyle.Thin;
  1248. style.BorderRight = BorderStyle.Thin;
  1249. style.BorderTop = BorderStyle.Thin;
  1250. //设置单元格的样式:水平对齐居中
  1251. style.Alignment = HorizontalAlignment.Center;
  1252. //垂直居中
  1253. style.VerticalAlignment = VerticalAlignment.Center;
  1254. //新建一个字体样式对象
  1255. IFont font = workbook.CreateFont();
  1256. //设置字体加粗样式
  1257. // font.Boldweight = short.MaxValue;
  1258. font.FontHeightInPoints = 12;
  1259. //使用SetFont方法将字体样式添加到单元格样式中
  1260. style.SetFont(font);
  1261. var headerRow = sheet.CreateRow(currentRow);
  1262. foreach (var item in schema.Items)
  1263. {
  1264. ICell headerCell = headerRow.CreateCell(currentCol);
  1265. headerCell.SetCellValue(item.Title);
  1266. sheet.SetColumnWidth(currentCol, baseHeaderWidth * item.Title.Length + headerMargin);
  1267. headerCell.CellStyle = style;
  1268. currentCol++;
  1269. }
  1270. currentRow++;
  1271. int no = 1;
  1272. foreach (DataRow item in dataTable.Rows)
  1273. {
  1274. currentCol = 0;
  1275. IRow dataRow = sheet.CreateRow(currentRow);
  1276. foreach (var title in schema.Items)
  1277. {
  1278. var currHeaderWidth = baseHeaderWidth * title.Title.Length + headerMargin;
  1279. ICell cell = dataRow.CreateCell(currentCol);
  1280. if (currentCol == 0)
  1281. {
  1282. cell.SetCellValue(no);
  1283. var width = baseRowWidth * no.ToString().Length + rowMargin;
  1284. if (width > currHeaderWidth)
  1285. {
  1286. sheet.SetColumnWidth(currentCol, width);
  1287. }
  1288. }
  1289. else
  1290. {
  1291. // 防止前端传回的json值datatable中没有对应的字段与之匹配
  1292. if (string.IsNullOrEmpty(title.Index))
  1293. {
  1294. cell.SetCellValue(string.Empty);
  1295. }
  1296. else
  1297. {
  1298. cell.SetCellValue(item[title.Index].ToString());
  1299. var width = baseRowWidth * (item[title.Index].ToString().Length) + rowMargin;
  1300. if (width > currHeaderWidth)
  1301. {
  1302. sheet.SetColumnWidth(currentCol, width);
  1303. }
  1304. }
  1305. }
  1306. cell.CellStyle = style;
  1307. currentCol++;
  1308. }
  1309. currentRow++;
  1310. no++;
  1311. }
  1312. MemoryStream ms = new MemoryStream();
  1313. workbook.Write(ms);
  1314. var buffer = ms.ToArray();
  1315. return buffer;
  1316. }
  1317. /// <summary>
  1318. /// 导出实体
  1319. /// </summary>
  1320. /// <param name="schema">导出数据结构</param>
  1321. /// <param name="dataTable">导出数据源</param>
  1322. /// <param name="errorinfo">错误信息</param>
  1323. /// <returns></returns>
  1324. public static byte[] EntityToExcel<T>(StSchema schema, IEnumerable<T> entities, ref string errorinfo)
  1325. {
  1326. if (entities == null || entities.Count() <= 0)
  1327. {
  1328. errorinfo = "待导出数据为空";
  1329. return null;
  1330. }
  1331. var workbook = new XSSFWorkbook();
  1332. var startRow = 1;
  1333. var startCol = 1;
  1334. var sheet = workbook.CreateSheet("sheet1");
  1335. var currentRow = startRow;
  1336. var currentCol = startCol;
  1337. var baseHeaderWidth = 400;
  1338. var baseRowWidth = 500;
  1339. var headerMargin = 900;
  1340. var rowMargin = 400;
  1341. ICellStyle style = workbook.CreateCellStyle();
  1342. style.BorderBottom = BorderStyle.Thin;
  1343. style.BorderLeft = BorderStyle.Thin;
  1344. style.BorderRight = BorderStyle.Thin;
  1345. style.BorderTop = BorderStyle.Thin;
  1346. //设置单元格的样式:水平对齐居中
  1347. style.Alignment = HorizontalAlignment.Center;
  1348. //垂直居中
  1349. style.VerticalAlignment = VerticalAlignment.Center;
  1350. //新建一个字体样式对象
  1351. IFont font = workbook.CreateFont();
  1352. //设置字体加粗样式
  1353. // font.Boldweight = short.MaxValue;
  1354. font.FontHeightInPoints = 12;
  1355. //使用SetFont方法将字体样式添加到单元格样式中
  1356. style.SetFont(font);
  1357. var headerRow = sheet.CreateRow(currentRow);
  1358. foreach (var item in schema.Items)
  1359. {
  1360. ICell headerCell = headerRow.CreateCell(currentCol);
  1361. headerCell.SetCellValue(item.Title);
  1362. sheet.SetColumnWidth(currentCol, baseHeaderWidth * item.Title.Length + headerMargin);
  1363. headerCell.CellStyle = style;
  1364. currentCol++;
  1365. }
  1366. currentRow++;
  1367. int no = 1;
  1368. var type = typeof(T);
  1369. var properties = type.GetProperties();
  1370. foreach (T item in entities)
  1371. {
  1372. currentCol = startCol;
  1373. IRow dataRow = sheet.CreateRow(currentRow);
  1374. foreach (var title in schema.Items)
  1375. {
  1376. var currHeaderWidth = baseHeaderWidth * title.Title.Length + headerMargin;
  1377. ICell cell = dataRow.CreateCell(currentCol);
  1378. if (currentCol == startCol)
  1379. {
  1380. cell.SetCellValue(no);
  1381. var width = baseRowWidth * no.ToString().Length + rowMargin;
  1382. if (width > currHeaderWidth)
  1383. {
  1384. sheet.SetColumnWidth(currentCol, width);
  1385. }
  1386. }
  1387. else
  1388. {
  1389. // 防止前端传回的json值datatable中没有对应的字段与之匹配
  1390. if (string.IsNullOrEmpty(title.Index))
  1391. {
  1392. cell.SetCellValue(string.Empty);
  1393. }
  1394. else
  1395. {
  1396. var property = properties.FirstOrDefault(c => c.Name.ToLower() == title.Index.ToLower());
  1397. var value = property.GetValue(item);
  1398. if (value != null)
  1399. {
  1400. var typeName = value.GetType().FullName;
  1401. switch (typeName)
  1402. {
  1403. case "System.Int32": cell.SetCellValue((int)value); break;
  1404. case "System.Decimal":
  1405. case "System.Double":
  1406. cell.SetCellValue((double)value); break;
  1407. case "System.DateTime":
  1408. cell.SetCellValue(((DateTime)value).ToString("yyyy-MM-dd HH:mm:ss"));
  1409. break;
  1410. default:
  1411. cell.SetCellValue(value.ToString()); break;
  1412. }
  1413. var width = baseRowWidth * (value.ToString().Length) + rowMargin;
  1414. if (width > currHeaderWidth)
  1415. {
  1416. sheet.SetColumnWidth(currentCol, width);
  1417. }
  1418. }
  1419. }
  1420. }
  1421. cell.CellStyle = style;
  1422. currentCol++;
  1423. }
  1424. currentRow++;
  1425. no++;
  1426. }
  1427. if (schema.ShowFooter)
  1428. {
  1429. var sumRow = schema.SumRow;
  1430. var footRow = sheet.CreateRow(currentRow);
  1431. var nameCell = footRow.CreateCell(startCol);
  1432. nameCell.SetCellValue(sumRow.RowName);
  1433. nameCell.CellStyle = style;
  1434. var clos = sumRow.ColSpan > 2 ? sumRow.ColSpan : 3;
  1435. for (var i = 1; i < clos; i++)
  1436. {
  1437. var emptyCell = footRow.CreateCell(startCol + i);
  1438. emptyCell.SetCellValue("");
  1439. emptyCell.CellStyle = style;
  1440. }
  1441. sheet.AddMergedRegion(new CellRangeAddress(currentRow, currentRow, startCol, startCol +clos - 2));
  1442. var colIndex = clos-1;
  1443. foreach (var item in sumRow.RowValues)
  1444. {
  1445. var cell = footRow.CreateCell(colIndex + startCol);
  1446. cell.SetCellValue(item);
  1447. cell.CellStyle = style;
  1448. colIndex++;
  1449. }
  1450. }
  1451. MemoryStream ms = new MemoryStream();
  1452. workbook.Write(ms);
  1453. var buffer = ms.ToArray();
  1454. return buffer;
  1455. }
  1456. private void SetCellStyle(IWorkbook workbook, ICell cell)
  1457. {
  1458. ICellStyle style = workbook.CreateCellStyle();
  1459. style.BorderBottom = BorderStyle.Thin;
  1460. style.BorderLeft = BorderStyle.Thin;
  1461. style.BorderRight = BorderStyle.Thin;
  1462. style.BorderTop = BorderStyle.Thin;
  1463. //设置单元格的样式:水平对齐居中
  1464. style.Alignment = HorizontalAlignment.Center;
  1465. //垂直居中
  1466. style.VerticalAlignment = VerticalAlignment.Center;
  1467. //新建一个字体样式对象
  1468. IFont font = workbook.CreateFont();
  1469. //设置字体加粗样式
  1470. // font.Boldweight = short.MaxValue;
  1471. font.FontHeightInPoints = 12;
  1472. //使用SetFont方法将字体样式添加到单元格样式中
  1473. style.SetFont(font);
  1474. cell.CellStyle = style;
  1475. }
  1476. }
  1477. }