Print.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using UAS_MES.DataOperate;
  2. using LabelManager2;
  3. using System.Data;
  4. using System.Text;
  5. using System.IO;
  6. namespace UAS_MES.PublicMethod
  7. {
  8. class Print
  9. {
  10. static DataHelper dh = new DataHelper();
  11. //CodeSoft打印的驱动和文件
  12. static Document doc;
  13. ////CodeSoft的打印机
  14. //string CodeSpft_Printer;
  15. static FileInfo info;
  16. public Print() { }
  17. /// <summary>
  18. /// 默认打印一份
  19. /// </summary>
  20. /// <param name="Url"></param>
  21. /// <param name="LabelCode"></param>
  22. public static void CodeSoft(ApplicationClass lbl, string LabelName, string LabelCode, string PrinterName, string SnCode,int PrintNum,System.DateTime intime)
  23. {
  24. //打开模板路径
  25. doc = lbl.Documents.Open(ftpOperater.DownLoadTo + LabelName);
  26. //查询模板对应的取值SQL和参数名称
  27. DataTable dt = (DataTable)dh.ExecuteSql("select lp_name,lp_sql from label left join LABELPARAMETER on la_id=lp_laid where la_code='" + LabelCode + "'", "select");
  28. //比对模板维护的参数和在系统中维护的参数,名称对应进行SQL取值赋给变量
  29. StringBuilder sb = new StringBuilder();
  30. for (int i = 0; i < doc.Variables.FormVariables.Count; i++)
  31. {
  32. for (int j = 0; j < dt.Rows.Count; j++)
  33. {
  34. if (doc.Variables.FormVariables.Item(i + 1).Name == dt.Rows[j]["lp_name"].ToString())
  35. {
  36. string sql = dt.Rows[j]["lp_sql"].ToString();
  37. sql = sql.Substring(0, sql.IndexOf("{")) + "'" + SnCode + "'";
  38. doc.Variables.FormVariables.Item(i + 1).Value = dh.GetLabelParam(sql).ToString();
  39. sb.AppendLine("打印参数【" + doc.Variables.FormVariables.Item(i + 1).Name + "】赋值," + "取值SQL:" + sql + ",取到值" + doc.Variables.FormVariables.Item(i + 1).Value);
  40. }
  41. }
  42. }
  43. LogManager.DoLog(sb.ToString());
  44. //保存本次赋值进行打印
  45. doc.Save();
  46. doc.Printer.SwitchTo(PrinterName);
  47. doc.PrintDocument(PrintNum);
  48. doc.Close();
  49. //讲文件最后写入时间改成数据库中拿到的时间
  50. info = new FileInfo(ftpOperater.DownLoadTo + LabelName);
  51. info.LastWriteTime = intime;
  52. }
  53. public static void CodeSoft(ApplicationClass lbl, string LabelName, string LabelCode, string PrinterName, string SnCode)
  54. {
  55. //打开模板路径
  56. doc = lbl.Documents.Open(ftpOperater.DownLoadTo + LabelName);
  57. //查询模板对应的取值SQL和参数名称
  58. DataTable dt = (DataTable)dh.ExecuteSql("select lp_name,lp_sql from label left join LABELPARAMETER on la_id=lp_laid where la_code='" + LabelCode + "'", "select");
  59. //比对模板维护的参数和在系统中维护的参数,名称对应进行SQL取值赋给变量
  60. for (int i = 0; i < doc.Variables.FreeVariables.Count; i++)
  61. {
  62. for (int j = 0; j < dt.Rows.Count; j++)
  63. {
  64. if (doc.Variables.FreeVariables.Item(i + 1).Name == dt.Rows[j]["lp_name"].ToString())
  65. {
  66. string sql = dt.Rows[j]["lp_sql"].ToString();
  67. sql = sql.Substring(0, sql.IndexOf("{")) + "'" + SnCode + "'";
  68. doc.Variables.FreeVariables.Item(i + 1).Value = dh.GetLabelParam(sql).ToString();
  69. }
  70. }
  71. }
  72. //保存本次赋值进行打印
  73. doc.Save();
  74. doc.Printer.SwitchTo(PrinterName);
  75. doc.PrintDocument();
  76. doc.Close();
  77. }
  78. }
  79. }