//导出数据到Excel protected void lBtnToExcel_Click(object sender, EventArgs e) { //获取数据并填充到数据表 using (var da = new t_wait_scrap_record_backTableAdapter()) { //检测session中保存的查询条件 object obj = Session["wait_scrap_record_back_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"); //设置列标题 string[] titleCol = ("序号,发回日期,部门名称,批量卡序号,生产编号,pnl出数,pcs出数,下部门,备注,录入员,录取时间,修改时间").Split(','); //设置表名称 string titleTab = "MRB部门生产记录"; //临时文件名称 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(); } } }
/// <summary> /// 根据输入的LOT格式来保存多条数据到数据库 /// </summary> /// <param name="e">传入的带有数据的事件参数</param> /// <param name="procName">当前部门名称</param> /// <returns></returns> private bool InsertData(FormViewInsertEventArgs e, string procName) { //当前用户输入的批量卡号 string strLotIdBegin = e.Values["lot_id"].ToString(); //当前用户输入的结束批量卡号 string strLotIdEnd = ((TextBox)fvWaitScrapRecordBackAdd.FindControl("txtLotIdEnd")).Text.Trim(); //数据适配器 //当前添加语句对象 //当前数据库连接 using (var da = new t_wait_scrap_record_backTableAdapter()) using (var cmd = da.Adapter.InsertCommand) using (var conn = cmd.Connection) { //打开数据库连接 conn.Open(); //开启事务 using (var tran = conn.BeginTransaction()) { //设置事务 da.Transaction = tran; //试运行 try { //检测是否有结束批量卡号 if (strLotIdEnd.Length == 0) { //检测批量卡是否正确 //正则表达式 string strReg = @"^[1-9]\d(0[1-9]|1[012])-S?[1-9]\d{0,4}"; //检测是否正确批量卡号 if (!Regex.IsMatch(strLotIdBegin, strReg)) { //非数字返回失败 throw new Exception("您输入了一个不合格的批量卡号 " + strLotIdBegin + " !"); } //保存一条记录到数据库 if (!ydOperateBalanceLotCrad.InsertOneRecord(cmd, strLotIdBegin, e, true, null)) { //失败就直接返回 return false; } } else { //正则表达式 string strReg = @"^[1-9]\d{0,4}$"; //检测是否正确批量卡号 if (!Regex.IsMatch(strLotIdEnd, strReg)) { //非数字返回失败 throw new Exception("您输入了一个不合格的结束批量卡号 " + strLotIdEnd + " !"); } //检测是否样板 bool isSample = strLotIdBegin.ToUpper().Contains("S"); //批量卡中的年份月份和数字索引号 string strYearMonth; int iLotIdStart; int iLotIdEnd = int.Parse(strLotIdEnd); if (!isSample) { strYearMonth = strLotIdBegin.Substring(0, 5); iLotIdStart = int.Parse(strLotIdBegin.Substring(5)); } else { strYearMonth = strLotIdBegin.Substring(0, 6); iLotIdStart = int.Parse(strLotIdBegin.Substring(6)); } //检测起始和结束批量卡号,让起始批量卡号最小 if (iLotIdStart > iLotIdEnd) { int i = iLotIdStart; iLotIdStart = iLotIdEnd; iLotIdEnd = i; } //批量卡不能太多,不允许超过10张 if (iLotIdEnd - iLotIdStart > 10) { //不允许超过10张 throw new Exception( string.Format( "您输入批量卡号段 {0} 到 {1}{2} 的批量卡太多,不允许超过10张!", strLotIdBegin, strYearMonth, strLotIdEnd ) ); } //区分生产和样板来执行保存操作 if (!isSample) { //获取订单序号的数据适配器 using (var daGetOrderId = new t_ppc_lot_cardTableAdapter()) { //设置数据库连接 daGetOrderId.Connection = conn; //设置事务 daGetOrderId.Transaction = tran; //订单序号 string orderId = string.Empty; //检测输入的批量卡号段是否在同一个订单序号中 for (int i = iLotIdStart; i <= iLotIdEnd; i++) { //获取 var tab = daGetOrderId.GetDataByLotId(strYearMonth + i.ToString()); //检查结果 if (tab.Rows.Count == 0) { //报告失败 throw new Exception( string.Format( "您输入批量卡号段 {0} 到 {1}{2} 不存在!", strLotIdBegin, strYearMonth, strLotIdEnd ) ); } else { //首张批量卡号只获取不比较 if (i == iLotIdStart) { //取得订单序号 orderId = ((DataSetPpcLotCardMgr.t_ppc_lot_cardRow)tab.Rows[0]).order_id; } else if (orderId != ((DataSetPpcLotCardMgr.t_ppc_lot_cardRow)tab.Rows[0]).order_id) { //报告失败 throw new Exception( string.Format( "您输入批量卡号段 {0} 到 {1}{2} 中的{1}{3}跟其之前的批量卡不是一张订单!", strLotIdBegin, strYearMonth, strLotIdEnd, i ) ); } } } //执行保存 for (int i = iLotIdStart; i <= iLotIdEnd; i++) { //当前待保存的批量卡序号 string curLotId = strYearMonth + i.ToString(); //保存一条记录到数据库 if (!ydOperateBalanceLotCrad.InsertOneRecord(cmd, curLotId, e, true, null)) { //失败就直接返回 return false; } } } } else { //获取样板订单序号的数据适配器 using (var daGetOrderId = new t_sample_lot_cardTableAdapter()) { //设置数据库连接 daGetOrderId.Connection = conn; //设置事务 daGetOrderId.Transaction = tran; //订单序号 string orderId = string.Empty; //检测输入的批量卡号段是否在同一个订单序号中 for (int i = iLotIdStart; i <= iLotIdEnd; i++) { //获取 var tab = daGetOrderId.GetDataByLotId(strYearMonth + i.ToString()); //检查结果 if (tab.Rows.Count == 0) { //报告失败 throw new Exception( string.Format( "您输入的样板批量卡号段 {0} 到 {1}{2} 不存在!", strLotIdBegin, strYearMonth, strLotIdEnd ) ); } else { //首张批量卡号只获取不比较 if (i == iLotIdStart) { //取得订单序号 orderId = ((DataSetSampleLotCardMgr.t_sample_lot_cardRow)tab.Rows[0]).order_id; } else if (orderId != ((DataSetSampleLotCardMgr.t_sample_lot_cardRow)tab.Rows[0]).order_id) { //报告失败 throw new Exception( string.Format( "您输入的样板批量卡号段 {0} 到 {1}{2} 中的{1}{3}跟其之前的批量卡不是一张订单!", strLotIdBegin, strYearMonth, strLotIdEnd, i ) ); } } } //执行保存 for (int i = iLotIdStart; i <= iLotIdEnd; i++) { //当前待保存的批量卡序号 string curLotId = strYearMonth + i.ToString(); //保存一条记录到数据库 if (!ydOperateBalanceLotCrad.InsertOneRecord(cmd, curLotId, e, true, null)) { //失败就直接返回 return false; } } } } } //提交事务 tran.Commit(); //返回成功 return true; } catch (Exception ex) { //回滚事务 tran.Rollback(); //非数字返回失败 throw new Exception(ex.Message); } } } }