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.WebControls; namespace FineUIPro.Web.HJGL.WeldingManage { public partial class CheckManageDataIn : PageBase { #region 定义变量 /// /// 上传预设的虚拟路径 /// private string initPath = Const.ExcelUrl; /// /// 导入模版文件原始的虚拟路径 /// private string initTemplatePath = Const.CheckManageDataInTemplateUrl; /// /// 错误集合 /// 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 imgbtnUpload_Click(object sender, EventArgs e) { string uploadfilepath = Server.MapPath("~/") + initTemplatePath; string fileName = Path.GetFileName(initTemplatePath); FileInfo info = new FileInfo(uploadfilepath); if (info.Exists) { long fileSize = info.Length; Response.Clear(); Response.ContentType = "application/x-zip-compressed"; Response.AddHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8)); Response.AddHeader("Content-Length", fileSize.ToString()); Response.TransmitFile(uploadfilepath, 0, fileSize); Response.Flush(); Response.Close(); } else { ShowNotify("文件不存在!", MessageBoxIcon.Warning); } } #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], 27); } catch (Exception ex) { throw ex; } } #endregion #endregion /// /// 将Dataset的数据导入数据库 /// /// 数据集 /// 数据集列数 /// private bool AddDatasetToSQL(DataTable pds, int Cols) { string result = string.Empty; int ic, ir; ic = pds.Columns.Count; if (ic < Cols) { throw new Exception("导入Excel格式错误!Excel只有" + ic.ToString().Trim() + "列"); } ir = pds.Rows.Count; if (pds != null && ir > 0) { for (int i = 0; i < ir; i++) { if(string.IsNullOrEmpty( pds.Rows[i][0].ToString())) { result += (i + 2).ToString() + "," + "委托单位" + "," + "此项为必填项!" + "|"; } if (string.IsNullOrEmpty(pds.Rows[i][1].ToString())) { result += (i + 2).ToString() + "," + "单位名称" + "," + "此项为必填项!" + "|"; } if (string.IsNullOrEmpty(pds.Rows[i][2].ToString())) { result += (i + 2).ToString() + "," + "装置代号" + "," + "此项为必填项!" + "|"; } if (string.IsNullOrEmpty(pds.Rows[i][3].ToString())) { result += (i + 2).ToString() + "," + "区域代号" + "," + "此项为必填项!" + "|"; } if (string.IsNullOrEmpty(pds.Rows[i][4].ToString())) { result += (i + 2).ToString() + "," + "委托编号" + "," + "此项为必填项!" + "|"; } if (string.IsNullOrEmpty(pds.Rows[i][5].ToString())) { result += (i + 2).ToString() + "," + "委托日期" + "," + "此项为必填项!" + "|"; } if (string.IsNullOrEmpty(pds.Rows[i][6].ToString())) { result += (i + 2).ToString() + "," + "检件名称" + "," + "此项为必填项!" + "|"; } if (string.IsNullOrEmpty(pds.Rows[i][7].ToString())) { result += (i + 2).ToString() + "," + "检测批号" + "," + "此项为必填项!" + "|"; } if (string.IsNullOrEmpty(pds.Rows[i][8].ToString())) { result += (i + 2).ToString() + "," + "检测时机" + "," + "此项为必填项!" + "|"; } if (string.IsNullOrEmpty(pds.Rows[i][9].ToString())) { result += (i + 2).ToString() + "," + "检件编号(管线号)" + "," + "此项为必填项!" + "|"; } if (string.IsNullOrEmpty(pds.Rows[i][10].ToString())) { result += (i + 2).ToString() + "," + "焊口编号" + "," + "此项为必填项!" + "|"; } if (string.IsNullOrEmpty(pds.Rows[i][11].ToString())) { result += (i + 2).ToString() + "," + "焊工号" + "," + "此项为必填项!" + "|"; } if (string.IsNullOrEmpty(pds.Rows[i][12].ToString())) { result += (i + 2).ToString() + "," + "检件规格" + "," + "此项为必填项!" + "|"; } if (string.IsNullOrEmpty(pds.Rows[i][13].ToString())) { result += (i + 2).ToString() + "," + "检件材质" + "," + "此项为必填项!" + "|"; } if (string.IsNullOrEmpty(pds.Rows[i][14].ToString())) { result += (i + 2).ToString() + "," + "比列(%)" + "," + "此项为必填项!" + "|"; } if (string.IsNullOrEmpty(pds.Rows[i][15].ToString())) { result += (i + 2).ToString() + "," + "焊接方法" + "," + "此项为必填项!" + "|"; } if (string.IsNullOrEmpty(pds.Rows[i][16].ToString())) { result += (i + 2).ToString() + "," + "工作量米" + "," + "此项为必填项!" + "|"; } if (string.IsNullOrEmpty(pds.Rows[i][17].ToString())) { result += (i + 2).ToString() + "," + "工作量道" + "," + "此项为必填项!" + "|"; } if (string.IsNullOrEmpty(pds.Rows[i][18].ToString())) { result += (i + 2).ToString() + "," + "级别" + "," + "此项为必填项!" + "|"; } if (string.IsNullOrEmpty(pds.Rows[i][19].ToString())) { result += (i + 2).ToString() + "," + "检测日期" + "," + "此项为必填项!" + "|"; } if (string.IsNullOrEmpty(pds.Rows[i][20].ToString())) { result += (i + 2).ToString() + "," + "检测温度" + "," + "此项为必填项!" + "|"; } if (string.IsNullOrEmpty(pds.Rows[i][21].ToString())) { result += (i + 2).ToString() + "," + "检测人" + "," + "此项为必填项!" + "|"; } if (string.IsNullOrEmpty(pds.Rows[i][24].ToString())) { result += (i + 2).ToString() + "," + "结果(合格/不合格)" + "," + "此项为必填项!" + "|"; } if (string.IsNullOrEmpty(pds.Rows[i][27].ToString())) { result += (i + 2).ToString() + "," + "报告编号" + "," + "此项为必填项!" + "|"; } if (string.IsNullOrEmpty(pds.Rows[i][28].ToString())) { result += (i + 2).ToString() + "," + "报告日期" + "," + "此项为必填项!" + "|"; } if (string.IsNullOrEmpty(pds.Rows[i][31].ToString())) { result += (i + 2).ToString() + "," + "检测方法(PT/RT/MT/UT/设备PT/设备RT/设备MT/设备TU/TOFD)" + "," + "此项为必填项!" + "|"; } //a++; } 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; } #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], 27); } catch (Exception ex) { throw ex; } } #endregion /// /// 将Dataset的数据导入数据库 /// /// 数据集 /// 数据集列数 /// private bool AddDatasetToSQL2(DataTable pds, int Cols) { string result = string.Empty; string dReportID = string.Empty; string dateStr = string.Empty; int ic, ir; ic = pds.Columns.Count; if (ic < Cols) { throw new Exception("导入Excel格式错误!Excel只有" + ic.ToString().Trim() + "列"); } List checkStaticList = new List(); ir = pds.Rows.Count; if (pds != null && ir > 0) { for (int i = 0; i < ir; i++) { Model.CH_CheckStatic checkStatic = new Model.CH_CheckStatic(); checkStatic.CheckStaticId = Guid.NewGuid().ToString(); checkStatic.ProjectId = this.CurrUser.LoginProjectId; checkStatic.TrustUnitName = pds.Rows[i][0].ToString();//委托单位 checkStatic.UnitName = pds.Rows[i][1].ToString();//单位名称 checkStatic.InstallationName = pds.Rows[i][2].ToString();//装置代号 checkStatic.WorkAreaCode = pds.Rows[i][3].ToString();//区域代号 checkStatic.CH_TrustCode = pds.Rows[i][4].ToString(); //委托编号 checkStatic.Trust_Date = DateUtil.ToDateTime(pds.Rows[i][5].ToString());//委托日期 checkStatic.CheckThingName = pds.Rows[i][6].ToString();//检件名称 checkStatic.DefectCode = pds.Rows[i][7].ToString();//检测批号 checkStatic.CheckOpportunity = pds.Rows[i][8].ToString();//检测时机 checkStatic.ISO_IsoNo = pds.Rows[i][9].ToString();//管线号 checkStatic.JOT_JointNo = pds.Rows[i][10].ToString();//焊口号 checkStatic.WED_Code = pds.Rows[i][11].ToString();//焊工号 checkStatic.JOT_JointDesc = pds.Rows[i][12].ToString();//检件规格 checkStatic.MaterialCode = pds.Rows[i][13].ToString();//检件材质 checkStatic.DetectionRateCode = pds.Rows[i][14].ToString();//比例(%) checkStatic.WeldingMethodCode = pds.Rows[i][15].ToString();//焊接方法 checkStatic.WorkAmout1 = pds.Rows[i][16].ToString();//工作量米 checkStatic.WorkAmout2 = pds.Rows[i][17].ToString();//工作量道 checkStatic.CheckLevel = pds.Rows[i][18].ToString();//级别 checkStatic.CheckDate = DateUtil.ToDateTime(pds.Rows[i][19].ToString());//检测日期 checkStatic.CheckTemperature = pds.Rows[i][20].ToString();//检测温度 checkStatic.CheckPerson = pds.Rows[i][21].ToString();//检测人 checkStatic.DefectType = pds.Rows[i][22].ToString();//缺陷性质(若无为空) checkStatic.DefectNum = pds.Rows[i][23].ToString();//缺陷定量(若无为空) checkStatic.CheckResult = pds.Rows[i][24].ToString();//结果(合格/不合格) checkStatic.CheckResultNum = pds.Rows[i][25].ToString();//结果单编号 checkStatic.CheckResultDate = DateUtil.ToDateTime(pds.Rows[i][26].ToString());//结果单日期 checkStatic.ReportNo = pds.Rows[i][27].ToString();//报告编号 checkStatic.ReportDate = DateUtil.ToDateTime(pds.Rows[i][28].ToString());//报告日期 checkStatic.ReportAmount = pds.Rows[i][29].ToString();//工作量 checkStatic.ProgressMoney = pds.Rows[i][30].ToString();//工程进度款 checkStatic.DetectionTypeCode = pds.Rows[i][31].ToString();//检测方法(PT/RT/MT/UT/设备PT/设备RT/设备MT/设备TU/TOFD) checkStatic.Remark = pds.Rows[i][32].ToString();//备注 checkStaticList.Add(checkStatic); } Funs.DB.CH_CheckStatic.InsertAllOnSubmit(checkStaticList); Funs.DB.SubmitChanges(); ShowNotify("导入成功!", MessageBoxIcon.Success); PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference()); } else { throw new Exception("导入数据为空!"); } return true; } #endregion } }