You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
286 lines
13 KiB
286 lines
13 KiB
using MESClassLibrary.BLL.BasicInfo;
|
|
using MESClassLibrary.BLL.Log;
|
|
using MESClassLibrary.EFModel;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data.OleDb;
|
|
using System.Data;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using System.Web;
|
|
using System.Web.UI;
|
|
using System.Web.UI.WebControls;
|
|
using System.IO;
|
|
using MESClassLibrary.BLL;
|
|
|
|
namespace MESWebSite.Manage
|
|
{
|
|
public partial class Bom_MK : System.Web.UI.Page
|
|
{
|
|
protected void Page_Load(object sender, EventArgs e)
|
|
{
|
|
if (Request.Cookies["LoginUserInfo"] == null)
|
|
{
|
|
Response.Write("<script language=javascript>alert('您的登录信息已过期,请重新登录!');top.location.href='../Login.aspx';</script>");
|
|
}
|
|
}
|
|
|
|
|
|
protected void ImportExcel_Click(object sender, EventArgs e)
|
|
{
|
|
try
|
|
{
|
|
if (input01.PostedFile.ContentLength > 0)
|
|
{
|
|
string strUrl = "../Excel/";
|
|
if (Directory.Exists(Server.MapPath(strUrl)) == false)
|
|
{
|
|
Directory.CreateDirectory(Server.MapPath(strUrl));
|
|
}
|
|
String fileExtension = System.IO.Path.GetExtension(input01.PostedFile.FileName).ToLower();
|
|
String fileName = DateTime.Now.ToString("yyyyMMddHHmmssfff") + fileExtension;
|
|
input01.PostedFile.SaveAs(Server.MapPath(strUrl) + fileName);
|
|
string excelPath = Server.MapPath(strUrl) + fileName;
|
|
DataTable dt = null;
|
|
|
|
#region 校验文件
|
|
|
|
try
|
|
{
|
|
dt = GetExcelTableByOleDB(excelPath, "Sheet1");
|
|
if (dt != null && dt.Rows.Count > 1)
|
|
{
|
|
|
|
}
|
|
else
|
|
{
|
|
ClientScript.RegisterStartupScript(this.GetType(), "tishi", "<script type='text/javascript'>$.messager.alert('提示', '模板内容为空', 'warning');</script>");
|
|
return;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod());
|
|
return;
|
|
}
|
|
|
|
#endregion
|
|
|
|
Bom_MKBLL bll = new Bom_MKBLL();
|
|
List<tb_Bom_MK> list = new List<tb_Bom_MK>();
|
|
tb_Bom_MK md = new tb_Bom_MK();
|
|
|
|
ProductBLL pbll = new ProductBLL();
|
|
LineBLL linebll = new LineBLL();
|
|
StationBLL stationbll = new StationBLL();
|
|
BasicBLL<tb_Bom_MK> partNoBindDB = new BasicBLL<tb_Bom_MK>();
|
|
|
|
|
|
#region 校验数据
|
|
|
|
if (dt != null && dt.Rows.Count > 0)
|
|
{
|
|
for (int i = 1; i < dt.Rows.Count; i++)
|
|
{
|
|
#region 值校验
|
|
|
|
string PartNo1 = dt.Rows[i][0].ToString();
|
|
if (PartNo1 == "")
|
|
{
|
|
ClientScript.RegisterStartupScript(this.GetType(), "tishi", "<script type='text/javascript'>$.messager.alert('提示', '第" + i.ToString() + "行,总成零件号为空,故无法导入', 'warning');</script>");
|
|
return;
|
|
}
|
|
string PartNo2 = dt.Rows[i][1].ToString();
|
|
if (PartNo2 == "")
|
|
{
|
|
ClientScript.RegisterStartupScript(this.GetType(), "tishi", "<script type='text/javascript'>$.messager.alert('提示', '第" + i.ToString() + "行,子零件号为空,故无法导入', 'warning');</script>");
|
|
return;
|
|
}
|
|
|
|
string LineName = dt.Rows[i][2].ToString();
|
|
if (LineName == "")
|
|
{
|
|
ClientScript.RegisterStartupScript(this.GetType(), "tishi", "<script type='text/javascript'>$.messager.alert('提示', '第" + i.ToString() + "行,产线为空,故无法导入', 'warning');</script>");
|
|
return;
|
|
}
|
|
|
|
string StationNo = dt.Rows[i][3].ToString();
|
|
if (StationNo == "")
|
|
{
|
|
ClientScript.RegisterStartupScript(this.GetType(), "tishi", "<script type='text/javascript'>$.messager.alert('提示', '第" + i.ToString() + "行,工位号为空,故无法导入', 'warning');</script>");
|
|
return;
|
|
}
|
|
|
|
string LeftCount = dt.Rows[i][4].ToString();
|
|
if (LeftCount == "")
|
|
{
|
|
ClientScript.RegisterStartupScript(this.GetType(), "tishi", "<script type='text/javascript'>$.messager.alert('提示', '第" + i.ToString() + "行,左侧数量为空,故无法导入', 'warning');</script>");
|
|
return;
|
|
}
|
|
|
|
string RightCount = dt.Rows[i][5].ToString();
|
|
if (RightCount == "")
|
|
{
|
|
ClientScript.RegisterStartupScript(this.GetType(), "tishi", "<script type='text/javascript'>$.messager.alert('提示', '第" + i.ToString() + "行,右侧数量为空,故无法导入', 'warning');</script>");
|
|
return;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region 逻辑校验
|
|
|
|
if (!pbll.IsExist(PartNo1))
|
|
{
|
|
ClientScript.RegisterStartupScript(this.GetType(), "提示", "<script type='text/javascript'>$.messager.alert('提示', '第" + i.ToString() + "行,总成零件号系统中不存在,故无法导入', 'warning');</script>");
|
|
return;
|
|
}
|
|
|
|
if (!pbll.IsExist(PartNo2))
|
|
{
|
|
ClientScript.RegisterStartupScript(this.GetType(), "提示", "<script type='text/javascript'>$.messager.alert('提示', '第" + i.ToString() + "行,子零件号系统中不存在,故无法导入', 'warning');</script>");
|
|
return;
|
|
}
|
|
|
|
if (!linebll.IsExist(LineName))
|
|
{
|
|
ClientScript.RegisterStartupScript(this.GetType(), "提示", "<script type='text/javascript'>$.messager.alert('提示', '第" + i.ToString() + "行,产线在系统中不存在,故无法导入', 'warning');</script>");
|
|
return;
|
|
}
|
|
|
|
if (!stationbll.IsExist(StationNo))
|
|
{
|
|
ClientScript.RegisterStartupScript(this.GetType(), "提示", "<script type='text/javascript'>$.messager.alert('提示', '第" + i.ToString() + "行,工位号在系统中不存在,故无法导入', 'warning');</script>");
|
|
return;
|
|
}
|
|
|
|
var partNo1List = partNoBindDB.Search<tb_Bom_MK>(p => p.PartNo1 == PartNo1 && p.PartNo2 == PartNo2 && p.LineName == LineName && p.StationNo == StationNo);
|
|
if (partNo1List != null && partNo1List.Count > 0)
|
|
{
|
|
ClientScript.RegisterStartupScript(this.GetType(), "提示", "<script type='text/javascript'>$.messager.alert('提示', '第" + i.ToString() + "行,BOM总成零件号与子零件号绑定关系在系统中已存在,故无法导入', 'warning');</script>");
|
|
return;
|
|
}
|
|
|
|
#endregion
|
|
|
|
var info = Request.Cookies.Get("LoginUserInfo");
|
|
if (info != null)
|
|
{
|
|
md.UserID = info["UserID"].ToUpper();
|
|
}
|
|
|
|
|
|
md.PartNo1 = PartNo1;
|
|
md.PartNo2 = PartNo2;
|
|
md.LineName = LineName;
|
|
md.StationNo = StationNo;
|
|
md.LeftCount = int.Parse(LeftCount);
|
|
md.RightCount = int.Parse(RightCount);
|
|
|
|
list.Add(md);
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region 存储数据
|
|
|
|
try
|
|
{
|
|
bool flag = false;
|
|
if (list.Count > 0)
|
|
{
|
|
foreach (var item in list)
|
|
{
|
|
flag = bll.AddInfo(item);
|
|
}
|
|
}
|
|
if (flag == true)
|
|
{
|
|
ClientScript.RegisterStartupScript(this.GetType(), "提示", "<script type='text/javascript'>$.messager.alert('提示','导入成功!', 'warning');</script>");
|
|
}
|
|
else
|
|
{
|
|
ClientScript.RegisterStartupScript(this.GetType(), "提示", "<script type='text/javascript'>$.messager.alert('提示','导入失败!', 'warning');</script>");
|
|
}
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod());
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod());
|
|
}
|
|
}
|
|
|
|
public DataTable GetExcelTableByOleDB(string strExcelPath, string tableName)
|
|
{
|
|
OleDbConnection objConn = null;
|
|
try
|
|
{
|
|
DataTable dtExcel = new DataTable();
|
|
//数据表
|
|
DataSet ds = new DataSet();
|
|
//获取文件扩展名
|
|
string strExtension = System.IO.Path.GetExtension(strExcelPath);
|
|
string strFileName = System.IO.Path.GetFileName(strExcelPath);
|
|
//Excel的连接
|
|
|
|
switch (strExtension)
|
|
{
|
|
case ".xls":
|
|
objConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strExcelPath + ";" + "Extended Properties=\"Excel 8.0;HDR=NO;IMEX=1;\"");
|
|
break;
|
|
case ".xlsx":
|
|
objConn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + strExcelPath + ";" + "Extended Properties=\"Excel 12.0;HDR=NO;IMEX=1;\"");
|
|
break;
|
|
|
|
default:
|
|
objConn = null;
|
|
break;
|
|
}
|
|
if (objConn == null)
|
|
{
|
|
return null;
|
|
}
|
|
try
|
|
{
|
|
objConn.Open();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
|
|
}
|
|
|
|
//获取Excel中所有Sheet表的信息
|
|
//System.Data.DataTable schemaTable = objConn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, null);
|
|
//获取Excel的第一个Sheet表名
|
|
//string tableName = schemaTable.Rows[0][2].ToString().Trim();
|
|
string strSql = "select * from [" + tableName + "$]";
|
|
//获取Excel指定Sheet表中的信息
|
|
OleDbCommand objCmd = new OleDbCommand(strSql, objConn);
|
|
OleDbDataAdapter myData = new OleDbDataAdapter(strSql, objConn);
|
|
myData.Fill(ds, tableName);//填充数据
|
|
objConn.Close();
|
|
//dtExcel即为excel文件中指定表中存储的信息
|
|
dtExcel = ds.Tables[tableName];
|
|
return dtExcel;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
if (objConn != null)
|
|
{
|
|
objConn.Close();
|
|
}
|
|
|
|
return null;
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|