JhyOpe.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;//正则表达式所在空间
  6. using System.Data;
  7. using System.Windows.Forms;
  8. using Microsoft.Office.Interop.Excel;
  9. using Infragistics.Win;
  10. namespace Core.LZMes.Client.UIB
  11. {
  12. class JhyOpe
  13. {
  14. //double类型格式判断
  15. public static bool DcsDouble(string str)
  16. {
  17. Boolean bl = true;//默认为真,是字符串格式
  18. Regex r = new Regex(@"^\d+(\.\d+)?\d?$");
  19. if (str == "合格" || str == "不合格")//合格与不合格作为满足来处理
  20. {
  21. bl = true;
  22. }
  23. else
  24. {
  25. Match m = r.Match(str);
  26. if (!m.Success)
  27. {
  28. bl = false;
  29. }
  30. }
  31. return bl;
  32. }
  33. /// <summary>
  34. /// </summary>
  35. /// <param name="dt"></param>
  36. /// <param name="path">导出路径</param>
  37. /// <param name="fileName">文件名</param>
  38. /// <param name="modelExcel">模板名</param>
  39. public void Export(System.Data.DataTable dt, string path , string fileName , string modelExcel)
  40. {
  41. try
  42. {
  43. if (path == ""){MessageBox.Show("请选择导出路径" , "提示");return;}
  44. ApplicationClass ExcelApp = null;
  45. Microsoft.Office.Interop.Excel.Application excel = null;
  46. Microsoft.Office.Interop.Excel.Workbooks workbooks = null;
  47. Microsoft.Office.Interop.Excel._Workbook workbook = null;
  48. Microsoft.Office.Interop.Excel.Sheets sheets = null;
  49. Microsoft.Office.Interop.Excel.Worksheet sheet = null;
  50. //导出
  51. string strfile = System.Windows.Forms.Application.StartupPath + "\\" + modelExcel;//模板路径
  52. ExcelApp = new ApplicationClass();
  53. excel = new Microsoft.Office.Interop.Excel.Application();
  54. workbooks = excel.Workbooks;
  55. workbook = workbooks.Add(strfile);
  56. sheets = workbook.Sheets;
  57. sheet = (Microsoft.Office.Interop.Excel.Worksheet)sheets[1];
  58. if (fileName == "热轧拉力试验原始记录表")
  59. setData_1(dt , sheet);
  60. else if (fileName == "热轧冲击试验原始记录表")
  61. setData_2(dt , sheet);
  62. workbook.Saved = true;
  63. workbook.SaveCopyAs(path + "\\" + fileName + ".xls");//fileName如:热轧拉力试验原始记录表
  64. excel.DisplayAlerts = false;
  65. excel.AlertBeforeOverwriting = false;
  66. workbooks.Close();
  67. workbook = null;
  68. excel.Quit();
  69. excel = null;
  70. ExcelApp.Quit();
  71. ExcelApp = null;
  72. }
  73. catch (Exception ex)
  74. {
  75. System.Diagnostics.Debug.WriteLine(ex.ToString());
  76. }
  77. }
  78. //热轧拉力试验原始记录表
  79. private void setData_1(System.Data.DataTable dt , Microsoft.Office.Interop.Excel.Worksheet sheet)
  80. {
  81. int startRow = 5;//模板数据填充从第五行开始
  82. Range range = null;
  83. int i = 0;
  84. int j = 1;
  85. for (i = 0; i < dt.Rows.Count; i++)
  86. {
  87. j = 1;
  88. sheet.Cells[startRow + i, j++] = dt.Rows[i]["SMP_STEEL_NO"].ToString();//钢印号
  89. sheet.Cells[startRow + i, j++] = dt.Rows[i]["SPEC_STL_GRD"].ToString();//牌号
  90. sheet.Cells[startRow + i, j++] = dt.Rows[i]["MATERIAL_NO"].ToString();//钢卷
  91. sheet.Cells[startRow + i, j++] = dt.Rows[i]["MATERIAL_THK"].ToString();//规格
  92. sheet.Cells[startRow + i, j++] = dt.Rows[i]["LA"].ToString();//a
  93. sheet.Cells[startRow + i, j++] = dt.Rows[i]["LB"].ToString();//b
  94. j++;
  95. j++;
  96. j++;
  97. j++;
  98. sheet.Cells[startRow + i, j++] = dt.Rows[i]["FE"].ToString();//Fe
  99. sheet.Cells[startRow + i, j++] = dt.Rows[i]["FM"].ToString();//Fm
  100. sheet.Cells[startRow + i, j++] = dt.Rows[i]["LO"].ToString();//Lo
  101. sheet.Cells[startRow + i, j++] = dt.Rows[i]["LU"].ToString();//Lu
  102. // sheet.Cells[startRow + i, j++] = dt.Rows[i]["ZJ"].ToString();//直径
  103. // sheet.Cells[startRow + i, j++] = dt.Rows[i]["CB"].ToString();//冷弯
  104. }
  105. range = sheet.get_Range(sheet.Cells[startRow, 1], sheet.Cells[startRow + (i - 1), j - 1 + 3]);//(Range)sheet.Rows[6, 1];
  106. range.Borders.LineStyle = 1;
  107. range.NumberFormatLocal = "@";//设置单元格格式为文本
  108. range.RowHeight = 22.5;
  109. //range.HorizontalAlignment = XlHAlign.xlHAlignLeft;//字体左靠
  110. // range.RowHeight = 24;
  111. // System.DateTime dt = System.DateTime.Now;
  112. // string TimeNow = string.Format("{0:yyyyMMddHHmmssffff}", dt);
  113. }
  114. //冲击试验原始记录表
  115. private void setData_2(System.Data.DataTable dt, Microsoft.Office.Interop.Excel.Worksheet sheet)
  116. {
  117. int startRow = 5;//模板数据填充从第五行开始
  118. Range range = null;
  119. int i = 0;
  120. int j = 1;
  121. for (i = 0; i < dt.Rows.Count; i++)
  122. {
  123. j = 1;
  124. sheet.Cells[startRow + i, j++] = dt.Rows[i]["SMP_STEEL_NO"].ToString();//钢印号
  125. sheet.Cells[startRow + i, j++] = dt.Rows[i]["SPEC_STL_GRD"].ToString();//牌号
  126. sheet.Cells[startRow + i, j++] = dt.Rows[i]["MATERIAL_NO"].ToString();//钢卷
  127. sheet.Cells[startRow + i, j++] = dt.Rows[i]["MATERIAL_THK"].ToString();//规格
  128. j++;//温度
  129. sheet.Cells[startRow + i, j++] = dt.Rows[i]["A1"].ToString();//A1
  130. sheet.Cells[startRow + i, j++] = dt.Rows[i]["A2"].ToString();//A2
  131. sheet.Cells[startRow + i, j++] = dt.Rows[i]["A3"].ToString();//A3
  132. //sheet.Cells[startRow + i, j++] = dt.Rows[i]["FM"].ToString();//Fm
  133. //sheet.Cells[startRow + i, j++] = dt.Rows[i]["LO"].ToString();//Lo
  134. //sheet.Cells[startRow + i, j++] = dt.Rows[i]["LU"].ToString();//Lu
  135. //sheet.Cells[startRow + i, j++] = dt.Rows[i]["ZJ"].ToString();//直径
  136. //sheet.Cells[startRow + i, j++] = dt.Rows[i]["CB"].ToString();//冷弯
  137. }
  138. //设置样式
  139. range = sheet.get_Range(sheet.Cells[startRow, 1], sheet.Cells[startRow + (i - 1), j - 1 + 1]);//(Range)sheet.Rows[6, 1];
  140. range.Borders.LineStyle = 1;
  141. range.NumberFormatLocal = "@";//设置单元格格式为文本
  142. range.RowHeight = 22.5;
  143. }
  144. public static void ttt()
  145. {
  146. //CheckBoxUIElement checkBoxUIElement = parent.GetDescendant(typeof(CheckBoxUIElement)) as CheckBoxUIElement;
  147. }
  148. }
  149. }