ExportExcelHelper.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854
  1. using Ant.Data;
  2. using Ant.ORM;
  3. using MES.Production.Entity;
  4. using NPOI.HSSF.UserModel;
  5. using NPOI.SS.UserModel;
  6. using NPOI.SS.Util;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Drawing;
  10. using System.IO;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Web;
  14. using ZXing;
  15. using ZXing.QrCode;
  16. namespace ChangFa.Machinery.WebPage.App_Start
  17. {
  18. public class ExportExcelHelper
  19. {
  20. public static MemoryStream ExportSchoolExcel(dynamic schoolInfo, List<dynamic> sporterInfo)
  21. {
  22. HSSFWorkbook book = new HSSFWorkbook();
  23. #region 样式定义
  24. HSSFCellStyle titleStyle = (HSSFCellStyle)book.CreateCellStyle();
  25. titleStyle.Alignment = HorizontalAlignment.CENTER;
  26. titleStyle.VerticalAlignment = VerticalAlignment.CENTER;
  27. HSSFFont titlefont = (HSSFFont)book.CreateFont();
  28. titlefont.FontName = "黑体";
  29. titlefont.FontHeightInPoints = (short)12;
  30. titleStyle.SetFont(titlefont);
  31. HSSFCellStyle contentStyle = (HSSFCellStyle)book.CreateCellStyle();
  32. contentStyle.Alignment = HorizontalAlignment.CENTER;
  33. contentStyle.VerticalAlignment = VerticalAlignment.CENTER;
  34. contentStyle.BorderBottom = BorderStyle.THIN;
  35. contentStyle.BorderLeft = BorderStyle.THIN;
  36. contentStyle.BorderRight = BorderStyle.THIN;
  37. contentStyle.BorderTop = BorderStyle.THIN;
  38. HSSFFont contentfont = (HSSFFont)book.CreateFont();
  39. contentfont.FontName = "黑体";
  40. contentfont.FontHeightInPoints = (short)10;
  41. contentStyle.SetFont(contentfont);
  42. #endregion
  43. #region 运动员参赛
  44. ISheet sheet1 = book.CreateSheet("运动员参赛");
  45. //sheet1.SetColumnWidth(0, 20 * 256);
  46. //sheet1.SetColumnWidth(1, 20 * 256);
  47. //sheet1.SetColumnWidth(2, 20 * 256);
  48. //sheet1.SetColumnWidth(3, 20 * 256);
  49. //sheet1.SetColumnWidth(4, 20 * 256);
  50. //sheet1.SetColumnWidth(5, 20 * 256);
  51. //sheet1.SetColumnWidth(6, 20 * 256);
  52. //sheet1.SetColumnWidth(7, 20 * 256);
  53. //sheet1.SetColumnWidth(8, 20 * 256);
  54. //sheet1.SetColumnWidth(9, 20 * 256);
  55. //sheet1.SetColumnWidth(10, 20 * 256);
  56. #region 比赛名称
  57. IRow row1 = sheet1.CreateRow(0);
  58. for (int i = 0; i < 11; i++)
  59. {
  60. row1.CreateCell(i);
  61. }
  62. var cellRangeAddress = new CellRangeAddress(0, 0, 0, 10);
  63. sheet1.AddMergedRegion(cellRangeAddress);
  64. var cell = row1.GetCell(0);
  65. cell.CellStyle = titleStyle;
  66. var cellContent = schoolInfo.RaceName;
  67. cell.SetCellValue(cellContent);
  68. #endregion
  69. #region 代表队名单
  70. IRow row2 = sheet1.CreateRow(1);
  71. for (int i = 0; i < 11; i++)
  72. {
  73. row2.CreateCell(i);
  74. }
  75. cellRangeAddress = new CellRangeAddress(1, 1, 0, 10);
  76. sheet1.AddMergedRegion(cellRangeAddress);
  77. cell = row2.GetCell(0);
  78. cell.CellStyle = titleStyle;
  79. cellContent = "代表队名单";
  80. cell.SetCellValue(cellContent);
  81. #endregion
  82. #region 学校名称
  83. IRow row3 = sheet1.CreateRow(2);
  84. for (int i = 0; i < 11; i++)
  85. {
  86. row3.CreateCell(i);
  87. }
  88. cellRangeAddress = new CellRangeAddress(2, 2, 0, 10);
  89. sheet1.AddMergedRegion(cellRangeAddress);
  90. cell = row3.GetCell(0);
  91. cell.CellStyle = titleStyle;
  92. cellContent = schoolInfo.SchoolName;
  93. cell.SetCellValue(cellContent);
  94. #endregion
  95. #region 领队信息
  96. IRow row4 = sheet1.CreateRow(3);
  97. for (int i = 0; i < 11; i++)
  98. {
  99. row4.CreateCell(i);
  100. }
  101. cellRangeAddress = new CellRangeAddress(3, 3, 0, 10);
  102. sheet1.AddMergedRegion(cellRangeAddress);
  103. cell = row4.GetCell(0);
  104. cell.CellStyle = titleStyle;
  105. cellContent = string.Format("领队:{0} 教练员:{1} 联系电话:{2}", schoolInfo.Leader, schoolInfo.Coacher, schoolInfo.Contact);
  106. cell.SetCellValue(cellContent);
  107. #endregion
  108. #region 学生信息
  109. IRow row5 = sheet1.CreateRow(4);
  110. cell = row5.CreateCell(0);
  111. cell.CellStyle = contentStyle;
  112. cell.SetCellValue("注册证号");
  113. cell = row5.CreateCell(1);
  114. cell.CellStyle = contentStyle;
  115. cell.SetCellValue("姓名");
  116. cell = row5.CreateCell(2);
  117. cell.CellStyle = contentStyle;
  118. cell.SetCellValue("性别");
  119. cell = row5.CreateCell(3);
  120. cell.CellStyle = contentStyle;
  121. cell.SetCellValue("学籍号");
  122. cell = row5.CreateCell(4);
  123. cell.CellStyle = contentStyle;
  124. cell.SetCellValue("身份证号");
  125. cell = row5.CreateCell(5);
  126. cell.CellStyle = contentStyle;
  127. cell.SetCellValue("年级");
  128. cell = row5.CreateCell(6);
  129. cell.CellStyle = contentStyle;
  130. cell.SetCellValue("组别");
  131. cell = row5.CreateCell(7);
  132. cell.CellStyle = contentStyle;
  133. cell.SetCellValue("项目1");
  134. cell = row5.CreateCell(8);
  135. cell.CellStyle = contentStyle;
  136. cell.SetCellValue("项目2");
  137. cell = row5.CreateCell(9);
  138. cell.CellStyle = contentStyle;
  139. cell.SetCellValue("项目3");
  140. cell = row5.CreateCell(10);
  141. cell.CellStyle = contentStyle;
  142. cell.SetCellValue("项目4");
  143. var contentRowIndex = 5;
  144. try
  145. {
  146. foreach (dynamic item in sporterInfo)
  147. {
  148. IRow contentRow = sheet1.CreateRow(contentRowIndex);
  149. cell = contentRow.CreateCell(0);
  150. cell.CellStyle = contentStyle;
  151. cell.SetCellValue(item.RaceNo);
  152. //sheet1.AutoSizeColumn(0);
  153. //sheet1.SetColumnWidth(0, sheet1.GetColumnWidth(0) * 11 / 10);
  154. cell = contentRow.CreateCell(1);
  155. cell.CellStyle = contentStyle;
  156. cell.SetCellValue(item.Name);
  157. //sheet1.AutoSizeColumn(1);
  158. //sheet1.SetColumnWidth(1, sheet1.GetColumnWidth(1) * 11 / 10);
  159. cell = contentRow.CreateCell(2);
  160. cell.CellStyle = contentStyle;
  161. cell.SetCellValue(item.Sex);
  162. //sheet1.AutoSizeColumn(2);
  163. //sheet1.SetColumnWidth(2, sheet1.GetColumnWidth(2) * 11 / 10);
  164. cell = contentRow.CreateCell(3);
  165. cell.CellStyle = contentStyle;
  166. cell.SetCellValue(item.RegNo);
  167. //sheet1.AutoSizeColumn(3);
  168. //sheet1.SetColumnWidth(3, sheet1.GetColumnWidth(3) * 11 / 10);
  169. cell = contentRow.CreateCell(4);
  170. cell.CellStyle = contentStyle;
  171. cell.SetCellValue(item.IDCard);
  172. //sheet1.AutoSizeColumn(4);
  173. //sheet1.SetColumnWidth(4, sheet1.GetColumnWidth(4) * 11 / 10);
  174. cell = contentRow.CreateCell(5);
  175. cell.CellStyle = contentStyle;
  176. cell.SetCellValue(item.Grade);
  177. //sheet1.AutoSizeColumn(5);
  178. //sheet1.SetColumnWidth(5, sheet1.GetColumnWidth(5) * 11 / 10);
  179. cell = contentRow.CreateCell(6);
  180. cell.CellStyle = contentStyle;
  181. cell.SetCellValue(item.Group);
  182. //sheet1.AutoSizeColumn(6);
  183. //sheet1.SetColumnWidth(6, sheet1.GetColumnWidth(6) * 11 / 10);
  184. cell = contentRow.CreateCell(7);
  185. cell.CellStyle = contentStyle;
  186. cell.SetCellValue(item.Project1);
  187. //sheet1.AutoSizeColumn(7);
  188. //sheet1.SetColumnWidth(7, sheet1.GetColumnWidth(7) * 11 / 10);
  189. cell = contentRow.CreateCell(8);
  190. cell.CellStyle = contentStyle;
  191. cell.SetCellValue(item.Project2);
  192. cell = contentRow.CreateCell(9);
  193. cell.CellStyle = contentStyle;
  194. cell.SetCellValue(item.Project3);
  195. cell = contentRow.CreateCell(10);
  196. cell.CellStyle = contentStyle;
  197. cell.SetCellValue(item.Project4);
  198. contentRowIndex++;
  199. }
  200. }
  201. catch (Exception ex)
  202. {
  203. throw ex;
  204. }
  205. #endregion
  206. #endregion
  207. #region 运动员参赛卡片
  208. ISheet sheet2 = book.CreateSheet("运动员参赛卡片");
  209. sheet2.FitToPage = false;
  210. sheet2.SetMargin(MarginType.RightMargin, 0.1);
  211. sheet2.SetMargin(MarginType.TopMargin, 0.1);
  212. sheet2.SetMargin(MarginType.LeftMargin, 0.1);
  213. sheet2.SetMargin(MarginType.BottomMargin, 0.1);
  214. sheet2.PrintSetup.PaperSize = (short)PaperSize.A4;
  215. int rowIndex = 1;
  216. foreach (dynamic item in sporterInfo)
  217. {
  218. QrCodeEncodingOptions options = new QrCodeEncodingOptions
  219. {
  220. DisableECI = true,
  221. CharacterSet = "UTF-8",
  222. Width = 84,
  223. Height = 84,
  224. Margin = 0
  225. };
  226. BarcodeWriter writer = new BarcodeWriter();
  227. writer.Format = BarcodeFormat.QR_CODE;
  228. writer.Options = options;
  229. Bitmap QRCodeBitmap = writer.Write(item.QRCode);
  230. Bitmap raceCardBitMap = new Bitmap(500, 250);
  231. Graphics graphics = Graphics.FromImage(raceCardBitMap);
  232. graphics.Clear(Color.White);
  233. Font font1 = new Font("宋体", 10);
  234. Font font2 = new Font("宋体", 12);
  235. Brush brush1 = new System.Drawing.SolidBrush(Color.Black);
  236. graphics.DrawLine(new Pen(Color.Black, 1), new Point(0, 0), new Point(500, 0));
  237. graphics.DrawLine(new Pen(Color.Black, 1), new Point(0, 249), new Point(500, 249));
  238. graphics.DrawLine(new Pen(Color.Black, 1), new Point(0, 0), new Point(0, 250));
  239. graphics.DrawLine(new Pen(Color.Black, 1), new Point(499, 0), new Point(499, 250));
  240. var leftMargin = (Int32)((500 - graphics.MeasureString("***运动员电子参赛证(仅限本次比赛使用)***", font1).Width) / 2);
  241. graphics.DrawString("***运动员电子参赛证(仅限本次比赛使用)***", font1, brush1, new Point(leftMargin > 0 ? leftMargin : 0, 5));
  242. leftMargin = (Int32)((500 - graphics.MeasureString(schoolInfo.RaceName, font1).Width) / 2);
  243. graphics.DrawString(schoolInfo.RaceName, font1, brush1, new Point(leftMargin > 0 ? leftMargin : 0, 30));
  244. graphics.DrawImage(QRCodeBitmap, new Point(20, 70));
  245. graphics.DrawString(item.Name, font2, brush1, new Point(150, 60));
  246. graphics.DrawString(item.SchoolName, font1, brush1, new Point(150, 90));
  247. graphics.DrawString(item.Grade, font1, brush1, new Point(150, 115));
  248. graphics.DrawString(item.Project1 ?? string.Empty, font1, brush1, new Point(150, 140));
  249. graphics.DrawString(item.Project2 ?? string.Empty, font1, brush1, new Point(150, 165));
  250. graphics.DrawString(item.Project3 ?? string.Empty, font1, brush1, new Point(150, 190));
  251. graphics.DrawString(item.Project4 ?? string.Empty, font1, brush1, new Point(150, 215));
  252. MemoryStream bitMapMs = new MemoryStream();
  253. raceCardBitMap.Save(bitMapMs, System.Drawing.Imaging.ImageFormat.Bmp);
  254. byte[] buff = bitMapMs.GetBuffer();
  255. var picIndex = (int)(rowIndex / 2) + ((rowIndex % 2) > 0 ? 1 : 0);
  256. var picSecond = rowIndex % 2 == 0;
  257. IDrawing patriarch = sheet2.CreateDrawingPatriarch();
  258. int pic = book.AddPicture(buff, PictureType.JPEG);
  259. HSSFClientAnchor anchor = null;
  260. if (picSecond)
  261. {
  262. anchor = new HSSFClientAnchor(0, 0, 0, 0, 6, 11 * (picIndex - 1) + 2 * picIndex, 11, 11 * (picIndex - 1) + 2 * picIndex + 11);
  263. }
  264. else
  265. {
  266. anchor = new HSSFClientAnchor(0, 0, 0, 0, 0, 11 * (picIndex - 1) + 2 * picIndex, 5, 11 * (picIndex - 1) + 2 * picIndex + 11);
  267. }
  268. anchor.AnchorType = 2;
  269. patriarch.CreatePicture(anchor, pic);
  270. if (rowIndex % 8 == 0)
  271. {
  272. sheet2.SetRowBreak(anchor.Row2);
  273. }
  274. rowIndex++;
  275. }
  276. #endregion
  277. AutoColumnWidth(sheet1, 7, 4);
  278. MemoryStream ms = new MemoryStream();
  279. book.Write(ms);
  280. ms.Seek(0, SeekOrigin.Begin);
  281. return ms;
  282. }
  283. /// <summary>
  284. ///
  285. /// </summary>
  286. /// <param name="sheet"></param>
  287. /// <param name="cols"></param>
  288. public static void AutoColumnWidth(ISheet sheet, int cols, int rowbBeginIndex)
  289. {
  290. for (int col = 0; col <= cols; col++)
  291. {
  292. int columnWidth = 0;
  293. for (int rowIndex = rowbBeginIndex; rowIndex <= sheet.LastRowNum; rowIndex++)
  294. {
  295. IRow row = sheet.GetRow(rowIndex);
  296. ICell cell = row.GetCell(col);
  297. if (cell != null)
  298. {
  299. int contextLength = Encoding.GetEncoding("gb2312").GetBytes(cell.ToString()).Length;//获取当前单元格的内容宽度
  300. columnWidth = columnWidth < contextLength ? contextLength : columnWidth;
  301. }
  302. }
  303. columnWidth = columnWidth * 256 * 11 / 10;
  304. columnWidth = columnWidth > 254 * 256 ? 254 * 256 : columnWidth;
  305. sheet.SetColumnWidth(col, columnWidth);
  306. try
  307. {
  308. sheet.AutoSizeColumn(col);
  309. sheet.SetColumnWidth(col, sheet.GetColumnWidth(col) * 11 / 10);
  310. }
  311. catch (Exception ex)
  312. {
  313. }
  314. }
  315. }
  316. public static MemoryStream ExportAllSchoolExcel(RaceInfo raceInfo, List<RaceContactInfo> contactInfos, List<RaceSporterInfo> sporterInfos, List<RaceStatisticsInfo> statisticsInfos)
  317. {
  318. if (raceInfo == null)
  319. {
  320. raceInfo = new RaceInfo();
  321. }
  322. if (contactInfos == null)
  323. {
  324. contactInfos = new List<RaceContactInfo>();
  325. }
  326. if (sporterInfos == null)
  327. {
  328. sporterInfos = new List<RaceSporterInfo>();
  329. }
  330. if (statisticsInfos == null)
  331. {
  332. statisticsInfos = new List<RaceStatisticsInfo>();
  333. }
  334. foreach (var itemSporter in sporterInfos)
  335. {
  336. if (itemSporter.Projects == null)
  337. {
  338. itemSporter.Projects = new List<string>();
  339. }
  340. while (itemSporter.Projects.Count < 4)
  341. {
  342. itemSporter.Projects.Add(string.Empty);
  343. }
  344. }
  345. HSSFWorkbook sourceBook = null;
  346. ISheet sheet = null;
  347. IRow row = null;
  348. int rowsCount = 0;
  349. int currentRowIndex = 0;
  350. using (FileStream fs = File.Open(HttpContext.Current.Server.MapPath("~/Template/资格审查导出模板.xls"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
  351. {
  352. sourceBook = new HSSFWorkbook(fs);
  353. }
  354. var schools = sporterInfos.OrderBy(p => p.SchoolType).GroupBy(m => new { m.SchoolID, m.SchoolName }).Distinct().Select(t => new RaceSporterInfo
  355. {
  356. SchoolID = t.Key.SchoolID,
  357. SchoolName = t.Key.SchoolName
  358. }).ToList();
  359. //var schools = sporterInfos.GroupBy(p => p.SchoolID).Select(p => p.).ToList();
  360. #region 代表队
  361. sheet = sourceBook.GetSheet("代表队");
  362. rowsCount = schools.Count * 3 + sporterInfos.Count + 2;
  363. for (int i = 0; i < rowsCount; i++)
  364. {
  365. row = sheet.GetRow(i);
  366. if (row == null)
  367. {
  368. row = sheet.CreateRow(i);
  369. }
  370. }
  371. sheet.GetRow(0).GetCell(0).SetCellValue(raceInfo.RaceName ?? string.Empty);
  372. currentRowIndex = 1;
  373. foreach (var itemSchool in schools)
  374. {
  375. currentRowIndex++;
  376. if (currentRowIndex != 2)
  377. {
  378. sheet.CopyRow(2, currentRowIndex);
  379. }
  380. sheet.GetRow(currentRowIndex).GetCell(0).SetCellValue(itemSchool.SchoolName);
  381. currentRowIndex++;
  382. if (currentRowIndex != 3)
  383. {
  384. sheet.CopyRow(3, currentRowIndex);
  385. }
  386. var contactmod = contactInfos.Where(p => p.SchoolID == itemSchool.SchoolID).FirstOrDefault();
  387. if (contactmod.IfNotNull())
  388. {
  389. string content = "";
  390. if (!contactmod.Coach1.IsEmpty())
  391. {
  392. content = (content.IsEmpty()) ? contactmod.Coach1 : content + " " + contactmod.Coach1;
  393. }
  394. if (!contactmod.Coach2.IsEmpty())
  395. {
  396. content = (content.IsEmpty()) ? contactmod.Coach2 : content + " " + contactmod.Coach2;
  397. }
  398. if (!contactmod.Coach3.IsEmpty())
  399. {
  400. content = (content.IsEmpty()) ? contactmod.Coach3 : content + " " + contactmod.Coach3;
  401. }
  402. if (!contactmod.Coach4.IsEmpty())
  403. {
  404. content = (content.IsEmpty()) ? contactmod.Coach4 : content + " " + contactmod.Coach4;
  405. }
  406. var cellContent = string.Format("领队:{0} 教练员:{1} 联系电话:{2}", contactmod.Leader, content, contactmod.ContactPhone);
  407. sheet.GetRow(currentRowIndex).GetCell(0).SetCellValue(cellContent);
  408. }
  409. else
  410. {
  411. sheet.GetRow(currentRowIndex).GetCell(0).SetCellValue("");
  412. }
  413. currentRowIndex++;
  414. if (currentRowIndex != 4)
  415. {
  416. sheet.CopyRow(4, currentRowIndex);
  417. }
  418. var sporters = sporterInfos.Where(p => p.SchoolID == itemSchool.SchoolID).OrderBy(p => p.Sex).OrderBy(p => p.GroupName).ToList();
  419. foreach (var itemSporter in sporters)
  420. {
  421. currentRowIndex++;
  422. if (currentRowIndex != 5)
  423. {
  424. sheet.CopyRow(5, currentRowIndex);
  425. }
  426. row = sheet.GetRow(currentRowIndex);
  427. row.GetCell(0).SetCellValue("");
  428. row.GetCell(1).SetCellValue(itemSporter.SporterName.Trim());
  429. row.GetCell(2).SetCellValue(itemSporter.Sex.Trim());
  430. row.GetCell(3).SetCellValue(itemSporter.RegNo.Trim());
  431. row.GetCell(4).SetCellValue(itemSporter.IDCard.Trim());
  432. row.GetCell(5).SetCellValue(itemSporter.Grade.Trim());
  433. row.GetCell(6).SetCellValue(itemSporter.GroupName.Trim());
  434. row.GetCell(7).SetCellValue(itemSporter.Projects[0]);
  435. row.GetCell(8).SetCellValue(itemSporter.Projects[1]);
  436. row.GetCell(9).SetCellValue(itemSporter.Projects[2]);
  437. row.GetCell(10).SetCellValue(itemSporter.Projects[3]);
  438. }
  439. currentRowIndex++;
  440. if (currentRowIndex != 6)
  441. {
  442. sheet.CopyRow(6, currentRowIndex);
  443. }
  444. row = sheet.GetRow(currentRowIndex);
  445. row.GetCell(1).SetCellValue("");
  446. row.GetCell(2).SetCellValue("");
  447. row.GetCell(3).SetCellValue("");
  448. row.GetCell(4).SetCellValue("");
  449. row.GetCell(5).SetCellValue("");
  450. row.GetCell(6).SetCellValue("");
  451. row.GetCell(7).SetCellValue("");
  452. row.GetCell(8).SetCellValue("");
  453. row.GetCell(9).SetCellValue("");
  454. row.GetCell(10).SetCellValue("");
  455. }
  456. #endregion
  457. #region 参赛单位人数统计表
  458. sheet = sourceBook.GetSheet("参赛单位人数统计表");
  459. row = sheet.GetRow(0);
  460. var groups = sporterInfos.GroupBy(p => p.GroupName).Select(p => p.Key).ToList();
  461. var columnIndex = 1;
  462. int toHide = (row.Cells.Count - 6) / 2 - groups.Count();
  463. while (toHide > 0)
  464. {
  465. columnIndex++;
  466. sheet.SetColumnHidden(columnIndex, true);
  467. columnIndex++;
  468. sheet.SetColumnHidden(columnIndex, true);
  469. toHide--;
  470. }
  471. row.GetCell(0).SetCellValue(raceInfo.RaceName + "报名统计报表");
  472. row = sheet.GetRow(1);
  473. toHide = (row.Cells.Count - 6) / 2 - groups.Count();
  474. columnIndex = 1 + toHide * 2;
  475. for (int i = 0; i < groups.Count(); i++)
  476. {
  477. columnIndex++;
  478. row.GetCell(columnIndex).SetCellValue(groups[i]);
  479. columnIndex += 1;
  480. }
  481. int seq = 0;
  482. int count = 0;
  483. currentRowIndex = 2;
  484. foreach (var itemSchool in schools)
  485. {
  486. columnIndex = 1 + toHide * 2;
  487. currentRowIndex++;
  488. seq++;
  489. row = sheet.GetRow(currentRowIndex);
  490. if (row == null)
  491. {
  492. row = sheet.CopyRow(3, currentRowIndex);
  493. }
  494. row.GetCell(0).SetCellValue(seq);
  495. row.GetCell(1).SetCellValue(itemSchool.SchoolName);
  496. foreach (var itemGroup in groups)
  497. {
  498. count = sporterInfos.Where(p => p.SchoolID == itemSchool.SchoolID && p.GroupName == itemGroup && p.Sex == "男").Count();
  499. columnIndex++;
  500. row.GetCell(columnIndex).SetCellValue(count);
  501. count = sporterInfos.Where(p => p.SchoolID == itemSchool.SchoolID && p.GroupName == itemGroup && p.Sex == "女").Count();
  502. columnIndex++;
  503. row.GetCell(columnIndex).SetCellValue(count);
  504. }
  505. count = sporterInfos.Where(p => p.SchoolID == itemSchool.SchoolID).Count();
  506. columnIndex++;
  507. row.GetCell(columnIndex).SetCellValue(count);
  508. count = statisticsInfos.Where(p => p.SchoolID == itemSchool.SchoolID).Sum(p => p.LeaderCount);
  509. columnIndex++;
  510. row.GetCell(columnIndex).SetCellValue(count);
  511. count = statisticsInfos.Where(p => p.SchoolID == itemSchool.SchoolID).Sum(p => p.CoachCount);
  512. columnIndex++;
  513. row.GetCell(columnIndex).SetCellValue(count);
  514. count = sporterInfos.Where(p => p.SchoolID == itemSchool.SchoolID).Count() + statisticsInfos.Where(p => p.SchoolID == itemSchool.SchoolID).Sum(p => p.LeaderCount + p.CoachCount);
  515. columnIndex++;
  516. row.GetCell(columnIndex).SetCellValue(count);
  517. }
  518. currentRowIndex++;
  519. row = sheet.GetRow(currentRowIndex);
  520. if (row == null)
  521. {
  522. row = sheet.CopyRow(3, currentRowIndex);
  523. }
  524. columnIndex = 1 + toHide * 2;
  525. seq++;
  526. row.GetCell(0).SetCellValue(seq);
  527. row.GetCell(1).SetCellValue("总计");
  528. foreach (var itemGroup in groups)
  529. {
  530. count = sporterInfos.Where(p => p.GroupName == itemGroup && p.Sex == "男").Count();
  531. columnIndex++;
  532. row.GetCell(columnIndex).SetCellValue(count);
  533. count = sporterInfos.Where(p => p.GroupName == itemGroup && p.Sex == "女").Count();
  534. columnIndex++;
  535. row.GetCell(columnIndex).SetCellValue(count);
  536. }
  537. count = sporterInfos.Count();
  538. columnIndex++;
  539. row.GetCell(columnIndex).SetCellValue(count);
  540. count = statisticsInfos.Sum(p => p.LeaderCount);
  541. columnIndex++;
  542. row.GetCell(columnIndex).SetCellValue(count);
  543. count = statisticsInfos.Sum(p => p.CoachCount);
  544. columnIndex++;
  545. row.GetCell(columnIndex).SetCellValue(count);
  546. count = sporterInfos.Count() + statisticsInfos.Sum(p => p.LeaderCount + p.CoachCount);
  547. columnIndex++;
  548. row.GetCell(columnIndex).SetCellValue(count);
  549. #endregion
  550. #region 联系方式
  551. sheet = sourceBook.GetSheet("联系方式表");
  552. rowsCount = contactInfos.Count + 2;
  553. for (int i = 0; i < rowsCount; i++)
  554. {
  555. row = sheet.GetRow(i);
  556. if (row == null)
  557. {
  558. row = sheet.CreateRow(i);
  559. }
  560. }
  561. currentRowIndex = 1;
  562. foreach (var itemContact in contactInfos)
  563. {
  564. currentRowIndex++;
  565. if (currentRowIndex != 2)
  566. {
  567. sheet.CopyRow(2, currentRowIndex);
  568. }
  569. row = sheet.GetRow(currentRowIndex);
  570. row.GetCell(0).SetCellValue(itemContact.SchoolName);
  571. row.GetCell(1).SetCellValue(itemContact.Leader);
  572. row.GetCell(2).SetCellValue(itemContact.Coach1);
  573. row.GetCell(3).SetCellValue(itemContact.Coach2);
  574. row.GetCell(4).SetCellValue(itemContact.Coach3);
  575. row.GetCell(5).SetCellValue(itemContact.Coach4);
  576. row.GetCell(6).SetCellValue(itemContact.Contact);
  577. row.GetCell(7).SetCellValue(itemContact.ContactPhone);
  578. }
  579. #endregion
  580. #region 成绩表
  581. var templateSheet = sourceBook.GetSheet("成绩表");
  582. var templateSheetIndex = sourceBook.GetSheetIndex("成绩表");
  583. var projects = new List<string>();
  584. foreach (var itemGroup in groups)
  585. {
  586. projects.Clear();
  587. sporterInfos.Where(p => p.GroupName == itemGroup).ToList().ForEach(p => projects.AddRange(p.Projects));
  588. projects = projects.Where(p => !string.IsNullOrWhiteSpace(p)).Distinct().ToList();
  589. foreach (var itemProject in projects)
  590. {
  591. var desSporterInfos = sporterInfos.Where(p => p.GroupName == itemGroup && p.Projects.Contains(itemProject));
  592. var desSheet = sourceBook.CloneSheet(templateSheetIndex);
  593. var desSheetIndex = sourceBook.GetSheetIndex(desSheet);
  594. var sheetName = itemGroup + "成绩表" + "(" + itemProject + ")";
  595. sourceBook.SetSheetName(desSheetIndex, sheetName);
  596. desSheet.GetRow(1).GetCell(2).SetCellValue(raceInfo.RaceName ?? string.Empty);
  597. desSheet.GetRow(2).GetCell(2).SetCellValue(raceInfo.RaceTime.ToString("yyyy-MM-dd"));
  598. desSheet.GetRow(2).GetCell(6).SetCellValue(raceInfo.Address);
  599. desSheet.GetRow(4).GetCell(0).SetCellValue(sheetName);
  600. currentRowIndex = 5;
  601. int recordNo = 0;
  602. foreach (var itemDesSporter in desSporterInfos)
  603. {
  604. recordNo++;
  605. currentRowIndex++;
  606. row = desSheet.GetRow(currentRowIndex);
  607. if (row == null)
  608. {
  609. row = desSheet.CreateRow(currentRowIndex);
  610. }
  611. if (currentRowIndex != 6)
  612. {
  613. desSheet.CopyRow(6, currentRowIndex);
  614. }
  615. row.GetCell(0).SetCellValue(recordNo);
  616. row.GetCell(1).SetCellValue(itemDesSporter.SporterName);
  617. row.GetCell(2).SetCellValue(itemDesSporter.Sex);
  618. row.GetCell(3).SetCellValue(string.Empty);
  619. row.GetCell(4).SetCellValue(itemDesSporter.IDCard);
  620. row.GetCell(5).SetCellValue(itemDesSporter.StudentNo);
  621. row.GetCell(6).SetCellValue(itemDesSporter.SchoolName);
  622. }
  623. }
  624. }
  625. sourceBook.RemoveSheetAt(templateSheetIndex);
  626. #endregion
  627. MemoryStream ms = new MemoryStream();
  628. sourceBook.Write(ms);
  629. ms.Seek(0, SeekOrigin.Begin);
  630. return ms;
  631. }
  632. public class RaceInfo
  633. {
  634. public string RaceName { get; set; }
  635. public DateTime RaceTime { get; set; }
  636. public string Address { get; set; }
  637. }
  638. public class RaceContactInfo
  639. {
  640. public int SchoolID { get; set; }
  641. public string SchoolName { get; set; }
  642. public string Leader { get; set; }
  643. public string Coach1 { get; set; }
  644. public string Coach2 { get; set; }
  645. public string Coach3 { get; set; }
  646. public string Coach4 { get; set; }
  647. public string Contact { get; set; }
  648. public string ContactPhone { get; set; }
  649. }
  650. public class RaceSporterInfo
  651. {
  652. public string SchoolName { get; set; }
  653. public int SchoolID { get; set; }
  654. public int SchoolType { get; set; }
  655. public string GroupName { get; set; }
  656. public string SporterName { get; set; }
  657. public string IDCard { get; set; }
  658. public string StudentNo { get; set; }
  659. public string Sex { get; set; }
  660. public string RegNo { get; set; }
  661. public string Grade { get; set; }
  662. public List<string> Projects { get; set; }
  663. }
  664. public class RaceStatisticsInfo
  665. {
  666. public int SchoolID { get; set; }
  667. public string SchoolName { get; set; }
  668. public int SporterCount { get; set; }
  669. public int LeaderCount { get; set; }
  670. public int CoachCount { get; set; }
  671. public int TotalCount { get; set; }
  672. }
  673. }
  674. }