using BLL; using System; using System.Collections.Generic; using System.Data; using System.Data.OleDb; using System.IO; using System.Linq; using System.Web.UI; using System.Web.UI.WebControls; namespace FineUIPro.Web.HJGL.PersonManage { public partial class TestApplicationDataIn : PageBase { #region 定义变量 /// /// 上传预设的虚拟路径 /// private string initPath = Const.ExcelUrl; /// /// 错误集合 /// public static List errorInfos = new List(); #endregion #region 加载页面 /// /// 加载页面 /// /// /// protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { if (errorInfos != null) { errorInfos.Clear(); } } } #endregion #region 审核 /// /// 审核 /// /// /// protected void btnAudit_Click(object sender, EventArgs e) { try { if (this.FileExcel.HasFile == false) { Alert.ShowInTop("请您选择Excel文件!", MessageBoxIcon.Warning); return; } string IsXls = Path.GetExtension(FileExcel.FileName).ToString().Trim().ToLower(); if (IsXls != ".xls") { Alert.ShowInTop("只可以选择Excel文件!", MessageBoxIcon.Warning); return; } if (errorInfos != null) { errorInfos.Clear(); } string rootPath = Server.MapPath("~/"); string initFullPath = rootPath + initPath; if (!Directory.Exists(initFullPath)) { Directory.CreateDirectory(initFullPath); } this.hdfileName.Text = BLL.Funs.GetNewFileName() + IsXls; string filePath = initFullPath + this.hdfileName.Text; FileExcel.PostedFile.SaveAs(filePath); ImportXlsToData(filePath); } catch (Exception ex) { Alert.ShowInTop(ex.Message); } } #region 读Excel提取数据 /// /// 从Excel提取数据--》Dataset /// /// Excel文件路径名 private void ImportXlsToData(string fileName) { try { string oleDBConnString = String.Empty; oleDBConnString = "Provider=Microsoft.Jet.OLEDB.4.0;"; oleDBConnString += "Data Source="; oleDBConnString += fileName; oleDBConnString += ";Extended Properties=Excel 8.0;"; OleDbConnection oleDBConn = null; OleDbDataAdapter oleAdMaster = null; DataTable m_tableName = new DataTable(); DataSet ds = new DataSet(); oleDBConn = new OleDbConnection(oleDBConnString); oleDBConn.Open(); m_tableName = oleDBConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null); if (m_tableName != null && m_tableName.Rows.Count > 0) { m_tableName.TableName = m_tableName.Rows[0]["TABLE_NAME"].ToString().Trim(); } string sqlMaster; sqlMaster = " SELECT * FROM [" + m_tableName.TableName + "]"; oleAdMaster = new OleDbDataAdapter(sqlMaster, oleDBConn); oleAdMaster.SelectCommand.CommandTimeout = 1200; oleAdMaster.Fill(ds, "m_tableName"); oleAdMaster.Dispose(); oleDBConn.Close(); oleDBConn.Dispose(); AddDatasetToSQL(ds.Tables[0]); } catch (Exception ex) { throw ex; } } #endregion /// /// 将Dataset的数据导入数据库 /// /// 数据集 /// 数据集列数 /// private bool AddDatasetToSQL(DataTable pds) { string result = string.Empty; int ic, ir; ic = pds.Columns.Count; ir = pds.Rows.Count; if (pds != null && ir > 0) { var conUnits = from x in Funs.DB.Base_Unit join y in Funs.DB.Project_ProjectUnit on x.UnitId equals y.UnitId where y.ProjectId == this.CurrUser.LoginProjectId && y.UnitType == BLL.Const.ProjectUnitType_2 select x;//施工单位 var checkUnits = from x in Funs.DB.Base_Unit join y in Funs.DB.Project_ProjectUnit on x.UnitId equals y.UnitId where y.ProjectId == this.CurrUser.LoginProjectId && y.UnitType == BLL.Const.ProjectUnitType_5 select x;//检测单位 var users = from x in Funs.DB.Sys_User join y in Funs.DB.Project_ProjectUser on x.UserId equals y.UserId where y.ProjectId == this.CurrUser.LoginProjectId && y.RoleId.Contains(BLL.Const.SubWeldingEngineer) select x; //分包商焊接工程师 var workAreas = from x in Funs.DB.ProjectData_WorkArea where x.ProjectId == this.CurrUser.LoginProjectId select x; var weldingMethods = from x in Funs.DB.Base_WeldingMethod select x; for (int i = 0; i < ir; i++) { string col0 = pds.Rows[i][0].ToString().Trim(); if (!string.IsNullOrEmpty(col0)) { var unit = conUnits.FirstOrDefault(e => e.UnitName == col0); if (unit == null) { result += (i + 2).ToString() + "," + "施工单位" + "," + "[" + col0 + "]错误!" + "|"; } } else { result += (i + 2).ToString() + "," + "施工单位" + "," + "此项为必填项!" + "|"; } string col1 = pds.Rows[i][1].ToString().Trim(); if (!string.IsNullOrEmpty(col1)) { var user = users.FirstOrDefault(e => e.UserName == col1); if (user == null) { result += (i + 2).ToString() + "," + "施工单位焊接工程师" + "," + "[" + col1 + "]错误!" + "|"; } } string col2 = pds.Rows[i][2].ToString().Trim(); if (!string.IsNullOrEmpty(col2)) { try { DateTime d = Convert.ToDateTime(col2); } catch (Exception) { result += (i + 2).ToString() + "," + "证件有效期" + "," + "[" + col2 + "]错误!" + "|"; } } else { result += (i + 2).ToString() + "," + "证件有效期" + "," + "此项为必填项!" + "|"; } string col3 = pds.Rows[i][3].ToString().Trim(); if (!string.IsNullOrEmpty(col3)) { try { DateTime d = Convert.ToDateTime(col3); } catch (Exception) { result += (i + 2).ToString() + "," + "预考试日期" + "," + "[" + col3 + "]错误!" + "|"; } } else { result += (i + 2).ToString() + "," + "预考试日期" + "," + "此项为必填项!" + "|"; } string col5 = pds.Rows[i][5].ToString().Trim(); if (!string.IsNullOrEmpty(col5)) { if (col5 != "土建" && col5 != "结构" && col5 != "设备" && col5 != "管道") { result += (i + 2).ToString() + "," + "焊工类别" + "," + "[" + col5 + "]错误!" + "|"; } } else { result += (i + 2).ToString() + "," + "焊工类别" + "," + "此项为必填项!" + "|"; } if (string.IsNullOrEmpty(pds.Rows[i][6].ToString().Trim())) { result += (i + 2).ToString() + "," + "焊工编号" + "," + "此项为必填项!" + "|"; } if (string.IsNullOrEmpty(pds.Rows[i][7].ToString())) { result += (i + 2).ToString() + "," + "姓名" + "," + "此项为必填项!" + "|"; } string col8 = pds.Rows[i][8].ToString().Trim(); if (!string.IsNullOrEmpty(col8)) { if (col8.Length > 50) { result += (i + 2).ToString() + "," + "身份证号" + "," + "[" + col8 + "]错误!" + "|"; } if (TestApplicationService.GetTestApplicationByTestDateAndIdentityCard(this.CurrUser.LoginProjectId, col3, col8) != null) { result += (i + 2).ToString() + "," + "身份证号" + "," + "[" + col8 + "]已存在!" + "|"; } if (!IDCardValid.CheckIDCard(col8)) { result += (i + 2).ToString() + "," + "身份证号" + "," + "[" + col8 + "]非法!" + "|"; } } else { result += (i + 2).ToString() + "," + "身份证号" + "," + "此项为必填项!" + "|"; } string col10 = pds.Rows[i][10].ToString().Trim(); if (!string.IsNullOrEmpty(col10)) { var weldMethod = weldingMethods.FirstOrDefault(e => e.WeldingMethodCode == col10); if (weldMethod == null) { result += (i + 2).ToString() + "," + "焊接方法" + "," + "[" + col10 + "]错误!" + "|"; } } string col11 = pds.Rows[i][11].ToString().Trim(); if (!string.IsNullOrEmpty(col11)) { if (col11 != "300×125×6mm" && col11 != "300×125×8mm" && col11 != "300×125×12mm" && col11 != "Ф22×3×125mm" && col11 != "Ф60.3×5.6×125mm" && col11 != "Ф89×7×125mm" && col11 != "Ф114×7×125mm") { result += (i + 2).ToString() + "," + "焊接试件尺寸" + "," + "[" + col11 + "]错误!" + "|"; } } string col12 = pds.Rows[i][12].ToString().Trim(); if (!string.IsNullOrEmpty(col12)) { if (col12 != "FeII" && col12 != "FeIII" && col12 != "FeIV" && col12 != "Ni" && col12 != "Al") { result += (i + 2).ToString() + "," + "材质类别" + "," + "[" + col12 + "]错误!" + "|"; } } string col16 = pds.Rows[i][16].ToString().Trim(); if (!string.IsNullOrEmpty(col16)) { try { decimal d = Convert.ToDecimal(col16); } catch (Exception) { result += (i + 2).ToString() + "," + "一次考试" + "," + "[" + col16 + "]错误!" + "|"; } } string col17 = pds.Rows[i][17].ToString().Trim(); if (!string.IsNullOrEmpty(col17)) { try { decimal d = Convert.ToDecimal(col17); } catch (Exception) { result += (i + 2).ToString() + "," + "二次考试" + "," + "[" + col17 + "]错误!" + "|"; } } string col18 = pds.Rows[i][18].ToString().Trim(); if (!string.IsNullOrEmpty(col18)) { try { decimal d = Convert.ToDecimal(col18); } catch (Exception) { result += (i + 2).ToString() + "," + "增项考试1" + "," + "[" + col18 + "]错误!" + "|"; } } string col19 = pds.Rows[i][19].ToString().Trim(); if (!string.IsNullOrEmpty(col19)) { try { decimal d = Convert.ToDecimal(col19); } catch (Exception) { result += (i + 2).ToString() + "," + "增项考试2" + "," + "[" + col19 + "]错误!" + "|"; } } string col20 = pds.Rows[i][20].ToString().Trim(); if (!string.IsNullOrEmpty(col20)) { if (col20 != "待考试" && col20 != "合格在岗" && col20 != "合格离岗" && col20 != "考试不合格退场") { result += (i + 2).ToString() + "," + "焊工状态" + "," + "[" + col20 + "]错误!" + "|"; } } else { result += (i + 2).ToString() + "," + "焊工状态" + "," + "此项为必填项!" + "|"; } string col21 = pds.Rows[i][21].ToString().Trim(); if (!string.IsNullOrEmpty(col21)) { try { DateTime d = Convert.ToDateTime(col21); } catch (Exception) { result += (i + 2).ToString() + "," + "日期" + "," + "[" + col21 + "]错误!" + "|"; } } string col23 = pds.Rows[i][23].ToString().Trim(); if (!string.IsNullOrEmpty(col23)) { var area = workAreas.FirstOrDefault(e => e.WorkAreaCode == col23); if (area == null) { result += (i + 2).ToString() + "," + "负责装置区域" + "," + "[" + col23 + "]错误!" + "|"; } } string col24 = pds.Rows[i][24].ToString().Trim(); if (!string.IsNullOrEmpty(col24)) { try { DateTime d = Convert.ToDateTime(col24); } catch (Exception) { result += (i + 2).ToString() + "," + "焊工考试委托日期" + "," + "[" + col24 + "]错误!" + "|"; } } string col26 = pds.Rows[i][26].ToString().Trim(); if (!string.IsNullOrEmpty(col26)) { try { DateTime d = Convert.ToDateTime(col26); } catch (Exception) { result += (i + 2).ToString() + "," + "检测日期" + "," + "[" + col26 + "]错误!" + "|"; } } string col28 = pds.Rows[i][28].ToString().Trim(); if (!string.IsNullOrEmpty(col28)) { try { int d = Convert.ToInt32(col28); } catch (Exception) { result += (i + 2).ToString() + "," + "无损检测拍片数量" + "," + "[" + col28 + "]错误!" + "|"; } } string col29 = pds.Rows[i][29].ToString().Trim(); if (!string.IsNullOrEmpty(col29)) { var unit = checkUnits.FirstOrDefault(e => e.UnitName == col29); if (unit == null) { result += (i + 2).ToString() + "," + "检测单位" + "," + "[" + col29 + "]错误!" + "|"; } } } if (!string.IsNullOrEmpty(result)) { result = result.Substring(0, result.LastIndexOf("|")); } errorInfos.Clear(); if (!string.IsNullOrEmpty(result)) { string results = result; List errorInfoList = results.Split('|').ToList(); foreach (var item in errorInfoList) { string[] errors = item.Split(','); Model.ErrorInfo errorInfo = new Model.ErrorInfo(); errorInfo.Row = errors[0]; errorInfo.Column = errors[1]; errorInfo.Reason = errors[2]; errorInfos.Add(errorInfo); } if (errorInfos.Count > 0) { Grid1.DataSource = errorInfos; Grid1.DataBind(); } } else { ShowNotify("审核完成,请点击保存!", MessageBoxIcon.Success); } } else { throw new Exception("导入数据为空!"); } return true; } #endregion #region 导入 /// /// 导入 /// /// /// protected void btnSave_Click(object sender, EventArgs e) { if (errorInfos.Count <= 0) { string rootPath = Server.MapPath("~/"); string initFullPath = rootPath + initPath; if (!Directory.Exists(initFullPath)) { Directory.CreateDirectory(initFullPath); } string filePath = initFullPath + this.hdfileName.Text; ImportXlsToData2(filePath); } else { Alert.ShowInTop("请先将错误数据修正,再重新导入保存!", MessageBoxIcon.Warning); } } #region 读Excel提取数据 /// /// 从Excel提取数据--》Dataset /// /// Excel文件路径名 private void ImportXlsToData2(string fileName) { try { string oleDBConnString = String.Empty; oleDBConnString = "Provider=Microsoft.Jet.OLEDB.4.0;"; oleDBConnString += "Data Source="; oleDBConnString += fileName; oleDBConnString += ";Extended Properties=Excel 8.0;"; OleDbConnection oleDBConn = null; OleDbDataAdapter oleAdMaster = null; DataTable m_tableName = new DataTable(); DataSet ds = new DataSet(); oleDBConn = new OleDbConnection(oleDBConnString); oleDBConn.Open(); m_tableName = oleDBConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null); if (m_tableName != null && m_tableName.Rows.Count > 0) { m_tableName.TableName = m_tableName.Rows[0]["TABLE_NAME"].ToString().Trim(); } string sqlMaster; sqlMaster = " SELECT * FROM [" + m_tableName.TableName + "]"; oleAdMaster = new OleDbDataAdapter(sqlMaster, oleDBConn); oleAdMaster.SelectCommand.CommandTimeout = 1200; oleAdMaster.Fill(ds, "m_tableName"); oleAdMaster.Dispose(); oleDBConn.Close(); oleDBConn.Dispose(); AddDatasetToSQL2(ds.Tables[0]); } catch (Exception ex) { throw ex; } } #endregion /// /// 将Dataset的数据导入数据库 /// /// 数据集 /// 数据集列数 /// private bool AddDatasetToSQL2(DataTable pds) { string result = string.Empty; var conUnits = from x in Funs.DB.Base_Unit join y in Funs.DB.Project_ProjectUnit on x.UnitId equals y.UnitId where y.ProjectId == this.CurrUser.LoginProjectId && y.UnitType == BLL.Const.ProjectUnitType_2 select x;//施工单位 var checkUnits = from x in Funs.DB.Base_Unit join y in Funs.DB.Project_ProjectUnit on x.UnitId equals y.UnitId where y.ProjectId == this.CurrUser.LoginProjectId && y.UnitType == BLL.Const.ProjectUnitType_5 select x;//检测单位 var users = from x in Funs.DB.Sys_User join y in Funs.DB.Project_ProjectUser on x.UserId equals y.UserId where y.ProjectId == this.CurrUser.LoginProjectId && y.RoleId.Contains(BLL.Const.SubWeldingEngineer) select x; //分包商焊接工程师 var workAreas = from x in Funs.DB.ProjectData_WorkArea where x.ProjectId == this.CurrUser.LoginProjectId select x; var weldingMethods = from x in Funs.DB.Base_WeldingMethod select x; foreach (DataRow row in pds.Rows) { var oldTestApplication = BLL.TestApplicationService.GetTestApplicationByTestDateAndIdentityCard(this.CurrUser.LoginProjectId,row[3].ToString().Trim(), row[7].ToString().Trim()); if (oldTestApplication == null) { Model.Welder_TestApplication testApplication = new Model.Welder_TestApplication(); testApplication.ApplicationTestId = Guid.NewGuid().ToString(); testApplication.ProjectId = this.CurrUser.LoginProjectId; string col0 = row[0].ToString().Trim(); string col1 = row[1].ToString().Trim(); string col2 = row[2].ToString().Trim(); string col3 = row[3].ToString().Trim(); string col4 = row[4].ToString().Trim(); string col5 = row[5].ToString().Trim(); string col6 = row[6].ToString().Trim(); string col7 = row[7].ToString().Trim(); string col8 = row[8].ToString().Trim(); string col9 = row[9].ToString().Trim(); string col10 = row[10].ToString().Trim(); string col11 = row[11].ToString().Trim(); string col12 = row[12].ToString().Trim(); string col13 = row[13].ToString().Trim(); string col14 = row[14].ToString().Trim(); string col15 = row[15].ToString().Trim(); string col16 = row[16].ToString().Trim(); string col17 = row[17].ToString().Trim(); string col18 = row[18].ToString().Trim(); string col19 = row[19].ToString().Trim(); string col20 = row[20].ToString().Trim(); string col21 = row[21].ToString().Trim(); string col22 = row[22].ToString().Trim(); string col23 = row[23].ToString().Trim(); string col24 = row[24].ToString().Trim(); string col25 = row[25].ToString().Trim(); string col26 = row[26].ToString().Trim(); string col27 = row[27].ToString().Trim(); string col28 = row[28].ToString().Trim(); string col29 = row[29].ToString().Trim(); var unit = conUnits.FirstOrDefault(e => e.UnitName == col0); if (unit != null) { testApplication.ConUnit = unit.UnitId; } var user = users.FirstOrDefault(e => e.UserName == col1); if (user != null) { testApplication.ConUnitWeldingEngineer = user.UserId; } testApplication.CertificateValidity = Funs.GetNewDateTime(col2); testApplication.PreTestDate = Funs.GetNewDateTime(col3); testApplication.OwnerNum = col4; testApplication.WelderClass = col5; testApplication.WelderCode = col6; testApplication.WelderName = col7; testApplication.IdentityCard = col8; testApplication.WeldingPosition = col9; var weldMethod = weldingMethods.FirstOrDefault(e => e.WeldingMethodCode == col10); if (weldMethod != null) { testApplication.WeldingMethod = weldMethod.WeldingMethodId; } testApplication.SpecimenSize = col11; testApplication.MaterialCategory = col12; testApplication.AdditionalTestCategories1 = col13; testApplication.AdditionalTestCategories2 = col14; testApplication.AppearanceEvaluation = col15; testApplication.FirstExamination = Funs.GetNewDecimal(col16); testApplication.SecondExamination = Funs.GetNewDecimal(col17); testApplication.AdditionalTestScore1 = Funs.GetNewDecimal(col18); testApplication.AdditionalTestScore2 = Funs.GetNewDecimal(col19); testApplication.WelderCondition = col20; testApplication.ExamDate = Funs.GetNewDateTime(col21); testApplication.InvigilatorConfirmed = col22; var area = workAreas.FirstOrDefault(e => e.WorkAreaCode == col23); if (area != null) { testApplication.Area = area.WorkAreaId; } testApplication.ExamTrustDate = Funs.GetNewDateTime(col24); testApplication.ExamTrustCode = col25; testApplication.CheckDate = Funs.GetNewDateTime(col26); testApplication.CheckResult = col27; testApplication.NDTFilm = Funs.GetNewInt(col28); var checkUnit = checkUnits.FirstOrDefault(e => e.UnitName == col29); if (checkUnit != null) { testApplication.CheckUnit = checkUnit.UnitId; } Funs.DB.Welder_TestApplication.InsertOnSubmit(testApplication); } } Funs.DB.SubmitChanges(); ShowNotify("导入成功!", MessageBoxIcon.Success); PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference()); return true; } #endregion #region 导出错误提示 /// /// 导出错误提示 /// /// /// protected void btnOut_Click(object sender, EventArgs e) { //string strFileName = DateTime.Now.ToString("yyyyMMdd-hhmmss"); //System.Web.HttpContext HC = System.Web.HttpContext.Current; //HC.Response.Clear(); //HC.Response.Buffer = true; //HC.Response.ContentEncoding = System.Text.Encoding.UTF8;//设置输出流为简体中文 ////---导出为Excel文件 //HC.Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(strFileName, System.Text.Encoding.UTF8) + ".xls"); //HC.Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。 //System.IO.StringWriter sw = new System.IO.StringWriter(); //System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(sw); //this.gvErrorInfo.RenderControl(htw); //HC.Response.Write(sw.ToString()); //HC.Response.End(); } /// /// 重载VerifyRenderingInServerForm方法,否则运行的时候会出现如下错误提示:“类型“GridView”的控件“GridView1”必须放在具有 runat=server 的窗体标记内” /// /// public override void VerifyRenderingInServerForm(Control control) { } #endregion #region 下载模板 /// /// 下载模板按钮 /// /// /// protected void btnDownLoad_Click(object sender, EventArgs e) { PageContext.RegisterStartupScript(Confirm.GetShowReference("确定下载导入模板吗?", String.Empty, MessageBoxIcon.Question, PageManager1.GetCustomEventReference(false, "Confirm_OK"), PageManager1.GetCustomEventReference("Confirm_Cancel"))); } /// /// 下载导入模板 /// /// /// protected void PageManager1_CustomEvent(object sender, CustomEventArgs e) { if (e.EventArgument == "Confirm_OK") { string rootPath = Server.MapPath("~/"); string uploadfilepath = rootPath + Const.HJGL_TestApplicationTemplateUrl; string filePath = Const.HJGL_TestApplicationTemplateUrl; string fileName = Path.GetFileName(filePath); FileInfo info = new FileInfo(uploadfilepath); long fileSize = info.Length; Response.ClearContent(); Response.AddHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8)); Response.ContentType = "excel/plain"; Response.ContentEncoding = System.Text.Encoding.UTF8; Response.AddHeader("Content-Length", fileSize.ToString().Trim()); Response.TransmitFile(uploadfilepath, 0, fileSize); Response.End(); } } #endregion } }