//导出数据到Excel protected void lBtnToExcel_Click(object sender, EventArgs e) { //获取数据并填充到数据表 using (var da = new v_sample_lot_card_join_orderTableAdapter()) { //检测session中保存的查询条件 object obj = Session["sample_lot_card_print_where_content"]; string str = string.Empty; if (obj != null && obj.ToString().Length > 0) { str = obj.ToString(); } //获取前1万行数据 var tab = da.GetPagedData(0, 10000, str, "order by [id] desc", false); //设置列标题 string[] titleCol = ("序号,lot卡号,是补料单,订单序号,pnl数量,首部门,备注," + "录入员,录入时间,修改时间").Split(','); //设置表名称 string titleTab = "待打印样板lot卡清单"; //临时文件名称 string fileName = titleTab + DateTime.Now.ToString("yyMMddHHmmss") + ".xls"; //执行导出数据到excel bool isSuccess = ydPublicMethod.DataTable2Excel(tab, titleCol, ref fileName, titleTab); //如果成功则跳转到指定页面 if (isSuccess) { Response.Redirect(fileName); //停止加载后续内容 Response.End(); } } }
//导出数据到Excel protected void lBtnToExcel_Click(object sender, EventArgs e) { //获取数据并填充到数据表 using (var da = new v_sample_lot_card_join_orderTableAdapter()) { //检测session中保存的查询条件 object obj = Session["sample_lot_card_mgr_where_content"]; string str = string.Empty; if (obj != null && obj.ToString().Length > 0) { str = obj.ToString(); } //获取前1万行数据 var tab = da.GetPagedData(0, 10000, str, "order by [id] desc", false); //设置列标题 string[] titleCol = ("序号,订单序号,lot卡号,lot卡月份,lot卡序号,是否补料,pnl长,pnl宽," + "开卡pnl数量,pnl面积,本卡pnl总面积,pnl含pcs数,本卡pcs总数,首工序," + "开卡备注,录入员,录入时间,修改时间,首卡卡号,尾卡卡号,状态,良率," + "订单样板编号,pcs面积,客户名称,客户代码,订单编号," + "订单pcs总数,订单pcs总面积,仓存pcs数,下单日期,需求日期,生产日期," + "已出pcs数量,已出pcs面积,未出pcs数量,未出pcs面积,订单备注").Split(','); //设置表名称 string titleTab = "样板lot卡清单"; //临时文件名称 string fileName = titleTab + DateTime.Now.ToString("yyMMddHHmmss") + ".xls"; //执行导出数据到excel bool isSuccess = ydPublicMethod.DataTable2Excel(tab, titleCol, ref fileName, titleTab); //如果成功则跳转到指定页面 if (isSuccess) { Response.Redirect(fileName); //停止加载后续内容 Response.End(); } } }
//导出数据到Excel protected void lBtnToExcel_Click(object sender, EventArgs e) { //获取数据并填充到数据表 using (var da = new v_sample_lot_card_join_orderTableAdapter()) { //检测session中保存的查询条件 object obj = Session["sample_lot_card_mgr_supplement_where_content"]; string str = string.Empty; if (obj != null && obj.ToString().Length > 0) { str = obj.ToString(); } //获取前1万行数据 var tab = da.GetPagedData(0, 10000, str, "order by [id] desc", true); //设置列标题 string[] titleCol = ("序号,批量卡序号,是补料单,订单序号,pnl数量,首部门,备注," + "录入员,录入时间,修改时间,状况,良率,样板编号,pnl长,pnl宽,pnl面积," + "客户名称,客户料号,批量卡编号,批量卡pcs数,批量卡pcs面积," + "开料pnl数,开料pnl面积,pnl含pcs数,开料pcs数,仓存pcs数" + "落单日期,需求日期,生产日期,已交货pcs数,已交货pcs面积," + "欠出货pcs数,欠出货pcs面积,事业部,销售单号,特殊流程,SMT备注,FPC单价,SMT单价," + "批次号,规格,备品数量,覆盖膜,投料率").Split(','); //设置表名称 string titleTab = "样板补料批量卡清单"; //临时文件名称 string fileName = titleTab + DateTime.Now.ToString("yyMMddHHmmss") + ".xls"; //执行导出数据到excel bool isSuccess = ydPublicMethod.DataTable2Excel(tab, titleCol, ref fileName, titleTab); //如果成功则跳转到指定页面 if (isSuccess) { Response.Redirect(fileName); //停止加载后续内容 Response.End(); } } }
//根据传入的参数处理事情 public void ProcessRequest(HttpContext context) { //清空之前数据 context.Response.Clear(); //检测是否含有session if (context.Session.Count < 5) { //跳转 context.Response.Redirect("/Account/Login", true); //停止加载后续内容 context.Response.End(); //直接返回 return; } //执行基类的方法 if (!base.CheckIsLogin(context)) { return; } //用户传入的lot卡号 string lotId = context.Request["lid"]; //检测lot卡号 if (lotId == null || lotId.Length < 6) { throw new Exception("未提供lot卡号参数或者lot卡号参数长度太短!"); } //待输出到浏览器的数据 string strResult = string.Empty; try { //根据lot卡号获取lot卡和订单信息 using (var da = new v_sample_lot_card_join_orderTableAdapter()) using (var tab = da.GetDataByLotId(lotId)) { //检测获取到的数据数据 if (tab.Rows.Count <= 0) { strResult = "打印失败:PMC录入的lot卡不存在!"; } else { //首行 var row = (DataSetSampleLotCardMgr.v_sample_lot_card_join_orderRow)tab.Rows[0]; //取得样板编号 string productNum = row.product_num.Trim().ToUpper(); //样板编号中的层数起始字符索引 int iLayerStart = 0; //检测样板编号的正确性 string strReg = @"^TYY-[1-9]\d{0,7}[0A-Z][0-9]$"; if (!Regex.IsMatch(productNum, strReg)) { strResult = string.Format("打印失败:{0}样板编号不符合规则,必须为 TYY100A0 格式!", productNum); } else { //第二个字符为减号则从第三个字符开始 if (productNum.Substring(2, 1) == "-") { iLayerStart += 2; } //抓取样板编号中的数字序号串和版本 string pNum = string.Empty; for (int i = iLayerStart; i < productNum.Length; i++) { //层数截至位置 if (char.IsLetter(productNum, i)) { for (int j = i + 1; j < productNum.Length; j++) { //流程截至位置 if (char.IsNumber(productNum, j)) { //最后两位为版本号 pNum = productNum.Substring(j, productNum.Length - j - 2); break; } } break; } } string pVersion = productNum.Substring(productNum.Length - 2); //样板编号转数字 int k = pNum.IndexOf("-"); int iNum, iNum2; string strFileNameNum = string.Empty; if (k > 0) { iNum = int.Parse(pNum.Substring(0, k)); iNum2 = int.Parse(pNum.Substring(k + 1)); } else { iNum = int.Parse(pNum); iNum2 = 0; } //当前样板编号数字或者样板编号数字段 strFileNameNum = iNum.ToString("0000"); if (iNum2 > 0) { strFileNameNum += "-" + iNum2.ToString("0000"); } //根目录编号段 int iNumBegin = (iNum / 3000) * 3000 + 1; int iNumEnd = iNumBegin + 3000 - 1; //拼接文件夹路径 var sb = new StringBuilder("H:\\工程"); //确定顶级文件夹名称 sb.Append(iNumBegin.ToString("0000") + "-" + iNumEnd.ToString("0000")); //确定次级文件夹名称 sb.Append("\\" + strFileNameNum); //加入版本文件夹 sb.Append("\\" + pVersion); //存储路径用来取得单体图路径 var sb2 = new StringBuilder(sb.ToString()); //lot卡末级文件夹名称 sb.Append("\\DOC"); //确认lot卡文件名称 sb.Append("\\" + strFileNameNum + pVersion + ".xls"); //得到lot卡文件全路径 string lotCardFullPath = sb.ToString(); //单体图末级文件夹名称 sb2.Append("\\DWG"); //确认单体图文件名称 sb2.Append("\\" + strFileNameNum + pVersion + "单体图.dwg"); //得到单体图文件全路径 string danTiTuFullPath = sb2.ToString(); //检查文件是否存在 if (!File.Exists(lotCardFullPath)) { lotCardFullPath = lotCardFullPath.Replace("\\DOC\\", "\\D0C\\"); } if (!File.Exists(lotCardFullPath)) { lotCardFullPath = lotCardFullPath.Replace("\\D0C\\", "\\DOC\\"); strResult = "打印失败:工程lot卡不存在!路径为:" + lotCardFullPath; } else { //excel对象 var xlApp = new Excel.Application(); //打开该excel文件 xlApp.Visible = true; xlApp.DisplayAlerts = false; try { //打开工作薄 var xlBook = xlApp.Workbooks.Open(lotCardFullPath, ReadOnly: true); var xlSheet = (Excel.Worksheet)xlBook.Worksheets[1]; //设置值 //开料本LOT数量 xlSheet.Range["M7"].Value = row.pnl_qty.ToString() + " pnl"; //订单数量 xlSheet.Range["A4"].Value = "订单数量: " + row.order_pcs_qty.ToString() + " pcs"; //单据状态 xlSheet.Range["I4"].Value = row.state; //LOT NO ((Excel.Range)xlSheet.Range["J3:M4"]).MergeCells = false; ((Excel.Range)xlSheet.Range["K4:M4"]).Merge(); xlSheet.Range["K4"].HorizontalAlignment = Excel.Constants.xlCenter; xlSheet.Range["K4"].Value = row.lot_id; //设置字体加大加粗 xlSheet.Range["M3,M7,A4,K4"].Font.Bold = true; xlSheet.Range["M3,M7,K4"].Font.Size = 18; xlSheet.Range["A4"].Characters[6].Font.Size = 18; //只打印为一页 xlSheet.PageSetup.FitToPagesWide = 1; xlSheet.PageSetup.FitToPagesTall = 1; //检查文件是否存在 if (!File.Exists(danTiTuFullPath)) { strResult = "打印部分失败:单体图不存在!路径为:" + danTiTuFullPath; } else { //待打印成的图片文件的名称 string imgName = context.Server.MapPath(string.Format(@"dwg2jpg-{0}.jpg", context.Session["user_name"])); imgName = (new FileInfo(imgName)).FullName; //待打印成的图片文件的名称 imgName = (new FileInfo(imgName)).FullName; //删除之前的文件 if (File.Exists(imgName)) { File.Delete(imgName); } //执行打印单体图生成图片文件 var p = new Process(); p.StartInfo.UseShellExecute = false; //p.StartInfo.RedirectStandardInput = true; p.StartInfo.RedirectStandardOutput = true; //p.StartInfo.RedirectStandardError = true; p.StartInfo.CreateNoWindow = true; p.StartInfo.FileName = context.Server.MapPath(@"PrintLotCard\Dwg2Jpg.exe"); p.StartInfo.Arguments = string.Format(@" {0} {1}", danTiTuFullPath, imgName); p.Start(); string strOutPut = p.StandardOutput.ReadToEnd(); //p.WaitForExit(60000); //检测文件是否存在 if (strOutPut.Length > 0 || !File.Exists(imgName)) { strResult = string.Format("打印单体图失败:{0}" + strOutPut, productNum); } else { //设置为活动 ((Excel._Workbook)xlBook).Activate(); ((Excel._Worksheet)xlSheet).Select(); //在excel中插入该图片并设置分页 string strError = string.Empty; if (InsertImageToExcelSheet(xlSheet, imgName, out strError)) { //打印lot卡和单体图 xlSheet.PrintOut(From: 1, To: 2, Copies: 1, Collate: true); //关闭工作表 xlBook.Close(SaveChanges: false); } else { //抛出错误 throw new Exception(strError); } //删除生成的图片文件 if (File.Exists(imgName)) { File.Delete(imgName); } //待返回的状态结果 strResult = "打印成功!"; } } //注销excel相关对象 xlSheet = null; xlBook = null; } catch (Exception exx) { throw exx; } finally { //关闭excel文档 foreach (Excel.Workbook itm in xlApp.Workbooks) { itm.Close(SaveChanges: false); } xlApp.Quit(); //关闭excel进程 ydPublicMethod.KillExcel(xlApp); xlApp = null; } } } } } } catch (Exception ex) { strResult = "打印失败:" + ex.Message; } //写入数据到浏览器 context.Response.Write(strResult); }
/// <summary> /// 检测是否存在指定的部门名称 /// </summary> /// <param name="txtLotId">当前待报数的lot卡序文本框</param> /// <param name="source">当前验证控件</param> /// <param name="args">验证控件提供的验证事件参数对象</param> /// <returns></returns> internal static bool IsSameOrderProductNum( TextBox txtLotId, object source, ServerValidateEventArgs args ) { //当前对象 var cv = ((CustomValidator)source); cv.ToolTip = "只允许版本号与订单生产编号不相同"; //是否找到lot卡号文本框 if (txtLotId == null) { return false; } //获取用户输入的lot卡号 string lotId = txtLotId.Text; cv.ToolTip = "提供的lot卡号不正确"; //检测lot卡号 if (!lotId.Contains("-") || lotId.Length < 6) { return false; } //检测是否样板 bool isSample = lotId.ToUpper().Contains("S"); //非样板使用生产订单和lot卡中的数据 if (!isSample && !ydOperateLotCard.IsLotCardId(ref lotId)) { return false; } else if (isSample && !ydOperateSampleLotCard.IsSampleLotCardId(ref lotId)) { return false; } //非样板订单在生产订单中查询 if (!isSample) { //通过lot卡号获取订单中生产编号 using (var da = new v_ppc_lot_card_join_orderTableAdapter()) { //获取 object obj = da.GetProductNumByLotId(lotId); if (obj == null) { cv.ToolTip = "在订单中未找到生产编号"; return false; } else { //取得的订单生产编号 string orderProductNum = obj.ToString(); //取得的输入的生产编号 string userProductNum = args.Value; //小于两个字符不检测 if (orderProductNum.Length < 2) { return true; } else if (userProductNum.Length >= 2) { cv.ToolTip = string.Format("只允许版本号与订单生产编号\"{0}\"不相同", orderProductNum); //检测是否存在部门 return orderProductNum.Remove(orderProductNum.Length - 2) == userProductNum.Remove(userProductNum.Length - 2); } else { return false; } } } } //样板在样板订单中查询 else { //通过样板lot卡号获取样板订单中生产编号 using (var da = new v_sample_lot_card_join_orderTableAdapter()) { //获取 object obj = da.GetProductNumByLotId(lotId); if (obj == null) { cv.ToolTip = "在样板订单中未找到生产编号"; return false; } else { //取得的样板订单生产编号 string orderProductNum = obj.ToString(); //取得的输入的样板生产编号 string userProductNum = args.Value; //小于两个字符不检测 if (orderProductNum.Length < 2) { return true; } else if (userProductNum.Length >= 2) { cv.ToolTip = string.Format("只允许版本号与样板订单生产编号\"{0}\"不相同", orderProductNum); //检测是否存在部门 return orderProductNum.Remove(orderProductNum.Length - 2) == userProductNum.Remove(userProductNum.Length - 2); } else { return false; } } } } }