Query_SpecialReport.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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_NEW.DataOperate;
  10. using UAS_MES_NEW.Entity;
  11. using UAS_MES_NEW.PublicMethod;
  12. namespace UAS_MES_NEW.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 (inoutno.Text != "")
  50. {
  51. AddCondition = " and ms_outno='" + inoutno.Text + "'";
  52. }
  53. if (SerialNum.Text != "")
  54. {
  55. AddCondition += " and ms_sncode='" + SerialNum.Text + "'";
  56. }
  57. if (CartonBegin.Text != "" || CartonEnd.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< '" + CartonBegin.Text + "' ";
  66. }
  67. if (CartonBegin.Text != "" && CartonEnd.Text != "")
  68. {
  69. AddCondition += " and ms_outboxcode between '" + CartonBegin.Text + "' and '" + CartonEnd.Text + "' ";
  70. }
  71. }
  72. if (PaletteBegin.Text != "" || PaletteBegin.Text != "")
  73. {
  74. if (PaletteBegin.Text != "" && PaletteEnd.Text == "")
  75. {
  76. AddCondition += " and v_outboxcode> '" + PaletteBegin.Text + "' ";
  77. }
  78. if (PaletteBegin.Text == "" && PaletteEnd.Text != "")
  79. {
  80. AddCondition += " and v_outboxcode< '" + PaletteEnd.Text + "' ";
  81. }
  82. if (PaletteBegin.Text != "" && PaletteEnd.Text != "")
  83. {
  84. AddCondition += " and v_outboxcode between '" + PaletteBegin.Text + "' and '" + PaletteEnd.Text + "' ";
  85. }
  86. }
  87. if (InDate.Checked)
  88. {
  89. AddCondition += " and trunc(ms_indate) = trunc(to_date('" + InDate.Value.ToString("yyyy-MM-dd") + "','yyyy-MM-dd')) ";
  90. }
  91. string GetInOutNoSQL = "select ms_outno from " + st_tablesql;
  92. if (st_condition != "")
  93. {
  94. GetInOutNoSQL += " where " + st_condition;
  95. }
  96. //获取出货单号
  97. dt = (DataTable)dh.ExecuteSql(GetInOutNoSQL + AddCondition + " and rownum=1", "select");
  98. if (dt.Rows.Count > 0)
  99. {
  100. InOutNo = dt.Rows[0]["ms_outno"].ToString();
  101. }
  102. DataTable SeqTanle = (DataTable)dh.ExecuteSql("select cl_code,cl_result from commandlog where cl_operate='导出特殊报表' order by cl_id desc", "select");
  103. string SEQ_NUMBER = "";
  104. string SEQ = "";
  105. 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";
  106. if (SeqTanle.Rows.Count > 0)
  107. {
  108. //如果今天已经包含本出货单则导出的使用之前的流水
  109. DataRow[] dr = SeqTanle.Select("cl_result='" + InOutNo + "'");
  110. if (dr.Length > 0)
  111. {
  112. SEQ_NUMBER = dr[0]["cl_code"].ToString();
  113. SEQ = SEQ_PreFix + SEQ_NUMBER;
  114. }
  115. else
  116. {
  117. SEQ_NUMBER = SeqTanle.Rows[0]["cl_code"].ToString();
  118. int temp = int.Parse(SEQ_NUMBER) + 1;
  119. SEQ_NUMBER = temp.ToString().PadLeft(5, '0');
  120. SEQ = SEQ_PreFix + SEQ_NUMBER;
  121. LogicHandler.DoCommandLog(Tag.ToString(), User.CurrentStepCode, "", "", User.UserSourceCode, "导出特殊报表", InOutNo, SEQ, SEQ_NUMBER);
  122. }
  123. }
  124. else
  125. {
  126. SEQ = SEQ_PreFix + "00001";
  127. LogicHandler.DoCommandLog(Tag.ToString(), User.CurrentStepCode, "", "", User.UserSourceCode, "导出特殊报表", InOutNo, SEQ, "00001");
  128. }
  129. dt = (DataTable)dh.ExecuteSql("select stg_field,stg_text,stg_table,stg_formula,stg_type from SEARCHTEMPLATEgrid where stg_stid='" + st_id + "'", "select");
  130. for (int i = 0; i < dt.Rows.Count; i++)
  131. {
  132. if (dt.Rows[i]["stg_table"].ToString() != "")
  133. {
  134. if (dt.Rows[i]["stg_type"].ToString() == "DATE")
  135. {
  136. SQLField += "to_char(" + dt.Rows[i]["stg_field"].ToString() + ",'YYYYMMDD')||';' as " + dt.Rows[i]["stg_text"].ToString() + ",";
  137. }
  138. else
  139. {
  140. SQLField += dt.Rows[i]["stg_field"].ToString() + "||';' as " + dt.Rows[i]["stg_text"].ToString() + ",";
  141. }
  142. }
  143. else
  144. {
  145. if (dt.Rows[i]["stg_text"].ToString() == "SEQ")
  146. {
  147. SQLField += "'" + SEQ + "'" + "||';' as " + dt.Rows[i]["stg_text"].ToString() + ",";
  148. }
  149. else if (dt.Rows[i]["stg_formula"].ToString().Replace("'", "").Trim() == "")
  150. SQLField += "''" + " as " + dt.Rows[i]["stg_text"].ToString() + ",";
  151. else
  152. SQLField += dt.Rows[i]["stg_formula"].ToString() + "||';' as " + dt.Rows[i]["stg_text"].ToString() + ",";
  153. }
  154. }
  155. SQLField = SQLField.Substring(0, SQLField.Length - 1);
  156. string sql = "select " + SQLField + " from " + st_tablesql;
  157. //单独获取出货单号对流水进行标识
  158. if (st_condition != "")
  159. {
  160. sql += " where " + st_condition;
  161. }
  162. //界面生成的条件
  163. dt = (DataTable)dh.ExecuteSql(sql + AddCondition, "select");
  164. //导出Excel的时候返回一个文件名,用户选择是否打开
  165. eh.WriteTxt(dt, FolderPath, SEQ);
  166. MessageBox.Show("导出成功");
  167. }
  168. }
  169. }
  170. private void Query_SpecialReport_Load(object sender, EventArgs e)
  171. {
  172. InDate.Checked = false;
  173. }
  174. }
  175. }