Query_SpecialReport.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. namespace UAS_MES.Query
  12. {
  13. public partial class Query_SpecialReport : Form
  14. {
  15. DataHelper dh = SystemInf.dh;
  16. public Query_SpecialReport()
  17. {
  18. InitializeComponent();
  19. }
  20. private void Export_Click(object sender, EventArgs e)
  21. {
  22. ExcelExport("");
  23. }
  24. //选择导出Excel时是选择导出数据的还是模板
  25. private void ExcelExport(string DataOrTemplet)
  26. {
  27. //Data表示导出数据
  28. //Templet表示导出模板
  29. folderBrowserDialog1.Description = "选择导出的路径";
  30. DialogResult result = folderBrowserDialog1.ShowDialog();
  31. if (result == DialogResult.OK)
  32. {
  33. string FolderPath = folderBrowserDialog1.SelectedPath;
  34. ExcelHandler eh = new ExcelHandler();
  35. DataTable dt = new DataTable();
  36. string st_tablesql = "";
  37. string st_id = "";
  38. string st_condition = "";
  39. dt = (DataTable)dh.ExecuteSql("select st_id,st_tablesql,st_condition from SEARCHTEMPLATE where st_title='ZIDE出货报表'", "select");
  40. if (dt.Rows.Count > 0)
  41. {
  42. st_tablesql = dt.Rows[0]["st_tablesql"].ToString();
  43. st_id = dt.Rows[0]["st_id"].ToString();
  44. st_condition = dt.Rows[0]["st_condition"].ToString();
  45. dt = (DataTable)dh.ExecuteSql("select stg_field,stg_text,stg_table,stg_formula,stg_type from SEARCHTEMPLATEgrid where stg_stid='" + st_id + "'", "select");
  46. string SQLField = "";
  47. for (int i = 0; i < dt.Rows.Count; i++)
  48. {
  49. if (dt.Rows[i]["stg_table"].ToString() != "")
  50. {
  51. if (dt.Rows[i]["stg_type"].ToString() == "DATE")
  52. {
  53. SQLField += "to_char(" + dt.Rows[i]["stg_field"].ToString() + ",'YYYYMMDD') as " + dt.Rows[i]["stg_text"].ToString() + ",";
  54. }
  55. else
  56. {
  57. SQLField += dt.Rows[i]["stg_field"].ToString() + " as " + dt.Rows[i]["stg_text"].ToString() + ",";
  58. }
  59. }
  60. else
  61. {
  62. if (dt.Rows[i]["stg_formula"].ToString().Replace("'", "").Trim() == "")
  63. SQLField += "''" + " as " + dt.Rows[i]["stg_text"].ToString() + ",";
  64. else
  65. SQLField += dt.Rows[i]["stg_formula"].ToString() + " as " + dt.Rows[i]["stg_text"].ToString() + ",";
  66. }
  67. }
  68. SQLField = SQLField.Substring(0, SQLField.Length - 1);
  69. string sql = "select " + SQLField + " from " + st_tablesql;
  70. if (st_condition != "")
  71. {
  72. sql += " where " + st_condition;
  73. }
  74. if (SerialNum.Text != "")
  75. {
  76. sql += " and ms_sncode='" + SerialNum.Text + "'";
  77. }
  78. if (CartonBegin.Text != "" || CartonEnd.Text != "")
  79. {
  80. if (CartonBegin.Text != "" && CartonEnd.Text == "")
  81. {
  82. sql += " and ms_outboxcode> '" + CartonBegin.Text + "' ";
  83. }
  84. if (CartonBegin.Text == "" && CartonEnd.Text != "")
  85. {
  86. sql += " and ms_outboxcode< '" + CartonBegin.Text + "' ";
  87. }
  88. if (CartonBegin.Text != "" && CartonEnd.Text != "")
  89. {
  90. sql += " and ms_outboxcode between '" + CartonBegin.Text + "' and '" + CartonEnd.Text + "' ";
  91. }
  92. }
  93. if (PaletteBegin.Text != "" || PaletteBegin.Text != "")
  94. {
  95. if (PaletteBegin.Text != "" && PaletteEnd.Text == "")
  96. {
  97. sql += " and v_outboxcode> '" + PaletteBegin.Text + "' ";
  98. }
  99. if (PaletteBegin.Text == "" && PaletteEnd.Text != "")
  100. {
  101. sql += " and v_outboxcode< '" + PaletteEnd.Text + "' ";
  102. }
  103. if (PaletteBegin.Text != "" && PaletteEnd.Text != "")
  104. {
  105. sql += " and v_outboxcode between '" + PaletteBegin.Text + "' and '" + PaletteEnd.Text + "' ";
  106. }
  107. }
  108. if (InDate.Text != "")
  109. {
  110. sql += " and trunc(ms_indate) = trunc(to_date('" + InDate.Value.ToString("yyyy-MM-dd") + "','yyyy-MM-dd')) ";
  111. }
  112. dt = (DataTable)dh.ExecuteSql(sql, "select");
  113. //导出Excel的时候返回一个文件名,用户选择是否打开
  114. /*string filePath = */
  115. eh.WriteTxt(dt, FolderPath, "NL" + DateTime.Now.Year.ToString().Substring(2,2) + (DateTime.Now.DayOfYear < 100 ? "0" + DateTime.Now.DayOfYear.ToString() : DateTime.Now.DayOfYear.ToString()) + "COC");
  116. ////用户选择导出之后是否立即打开
  117. //MessageBoxButtons messButton = MessageBoxButtons.YesNo;
  118. //string openFile = MessageBox.Show(this.ParentForm, "是否打开文件", "提示", messButton).ToString();
  119. //if (openFile == "Yes")
  120. //{
  121. // System.Diagnostics.Process.Start(filePath);
  122. //}
  123. }
  124. }
  125. }
  126. }
  127. }