using System; using System.Data; using System.IO; using NPOI.SS.UserModel; namespace FineUIPro.Web.HSSE.HazardousMG { /// /// 危大、超危工程导入页面 /// public partial class HazardProjectDataIn : PageBase { /// /// 上传预设的虚拟路径 /// private string initPath = BLL.Const.ExcelUrl; /// /// 加载页面 /// protected void Page_Load(object sender, EventArgs e) { } #region 模板下载 /// /// 下载导入模板 /// protected void btnTemplate_Click(object sender, EventArgs e) { string fileName = "危大超危工程导入模板.xls"; string rootPath = Server.MapPath("~/"); string fullPath = rootPath + initPath; if (!Directory.Exists(fullPath)) { Directory.CreateDirectory(fullPath); } string filePath = fullPath + fileName; BLL.Common.NPOIExcel excel = new BLL.Common.NPOIExcel(); string[] cols = { "分部分项工程", "施工内容", "风险等级", "开工时间", "结束时间", "现场负责人", "备注" }; short[] widths = { 30, 40, 16, 16, 16, 16, 30 }; ////表头样式:白字加粗、蓝底、带边框 IFont headerFont = excel.CreateFont(); headerFont.Boldweight = 700; headerFont.Color = BLL.Common.NPOIColor.WHITE; ICellStyle headerStyle = excel.CreateCellStyle(); headerStyle.SetFont(headerFont); headerStyle.FillForegroundColor = BLL.Common.NPOIColor.ROYAL_BLUE; headerStyle.FillPattern = FillPattern.SolidForeground; headerStyle.BorderTop = BorderStyle.Thin; headerStyle.BorderBottom = BorderStyle.Thin; headerStyle.BorderLeft = BorderStyle.Thin; headerStyle.BorderRight = BorderStyle.Thin; for (int i = 0; i < cols.Length; i++) { excel.SetValue(0, i, cols[i]); excel.SetStyle(0, i, headerStyle); excel.SetColumnWidth(i, widths[i]); } excel.SetRowHeight(0, 22); ////开工时间、结束时间列设为日期格式(Excel 中显示为日期) short dateFormat = excel.ActiveSheet.Workbook.CreateDataFormat().GetFormat("yyyy-mm-dd"); ICellStyle dateStyle = excel.CreateCellStyle(); dateStyle.DataFormat = dateFormat; excel.ActiveSheet.SetDefaultColumnStyle(3, dateStyle); excel.ActiveSheet.SetDefaultColumnStyle(4, dateStyle); ////风险等级下拉(第3列,数据行 1-500) excel.SetValueRange(1, 500, 2, 2, new string[] { "危大工程", "超危工程" }, "请选择:危大工程 或 超危工程"); excel.Save(filePath); FileInfo info = new FileInfo(filePath); long fileSize = info.Length; Response.Clear(); Response.ContentType = "application/vnd.ms-excel"; Response.AddHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8)); Response.AddHeader("Content-Length", fileSize.ToString()); Response.TransmitFile(filePath, 0, fileSize); Response.Flush(); Response.End(); } #endregion #region 导入 /// /// 导入 /// protected void btnImport_Click(object sender, EventArgs e) { try { this.txtError.Hidden = true; this.txtError.Text = string.Empty; if (string.IsNullOrEmpty(this.CurrUser.LoginProjectId)) { Alert.ShowInTop("请先选择项目!", MessageBoxIcon.Warning); return; } if (this.FileExcel.HasFile == false) { Alert.ShowInTop("请您选择Excel文件!", MessageBoxIcon.Warning); return; } string ext = Path.GetExtension(FileExcel.FileName).ToLower(); if (ext != ".xls") { Alert.ShowInTop("请选择.xls格式的Excel文件!", MessageBoxIcon.Warning); return; } string rootPath = Server.MapPath("~/"); string fullPath = rootPath + initPath; if (!Directory.Exists(fullPath)) { Directory.CreateDirectory(fullPath); } string filePath = fullPath + BLL.Funs.GetNewFileName() + ext; if (FileExcel.PostedFile == null || FileExcel.PostedFile.ContentLength == 0) { Alert.ShowInTop("文件上传失败,请重新选择文件!", MessageBoxIcon.Warning); return; } FileExcel.PostedFile.SaveAs(filePath); DataTable dt = BLL.Common.NPOIHelper.ExcelToDataTable1(filePath); if (dt == null || dt.Rows.Count == 0) { Alert.ShowInTop("导入数据为空!", MessageBoxIcon.Warning); return; } int success = 0; foreach (DataRow row in dt.Rows) { string projectName = GetCell(row, 0); if (string.IsNullOrEmpty(projectName)) { continue; } Model.HSSE_HazardProject info = new Model.HSSE_HazardProject { HazardProjectId = Guid.NewGuid().ToString(), ProjectId = this.CurrUser.LoginProjectId, ProjectCode = BLL.HazardProjectService.GetNewCode(), ProjectName = projectName, WorkContent = GetCell(row, 1), RiskLevel = GetCell(row, 2), StartDate = ParseDate(GetCell(row, 3)), EndDate = ParseDate(GetCell(row, 4)), ManagerMan = GetCell(row, 5), Remark = GetCell(row, 6) }; BLL.HazardProjectService.Add(info); success++; } ShowNotify("导入成功,共导入 " + success + " 条数据!", MessageBoxIcon.Success); PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference()); } catch (Exception ex) { this.txtError.Hidden = false; this.txtError.Text = ex.ToString(); } } /// /// 安全获取单元格值 /// private string GetCell(DataRow row, int index) { if (row.Table.Columns.Count > index && row[index] != null && row[index] != DBNull.Value) { return row[index].ToString().Trim(); } return string.Empty; } /// /// 解析日期(兼容文本日期与Excel数值日期) /// private DateTime? ParseDate(string val) { if (string.IsNullOrEmpty(val)) { return null; } DateTime dt; if (DateTime.TryParse(val, out dt)) { return dt; } double oa; if (double.TryParse(val, out oa) && oa > 0) { try { return DateTime.FromOADate(oa); } catch (Exception) { } } return null; } #endregion } }