using BLL; using MiniExcelLibs; using Model; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace FineUIPro.Web.PHTGL.Filing { public partial class ContractStandingBookIn : PageBase { #region 定义变量 /// /// 上传预设的虚拟路径 /// private string initPath = Const.ExcelUrl; /// /// 安装组件集合 /// public static List ContractStandingBookList = new List(); /// /// 错误集合 /// public static string errorInfos = string.Empty; public string Type { get { return (string)ViewState["Type"]; } set { ViewState["Type"] = value; } } #endregion #region 加载 /// /// 加载页面 /// /// /// protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { this.hdFileName.Text = string.Empty; if (ContractStandingBookList != null) { ContractStandingBookList.Clear(); } errorInfos = string.Empty; Type = Request.Params["Type"]; } } #endregion #region 审核 /// /// 审核 /// /// /// protected void btnAudit_Click(object sender, EventArgs e) { try { Grid1.DataSource = null; Grid1.DataBind(); if (this.fuAttachUrl.HasFile == false) { ShowNotify("请您选择Excel文件!", MessageBoxIcon.Warning); return; } string IsXls = Path.GetExtension(this.fuAttachUrl.FileName).ToString().Trim().ToLower(); if (IsXls != ".xlsx") { ShowNotify("只可以选择Excel文件!", MessageBoxIcon.Warning); return; } if (ContractStandingBookList != null) { ContractStandingBookList.Clear(); } if (!string.IsNullOrEmpty(errorInfos)) { errorInfos = string.Empty; } 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; this.fuAttachUrl.PostedFile.SaveAs(filePath); ImportXlsToData(rootPath + initPath + this.hdFileName.Text); if (string.IsNullOrEmpty(errorInfos)) { if (!string.IsNullOrEmpty(this.hdFileName.Text)) { if (ContractStandingBookList.Count > 0) { this.Grid1.Hidden = false; this.Grid1.DataIDField = "ContractId"; this.Grid1.DataSource = ContractStandingBookList; this.Grid1.DataBind(); Grid1.RecordCount = ContractStandingBookList.Count; } } else { ShowNotify("请先审核要导入的文件!", MessageBoxIcon.Warning); } } else { ShowNotify("请先将错误数据修正,再重新导入提交!", MessageBoxIcon.Warning); } } catch (Exception ex) { Alert alert = new Alert { Message = "'" + ex.Message + "'", Target = Target.Self }; alert.Show(); //ShowNotify("'" + ex.Message + "'", MessageBoxIcon.Warning); } } #region 读Excel提取数据 /// /// 从Excel提取数据--》Dataset /// /// Excel文件路径名 private void ImportXlsToData(string fileName) { // var rows =Funs.LINQToDataTable(MiniExcel.Query(fileName).ToList()) ; var ds = MiniExcel.Query(fileName).ToList(); var columns = MiniExcel.GetColumns(fileName); var rows = MiniExcel.Query(fileName).ToList(); var cnt = columns.Count; var reposedata = AddDatasetToSQL(rows, cnt); if (reposedata.code == 1) { ShowNotify("审核完成,请点击导入!", MessageBoxIcon.Success); } else { Alert alert = new Alert { Message = reposedata.message, Target = Target.Self }; alert.Show(); //ShowNotify(responeData.message, MessageBoxIcon.Success); } } #endregion #region 将Dataset的数据导入数据库 /// /// 将Dataset的数据导入数据库 /// /// 数据集 /// 数据集行数 /// private Model.ResponeData AddDatasetToSQL(List pds, int count) { Model.ResponeData responeData = new Model.ResponeData(); // string result = string.Empty; List result = new List(); //pds = BLL.Funs.FilterBlankLines(pds); if (count < 30) { responeData.code = 0; responeData.message = "导入Excel格式错误!Excel只有" + count.ToString().Trim() + "列"; return responeData; } if (pds.Count > 0 && pds != null) { foreach (var item in pds) { Model.PHTGL_ContractStandingBook _ContractStandingBook = new PHTGL_ContractStandingBook(); _ContractStandingBook.ContractId = SQLHelper.GetNewID(); _ContractStandingBook.UnitId = this.CurrUser.UnitId; _ContractStandingBook.States = 0; _ContractStandingBook.DepartId = this.CurrUser.DepartId; _ContractStandingBook.Agent = this.CurrUser.PersonName; _ContractStandingBook.CreatUser = this.CurrUser.PersonId; _ContractStandingBook.CreateDate = DateTime.Now.ToString() ; if (Type == "1") { _ContractStandingBook.ProjectId = this.CurrUser.LoginProjectId; } _ContractStandingBook.ContractName = item.ContractName; _ContractStandingBook.ContractNum= item.ContractNum; _ContractStandingBook.ProjectName= item.ProjectName; _ContractStandingBook.ProjectCode= item.ProjectCode; _ContractStandingBook.Parties= item.Parties; _ContractStandingBook.Currency= item.Currency; _ContractStandingBook.ContractAmount= item.ContractAmount; _ContractStandingBook.ContractType= item.ContractType; _ContractStandingBook.EPCCode= item.EPCCode; _ContractStandingBook.ContractAttribute= item.ContractAttribute; _ContractStandingBook.OpeningBank_TT = item.OpeningBankTT; _ContractStandingBook.OpeningBank_Electrophore = item.OpeningBankelectrophore; _ContractStandingBook.BankAccount_TT = item.BankAccountTT; _ContractStandingBook.BankAccount_Electrophore = item.BankAccountelectrophore; _ContractStandingBook.LineNumber_Electrophore = item.LineNumberelectrophore; _ContractStandingBook.Retentionmoney= item.Retentionmoney; _ContractStandingBook.Status= item.Status; _ContractStandingBook.ContactUnitOfPartyA= item.ContactUnitOfPartyA; _ContractStandingBook.ContactPersonOfPartyA= item.ContactPersonOfPartyA; _ContractStandingBook.ContactPersonOfPartyB= item.ContactPersonOfPartyB; _ContractStandingBook.ContactPersonPhoneOfPartyB= item.ContactPersonPhoneOfPartyB; _ContractStandingBook.ContactPersonEmailOfPartyB= item.ContactPersonEmailOfPartyB; _ContractStandingBook.ContractAmountExcludingTax= item.ContractAmountExcludingTax; _ContractStandingBook.PriceMethod= item.PriceMethod; _ContractStandingBook.SignedOnDate= item.SignedOnDate; _ContractStandingBook.ContractStartDate= item.ContractStartDate; _ContractStandingBook.ContractEndDate= item.ContractEndDate; _ContractStandingBook.PricingBasis= item.PricingBasis; _ContractStandingBook.SubcontractingMethod= item.SubcontractingMethod; _ContractStandingBook.IsItACentralizedPurchaseSupplier = item.IsItACentralizedPurchaseSupplier; ContractStandingBookList.Add(_ContractStandingBook); } if (result.Count > 0) { ContractStandingBookList.Clear(); // result = result.Substring(0, result.LastIndexOf("|")); errorInfos = string.Join("|", result.Distinct()); //Alert alert = new Alert(); //alert.Message = result; //alert.Target = Target.Self; //alert.Show(); responeData.code = 0; responeData.message = errorInfos; } else { errorInfos = string.Empty; } } else { responeData.code = 0; responeData.message = "导入数据为空!"; } return responeData; } #endregion #endregion #region 导入 /// /// 导入 /// /// /// protected void btnImport_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(errorInfos)) { if (!string.IsNullOrEmpty(this.hdFileName.Text)) { if (ContractStandingBookList.Count > 0) { this.Grid1.Hidden = false; this.Grid1.DataIDField = "ContractId"; this.Grid1.DataSource = ContractStandingBookList; this.Grid1.DataBind(); Grid1.RecordCount = ContractStandingBookList.Count; } } else { ShowNotify("请先审核要导入的文件!", MessageBoxIcon.Warning); } } else { ShowNotify("请先将错误数据修正,再重新导入提交!", MessageBoxIcon.Warning); } } #endregion #region 提交 /// /// 提交 /// /// /// protected void btnSave_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(errorInfos)) { if (ContractStandingBookList.Count > 0) { ContractStandingBookService.AddBulkPHTGL_ContractStandingBook(ContractStandingBookList); } int a = ContractStandingBookList.Count(); string rootPath = Server.MapPath("~/"); string initFullPath = rootPath + initPath; string filePath = initFullPath + this.hdFileName.Text; if (filePath != string.Empty && System.IO.File.Exists(filePath)) { System.IO.File.Delete(filePath);//删除上传的XLS文件 } ShowNotify("导入成功!", MessageBoxIcon.Success); PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference()); } else { ShowNotify("请先将错误数据修正,再重新导入提交!", MessageBoxIcon.Warning); } } #endregion #region 获取名称 public string ConvertPipelineId(object PipelineId) { string StateName = string.Empty; if (!string.IsNullOrEmpty(PipelineId.ToString())) { if (PipelineId != null) { string txt = PipelineService.GetPipelineByPipelineId(PipelineId.ToString()).PipelineCode; return txt; } return ""; } return StateName; } #endregion #region 下载模板 /// /// 下载模板按钮 /// /// /// protected void btnDownLoad_Click(object sender, EventArgs e) { string rootPath = Server.MapPath("~/"); string uploadfilepath = rootPath + Const.ContractStandingBookIn; string filePath = Const.ContractStandingBookIn; 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 #region 分页选择下拉改变事件 /// /// 分页选择下拉改变事件 /// /// /// protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e) { Grid1.PageSize = Convert.ToInt32(ddlPageSize.SelectedValue); this.Grid1.DataSource = ContractStandingBookList; this.Grid1.DataBind(); Grid1.RecordCount = ContractStandingBookList.Count; } #endregion } }