Query_SpecialReport.cs 8.5 KB

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