SGGL_HBAZ/SGGL/FineUIPro.Web/SYHSE/Data_HiddenDangersIn.aspx.cs

347 lines
12 KiB
C#
Raw Normal View History

2025-02-12 09:27:47 +08:00
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 Data_HiddenDangersIn : PageBase
{
#region
/// <summary>
/// 上传预设的虚拟路径
/// </summary>
private string initPath = Const.ExcelUrl;
/// <summary>
/// 导入模版文件原始的虚拟路径
/// </summary>
private string initTemplatePath = Const.SYHiddenDangerDataInTemplateUrl;
/// <summary>
/// 错误集合
/// </summary>
public static List<Model.ErrorInfo> errorInfos = new List<Model.ErrorInfo>();
#endregion
#region
/// <summary>
/// 加载页面
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (errorInfos != null)
{
errorInfos.Clear();
}
}
}
#endregion
#region
/// <summary>
/// 模板下载
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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
/// <summary>
/// 审核
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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提取数据
/// <summary>
/// 从Excel提取数据--》Dataset
/// </summary>
/// <param name="filename">Excel文件路径名</param>
private void ImportXlsToData(string fileName)
{
var dataTable = BLL.Common.NPOIHelper.ExcelToDataTable1(fileName);
AddDatasetToSQL(dataTable, 5);
}
#endregion
#endregion
/// <summary>
/// 将Dataset的数据导入数据库
/// </summary>
/// <param name="pds">数据集</param>
/// <param name="Cols">数据集列数</param>
/// <returns></returns>
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++)
{
var row0 = pds.Rows[i][0].ToString();
if (string.IsNullOrEmpty(row0))
{
result += (i + 2).ToString() + "," + "登记日期" + "," + "此项为必填项!" + "|";
}
else
{
try
{
DateTime date = Convert.ToDateTime(row0.Trim());
}
catch (Exception)
{
result += (i + 2).ToString() + "," + "登记日期" + "," + "[" + row0 + "]错误!" + "|";
}
}
if (string.IsNullOrEmpty(pds.Rows[i][1].ToString()))
{
result += (i + 2).ToString() + "," + "类别" + "," + "此项为必填项!" + "|";
}
else
{
if ( pds.Rows[i][1].ToString() != "人的不安全行为" && pds.Rows[i][1].ToString()!= "物的不安全状态" && pds.Rows[i][1].ToString() != "管理缺陷")
{
result += (i + 2).ToString() + "," + "类别" + "," + "不为人的不安全行为、物的不安全状态或管理缺陷!" + "|";
}
}
if (string.IsNullOrEmpty(pds.Rows[i][2].ToString()))
{
result += (i + 2).ToString() + "," + "状态" + "," + "此项为必填项!" + "|";
}
else
{
if (pds.Rows[i][2].ToString() != "待整改" && pds.Rows[i][2].ToString() != "待复查验收" && pds.Rows[i][2].ToString() != "已闭环")
{
result += (i + 2).ToString() + "," + "类别" + "," + "待整改、待复查验收或已闭环!" + "|";
}
}
if (string.IsNullOrEmpty(pds.Rows[i][3].ToString()))
{
result += (i + 2).ToString() + "," + "内容" + "," + "此项为必填项!" + "|";
}
}
if (!string.IsNullOrEmpty(result))
{
result = result.Substring(0, result.LastIndexOf("|"));
}
errorInfos.Clear();
if (!string.IsNullOrEmpty(result))
{
string results = result;
List<string> 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
/// <summary>
/// 导入
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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提取数据
/// <summary>
/// 从Excel提取数据--》Dataset
/// </summary>
/// <param name="filename">Excel文件路径名</param>
private void ImportXlsToData2(string fileName)
{
var dataTable = BLL.Common.NPOIHelper.ExcelToDataTable1(fileName);
AddDatasetToSQL2(dataTable, 5);
}
#endregion
/// <summary>
/// 将Dataset的数据导入数据库
/// </summary>
/// <param name="pds">数据集</param>
/// <param name="Cols">数据集列数</param>
/// <returns></returns>
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() + "列");
}
ir = pds.Rows.Count;
if (pds != null && ir > 0)
{ for (int i = 0; i < ir; i++)
{
Model.SYHSEData_HiddenDangers hiddenDangers = new Model.SYHSEData_HiddenDangers();
hiddenDangers.Id = Guid.NewGuid().ToString();
hiddenDangers.CheckDate = Funs.GetNewDateTime(pds.Rows[i][0].ToString().Trim());
hiddenDangers.Type = pds.Rows[i][1].ToString().Trim();
hiddenDangers.Status = pds.Rows[i][2].ToString().Trim();
hiddenDangers.DangerDes = pds.Rows[i][3].ToString().Trim() ;
hiddenDangers.ResponseMan = pds.Rows[i][4].ToString().Trim() ;
hiddenDangers.CreateMan = CurrUser.UserId;
hiddenDangers.CreateDate = DateTime.Now;
hiddenDangers.UnitId = CurrUser.UnitId;
hiddenDangers.ProjectId = CurrUser.LoginProjectId;
Funs.DB.SYHSEData_HiddenDangers.InsertOnSubmit(hiddenDangers);
}
Funs.DB.SubmitChanges();
ShowNotify("导入成功!", MessageBoxIcon.Success);
PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
}
else
{
throw new Exception("导入数据为空!");
}
return true;
}
#endregion
}
}