CodeSoft.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. using System;
  2. using System.Windows.Forms;
  3. using LabelManager2;
  4. using System.Drawing;
  5. using System.IO;
  6. using UAS_MES.DataOperate;
  7. using System.Data;
  8. namespace UAS_MES
  9. {
  10. public partial class CodeSoft : Form
  11. {
  12. ApplicationClass lbl =new ApplicationClass();
  13. //预览框中文件的索引
  14. int selectedIndex;
  15. //用来存放文件夹的文件名
  16. private string[] browsingFormats;
  17. //选中的Label的名称
  18. string LabelName;
  19. //包含路径的文件名
  20. string LabelPath;
  21. DataHelper dh = new DataHelper();
  22. public CodeSoft()
  23. {
  24. InitializeComponent();
  25. //不存在目录创建目录
  26. //if (!Directory.Exists(System.Windows.Forms.Application.StartupPath+("//Log")))//如果不存在就创建file文件夹
  27. //{
  28. // Console.Write("不存在");
  29. // Directory.CreateDirectory(System.Windows.Forms.Application.StartupPath + ("//Log"));
  30. //}
  31. ////不存在文件创建文件
  32. //string LogName = System.DateTime.Now.Date.ToString("yyyyMMdd");
  33. //if (!File.Exists(System.Windows.Forms.Application.StartupPath + "\\Log\\"+LogName+".txt")) {
  34. // Console.Write("不存在路径");
  35. // File.Create(System.Windows.Forms.Application.StartupPath + "\\Log\\" + LogName + ".txt");//创建该文件
  36. //}
  37. //FileStream fs1 = new FileStream(System.Windows.Forms.Application.StartupPath + "\\Log\\" + LogName + ".txt", FileMode.Append, FileAccess.Write);//创建写入文件
  38. //StreamWriter sw = new StreamWriter(fs1);
  39. //Console.Write(System.DateTime.Now.Second);
  40. //for (int i = 0; i < 100000; i++) {
  41. // sw.WriteLine(i);
  42. // sw.WriteLine("[runtype][runtype][runtype][runtype][runtype][runtype][runtype][runtype][runtype][runtype][runtype][runtype][runtype][runtype][runtype]");//开始写入值
  43. // sw.WriteLine("type=1");
  44. //}
  45. //Console.Write(System.DateTime.Now.Second);
  46. //sw.Close();
  47. //fs1.Close();
  48. //条码赋值
  49. //lbl.Documents.Open(@"C:\Users\callmexiaozhang\Desktop\Document1.lab", false);//调用设好的lbl标签
  50. //Document doc = lbl.ActiveDocument;
  51. //doc.Variables.Item("var0").Value = "123123";
  52. //doc.Variables.Item("var1").Value = "123123";
  53. //设置默认打印机
  54. //Console.Write(doc.Variables.FormVariables.Item("var0").Value);
  55. //Strings vars = lbl.PrinterSystem().Printers(enumKindOfPrinters.lppxAllPrinters);
  56. //PrintDocument prtdoc = new PrintDocument();
  57. //string strDefaultPrinter = prtdoc.PrinterSettings.PrinterName;
  58. //for (int j = 0; j < vars.Count; j++)
  59. //{
  60. // string[] arryString = vars.Item(j).Split(',');
  61. // comboBox1.Items.Add(arryString[0]);
  62. // if (arryString[0] == strDefaultPrinter)
  63. // {
  64. // comboBox1.SelectedItem = strDefaultPrinter;
  65. // }
  66. //}
  67. //设置listview中每个单元的大小和图片
  68. lstLabelBrowser.View = View.LargeIcon;
  69. lstLabelBrowser.LargeImageList = new ImageList();
  70. lstLabelBrowser.LargeImageList.ImageSize = new Size(50, 50);
  71. lstLabelBrowser.LargeImageList.Images.Add(UAS_MES.Properties.Resources.file_info_64px_1176626_easyicon_net);
  72. }
  73. private void button1_Click(object sender, EventArgs e)
  74. {
  75. folderBrowserDialog1.Description = "选择打印模板所在的路径";
  76. DialogResult result = folderBrowserDialog1.ShowDialog();
  77. if (result == DialogResult.OK) {
  78. lstLabelBrowser.Clear();
  79. FolderPath.Text = folderBrowserDialog1.SelectedPath;
  80. DirectoryInfo TheFolder = new DirectoryInfo(folderBrowserDialog1.SelectedPath);
  81. browsingFormats = Directory.GetFiles(FolderPath.Text, "*.lab");
  82. for (int i = 0; i < browsingFormats.Length; i++) {
  83. int lenght = browsingFormats[i].Split('\\').Length;
  84. lstLabelBrowser.Items.Add(browsingFormats[i].Split('\\')[lenght - 1]);
  85. lstLabelBrowser.Items[i].ImageIndex = 0;
  86. }
  87. }
  88. }
  89. private void saveFormButton1_Click(object sender, EventArgs e)
  90. {
  91. Error.Text = "";
  92. try
  93. {
  94. dh.CheckSQL(SQL.Text);
  95. if (LabelName != "" && LabelName != null)
  96. {
  97. SaveSQL();
  98. }
  99. else
  100. {
  101. Error.Text = "请选择一个标签文件";
  102. }
  103. }
  104. catch (Exception ex)
  105. {
  106. if (ex.Message.Contains("ORA-01008"))
  107. {
  108. SaveSQL();
  109. }
  110. else
  111. {
  112. Error.Text = ex.Message;
  113. }
  114. }
  115. }
  116. private void lstLabelBrowser_SelectedIndexChanged(object sender, EventArgs e)
  117. {
  118. if (lstLabelBrowser.SelectedItems.Count == 1) {
  119. selectedIndex = lstLabelBrowser.SelectedIndices[0];
  120. LabelPath =browsingFormats[selectedIndex];
  121. LabelName = lstLabelBrowser.Items[selectedIndex].Text.Split('.')[0];
  122. DataTable dt= dh.getFieldsDataByCondition("Label", new string[] {"la_sql"},"La_name='"+LabelName+"'");
  123. if (dt.Rows.Count > 0)
  124. {
  125. SQL.Text = dt.Rows[0]["la_sql"].ToString();
  126. }
  127. else {
  128. SQL.Text = "";
  129. }
  130. }
  131. }
  132. private void SaveSQL() {
  133. if (!dh.CheckExist("Label", "la_name='" + LabelName + "'"))
  134. {
  135. dh.ExecuteSql("insert into label (la_id,la_name,la_sql) values(label_seq.nextval,'" + LabelName + "','" + SQL.Text + "')", "insert");
  136. Error.Text = "保存成功";
  137. }
  138. else
  139. {
  140. dh.UpdateByCondition("label", "la_sql='" + SQL.Text + "'", "la_name='" + LabelName + "'");
  141. Error.Text = "保存成功";
  142. }
  143. }
  144. private void Check_SQL_Click(object sender, EventArgs e)
  145. {
  146. try
  147. {
  148. dh.CheckSQL(SQL.Text);
  149. Error.Text = "检测通过";
  150. }
  151. catch (Exception ex)
  152. {
  153. //如果有动态添加的参数则不去判断
  154. if (ex.Message.Contains("ORA-01008"))
  155. {
  156. Error.Text = "检测通过";
  157. }
  158. else
  159. {
  160. Error.Text = ex.Message;
  161. }
  162. }
  163. }
  164. private void PrintTest_Click(object sender, EventArgs e)
  165. {
  166. if (LabelPath != null && LabelPath != "")
  167. {
  168. if (SQL.Text != null && SQL.Text != "")
  169. {
  170. try
  171. {
  172. DataTable dt=(DataTable)dh.ExecuteSql(SQL.Text, "select");
  173. Error.Text = "检测通过";
  174. lbl.Documents.Open(LabelPath, false);//调用设好的lbl标签
  175. Document doc = lbl.ActiveDocument;
  176. if (doc.Variables.Count > 0)
  177. {
  178. for (int i = 0; i < dt.Columns.Count; i++) {
  179. doc.Variables.Item(dt.Columns[i].ColumnName.ToLower()).Value = dt.Rows[0][i].ToString();
  180. }
  181. }
  182. doc.PrintDocument(1);
  183. doc.Close();
  184. Error.Text = "通过测试";
  185. }
  186. catch (Exception ex)
  187. {
  188. //如果有动态添加的参数则不去判断
  189. if (ex.Message.Contains("ORA-01008"))
  190. {
  191. Error.Text = "检测通过";
  192. }
  193. else
  194. {
  195. Error.Text = ex.Message;
  196. }
  197. }
  198. }
  199. else
  200. {
  201. Error.Text = "请先维护SQL";
  202. }
  203. }
  204. else
  205. {
  206. Error.Text = "请选择一个标签";
  207. }
  208. }
  209. private void FolderPath_TextChanged(object sender, EventArgs e)
  210. {
  211. }
  212. private void label1_Click(object sender, EventArgs e)
  213. {
  214. }
  215. private void CodeSoft_Load(object sender, EventArgs e)
  216. {
  217. }
  218. }
  219. }