Query_SpecialReport.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. using System;
  2. using System.Data;
  3. using System.Windows.Forms;
  4. using UAS_MES_NEW.DataOperate;
  5. using UAS_MES_NEW.Entity;
  6. using UAS_MES_NEW.PublicMethod;
  7. namespace UAS_MES_NEW.Query
  8. {
  9. public partial class Query_SpecialReport : Form
  10. {
  11. DataHelper dh = SystemInf.dh;
  12. public Query_SpecialReport()
  13. {
  14. InitializeComponent();
  15. }
  16. private void Export_Click(object sender, EventArgs e)
  17. {
  18. ExcelExport("");
  19. }
  20. //选择导出Excel时是选择导出数据的还是模板
  21. private void ExcelExport(string DataOrTemplet)
  22. {
  23. //Data表示导出数据
  24. //Templet表示导出模板
  25. folderBrowserDialog1.Description = "选择导出的路径";
  26. DialogResult result = folderBrowserDialog1.ShowDialog();
  27. if (result == DialogResult.OK)
  28. {
  29. string FolderPath = folderBrowserDialog1.SelectedPath;
  30. ExcelHandler eh = new ExcelHandler();
  31. DataTable dt = new DataTable();
  32. string st_tablesql = "";
  33. string st_id = "";
  34. string st_condition = "";
  35. dt = (DataTable)dh.ExecuteSql("select st_id,st_tablesql,st_condition from SEARCHTEMPLATE where st_title='ZIDE出货报表'", "select");
  36. if (dt.Rows.Count > 0)
  37. {
  38. string InOutNo = "";
  39. st_tablesql = dt.Rows[0]["st_tablesql"].ToString();
  40. st_id = dt.Rows[0]["st_id"].ToString();
  41. st_condition = dt.Rows[0]["st_condition"].ToString();
  42. string SQLField = "";
  43. string AddCondition = "";
  44. if (inoutno.Text != "")
  45. {
  46. AddCondition = " and ms_outno='" + inoutno.Text + "'";
  47. }
  48. if (SerialNum.Text != "")
  49. {
  50. AddCondition += " and ms_sncode='" + SerialNum.Text + "'";
  51. }
  52. if (CartonBegin.Text != "" || CartonEnd.Text != "")
  53. {
  54. if (CartonBegin.Text != "" && CartonEnd.Text == "")
  55. {
  56. AddCondition += " and ms_outboxcode> '" + CartonBegin.Text + "' ";
  57. }
  58. if (CartonBegin.Text == "" && CartonEnd.Text != "")
  59. {
  60. AddCondition += " and ms_outboxcode< '" + CartonBegin.Text + "' ";
  61. }
  62. if (CartonBegin.Text != "" && CartonEnd.Text != "")
  63. {
  64. AddCondition += " and ms_outboxcode between '" + CartonBegin.Text + "' and '" + CartonEnd.Text + "' ";
  65. }
  66. }
  67. if (PaletteBegin.Text != "" || PaletteBegin.Text != "")
  68. {
  69. if (PaletteBegin.Text != "" && PaletteEnd.Text == "")
  70. {
  71. AddCondition += " and v_outboxcode> '" + PaletteBegin.Text + "' ";
  72. }
  73. if (PaletteBegin.Text == "" && PaletteEnd.Text != "")
  74. {
  75. AddCondition += " and v_outboxcode< '" + PaletteEnd.Text + "' ";
  76. }
  77. if (PaletteBegin.Text != "" && PaletteEnd.Text != "")
  78. {
  79. AddCondition += " and v_outboxcode between '" + PaletteBegin.Text + "' and '" + PaletteEnd.Text + "' ";
  80. }
  81. }
  82. if (InDate.Checked)
  83. {
  84. AddCondition += " and trunc(ms_indate) = trunc(to_date('" + InDate.Value.ToString("yyyy-MM-dd") + "','yyyy-MM-dd')) ";
  85. }
  86. string GetInOutNoSQL = "select ms_outno from " + st_tablesql;
  87. if (st_condition != "")
  88. {
  89. GetInOutNoSQL += " where " + st_condition;
  90. }
  91. //获取出货单号
  92. dt = (DataTable)dh.ExecuteSql(GetInOutNoSQL + AddCondition + " and rownum=1", "select");
  93. if (dt.Rows.Count > 0)
  94. {
  95. InOutNo = dt.Rows[0]["ms_outno"].ToString();
  96. }
  97. DataTable SeqTanle = (DataTable)dh.ExecuteSql("select cl_code,cl_result from commandlog where cl_operate='导出特殊报表' order by cl_id desc", "select");
  98. string SEQ_NUMBER = "";
  99. string SEQ = "";
  100. string SEQ_PreFix = "NL" + DateTime.Now.Year.ToString().Substring(2, 2) + (DateTime.Now.DayOfYear < 100 ? "0" + DateTime.Now.DayOfYear.ToString() : DateTime.Now.DayOfYear.ToString()) + "COC";
  101. if (SeqTanle.Rows.Count > 0)
  102. {
  103. //如果今天已经包含本出货单则导出的使用之前的流水
  104. DataRow[] dr = SeqTanle.Select("cl_result='" + InOutNo + "'");
  105. if (dr.Length > 0)
  106. {
  107. SEQ_NUMBER = dr[0]["cl_code"].ToString();
  108. SEQ = SEQ_PreFix + SEQ_NUMBER;
  109. }
  110. else
  111. {
  112. SEQ_NUMBER = SeqTanle.Rows[0]["cl_code"].ToString();
  113. int temp = int.Parse(SEQ_NUMBER) + 1;
  114. SEQ_NUMBER = temp.ToString().PadLeft(5, '0');
  115. SEQ = SEQ_PreFix + SEQ_NUMBER;
  116. LogicHandler.DoCommandLog(Tag.ToString(), User.CurrentStepCode, "", "", User.UserSourceCode, "导出特殊报表", InOutNo, SEQ, SEQ_NUMBER);
  117. }
  118. }
  119. else
  120. {
  121. SEQ = SEQ_PreFix + "00001";
  122. LogicHandler.DoCommandLog(Tag.ToString(), User.CurrentStepCode, "", "", User.UserSourceCode, "导出特殊报表", InOutNo, SEQ, "00001");
  123. }
  124. dt = (DataTable)dh.ExecuteSql("select stg_field,stg_text,stg_table,stg_formula,stg_type from SEARCHTEMPLATEgrid where stg_stid='" + st_id + "'", "select");
  125. for (int i = 0; i < dt.Rows.Count; i++)
  126. {
  127. if (dt.Rows[i]["stg_table"].ToString() != "")
  128. {
  129. if (dt.Rows[i]["stg_type"].ToString() == "DATE")
  130. {
  131. SQLField += "to_char(" + dt.Rows[i]["stg_field"].ToString() + ",'YYYYMMDD')||';' as " + dt.Rows[i]["stg_text"].ToString() + ",";
  132. }
  133. else
  134. {
  135. SQLField += dt.Rows[i]["stg_field"].ToString() + "||';' as " + dt.Rows[i]["stg_text"].ToString() + ",";
  136. }
  137. }
  138. else
  139. {
  140. if (dt.Rows[i]["stg_text"].ToString() == "SEQ")
  141. {
  142. SQLField += "'" + SEQ + "'" + "||';' as " + dt.Rows[i]["stg_text"].ToString() + ",";
  143. }
  144. else if (dt.Rows[i]["stg_formula"].ToString().Replace("'", "").Trim() == "")
  145. SQLField += "''" + " as " + dt.Rows[i]["stg_text"].ToString() + ",";
  146. else
  147. SQLField += dt.Rows[i]["stg_formula"].ToString() + "||';' as " + dt.Rows[i]["stg_text"].ToString() + ",";
  148. }
  149. }
  150. SQLField = SQLField.Substring(0, SQLField.Length - 1);
  151. string sql = "select " + SQLField + " from " + st_tablesql;
  152. //单独获取出货单号对流水进行标识
  153. if (st_condition != "")
  154. {
  155. sql += " where " + st_condition;
  156. }
  157. //界面生成的条件
  158. dt = (DataTable)dh.ExecuteSql(sql + AddCondition, "select");
  159. //导出Excel的时候返回一个文件名,用户选择是否打开
  160. eh.WriteTxt(dt, FolderPath, SEQ);
  161. MessageBox.Show("导出成功");
  162. }
  163. }
  164. }
  165. private void Query_SpecialReport_Load(object sender, EventArgs e)
  166. {
  167. InDate.Checked = false;
  168. }
  169. }
  170. }