ExcelTools.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.IO;
  7. using DevExpress.XtraSpreadsheet;
  8. using ClosedXML.Excel;
  9. using DevExpress.XtraGrid.Views.Grid;
  10. using DevExpress.XtraGrid;
  11. namespace Uninpho.Tools.components.MTLFF
  12. {
  13. public class ExcelTools
  14. {
  15. public static string filenameall;
  16. public static string chezhan;
  17. public static string daocha;
  18. public static string zhecha;
  19. public static string jianceshijian;
  20. public static string jianceren;
  21. public static string shejituhao;
  22. public static string xianluming;
  23. public static string zhenhaoqs;//起始枕号
  24. public static string zhenhaozz;//终止枕号
  25. public static GridControl gridControl01;
  26. public static GridControl gridControl02;
  27. public static GridControl gridControl03;
  28. public static GridView gridView01;
  29. public static GridView gridView02;
  30. public static GridView gridView03;
  31. public static SpreadsheetControl sheetControldemo;
  32. /// <summary>
  33. /// 文件转字节
  34. /// </summary>
  35. /// <param name="fileUrl"></param>
  36. /// <returns></returns>
  37. public static string GetFileData(string fileUrl)
  38. {
  39. FileStream fs = new FileStream(fileUrl, FileMode.Open, FileAccess.Read);
  40. try
  41. {
  42. byte[] buffur = new byte[fs.Length];
  43. fs.Read(buffur, 0, (int)fs.Length);
  44. string numberarr = "";
  45. for (int i = 0; i < buffur.Length; i++)
  46. {
  47. if (i == buffur.Length - 1)
  48. {
  49. numberarr += buffur[i].ToString();
  50. break;
  51. }
  52. numberarr += buffur[i].ToString() + ",";
  53. }
  54. return numberarr;
  55. }
  56. catch (Exception ex)
  57. {
  58. //MessageBoxHelper.ShowPrompt(ex.Message);
  59. return null;
  60. }
  61. finally
  62. {
  63. if (fs != null)
  64. {
  65. //关闭资源
  66. fs.Close();
  67. }
  68. }
  69. }
  70. /// <summary>
  71. /// 字节转文件
  72. /// </summary>
  73. /// <param name="pReadByte"></param>
  74. /// <param name="fileName"></param>
  75. /// <returns></returns>
  76. public static bool writeFile(string pReadBytearr, string fileName)
  77. {
  78. var pReadBytearrss = pReadBytearr.Split(',');
  79. byte[] pReadByte = new byte[pReadBytearrss.Length];
  80. for (int i = 0; i < pReadBytearrss.Length-1; i++)
  81. {
  82. pReadByte[i] = Convert.ToByte(pReadBytearrss[i].ToString());
  83. }
  84. FileStream pFileStream = null;
  85. try
  86. {
  87. pFileStream = new FileStream(fileName, FileMode.OpenOrCreate);
  88. pFileStream.Write(pReadByte, 0, pReadByte.Length);
  89. }
  90. catch
  91. {
  92. return false;
  93. }
  94. finally
  95. {
  96. if (pFileStream != null)
  97. pFileStream.Close();
  98. }
  99. return true;
  100. }
  101. /// <summary>
  102. /// 添加抬头信息
  103. /// </summary>
  104. public static void initNewTableExcel()
  105. {
  106. var workbook = new XLWorkbook(filenameall);
  107. IXLWorksheet XLWorksheet = workbook.Worksheet(1);
  108. XLWorksheet.Row(2).Cell(3).SetValue(chezhan);
  109. XLWorksheet.Row(2).Cell(6).SetValue(daocha);
  110. XLWorksheet.Row(2).Cell(9).SetValue(zhecha );
  111. XLWorksheet.Row(2).Cell(12).SetValue(shejituhao );
  112. XLWorksheet.Row(2).Cell(14).SetValue(xianluming);
  113. XLWorksheet.Row(2).Cell(17).SetValue(jianceren);
  114. XLWorksheet.Row(2).Cell(20).SetValue(jianceshijian);
  115. workbook.Save();
  116. }
  117. /// <summary>
  118. /// 添加逐枕几何抬头信息
  119. /// </summary>
  120. public static void initNewTableExcelzzjh()
  121. {
  122. var workbook = new XLWorkbook(filenameall);
  123. IXLWorksheet XLWorksheet = workbook.Worksheet(1);
  124. IXLStyle style = XLWorksheet.Style;
  125. XLWorksheet.Row(2).Cell(2).SetValue(chezhan);
  126. XLWorksheet.Row(2).Cell(4).SetValue(daocha);
  127. XLWorksheet.Row(2).Cell(6).SetValue(zhecha);
  128. XLWorksheet.Row(2).Cell(8).SetValue(shejituhao);
  129. XLWorksheet.Row(3).Cell(2).SetValue(jianceren);
  130. XLWorksheet.Row(2).Cell(10).SetValue(jianceshijian);
  131. CreateExcel2zhenhaoqujian(XLWorksheet, style);
  132. workbook.Save();
  133. }
  134. /// <summary>
  135. /// 逐枕几何通过枕号区间创建excel
  136. /// </summary>
  137. public static void CreateExcel2zhenhaoqujian(IXLWorksheet XLWorksheet, IXLStyle style)
  138. {
  139. int startZH = int.Parse(zhenhaoqs);
  140. int endZH = int.Parse(zhenhaozz);
  141. int startrow = 6;
  142. int rowcount = (int)Math.Ceiling((decimal)(endZH - startZH+1) / 2);
  143. for (int i = 0; i < rowcount; i++)
  144. {
  145. XLWorksheet.Row(startrow).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
  146. XLWorksheet.Row(startrow).Style.Font.Bold = true;
  147. changeBorder(XLWorksheet, startrow, 1);
  148. changeBorder(XLWorksheet, startrow, 2);
  149. changeBorder(XLWorksheet, startrow, 3);
  150. changeBorder(XLWorksheet, startrow, 4);
  151. changeBorder(XLWorksheet, startrow, 5);
  152. changeBorder(XLWorksheet, startrow, 6);
  153. changeBorder(XLWorksheet, startrow, 7);
  154. changeBorder(XLWorksheet, startrow, 8);
  155. changeBorder(XLWorksheet, startrow, 9);
  156. changeBorder(XLWorksheet, startrow, 10);
  157. changeBorder(XLWorksheet, startrow, 11);
  158. XLWorksheet.Row(startrow).Cell(1).SetValue(startZH.ToString());
  159. if (endZH >= startZH + rowcount)
  160. {
  161. XLWorksheet.Row(startrow).Cell(6).SetValue((startZH + rowcount).ToString());
  162. }
  163. startrow++;
  164. startZH++;
  165. }
  166. }
  167. private static void changeBorder(IXLWorksheet XLWorksheet,int row,int culum)
  168. {
  169. XLWorksheet.Row(row).Cell(culum).Style.Border.TopBorder = XLBorderStyleValues.Medium;
  170. XLWorksheet.Row(row).Cell(culum).Style.Border.BottomBorder = XLBorderStyleValues.Medium;
  171. XLWorksheet.Row(row).Cell(culum).Style.Border.LeftBorder = XLBorderStyleValues.Medium;
  172. XLWorksheet.Row(row).Cell(culum).Style.Border.RightBorder = XLBorderStyleValues.Medium;
  173. }
  174. }
  175. }