284 lines
11 KiB
C#
284 lines
11 KiB
C#
using BLL;
|
|
using MiniExcelLibs;
|
|
using Model;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data;
|
|
using System.IO;
|
|
using System.Linq;
|
|
|
|
namespace FineUIPro.Web.HJGL.BaseInfo
|
|
{
|
|
public partial class MaterialCodeLibIn : PageBase
|
|
{
|
|
#region 定义项
|
|
/// <summary>
|
|
/// 上传预设的虚拟路径
|
|
/// </summary>
|
|
private string initPath = Const.ExcelUrl;//"File\\Excel\\DataIn\\";
|
|
|
|
/// <summary>
|
|
/// 错误集合
|
|
/// </summary>
|
|
public static string errorInfos = string.Empty;
|
|
#endregion
|
|
|
|
#region 加载
|
|
/// <summary>
|
|
/// 加载页面
|
|
/// </summary>
|
|
/// <param name="sender"></param>
|
|
/// <param name="e"></param>
|
|
protected void Page_Load(object sender, EventArgs e)
|
|
{
|
|
if (!IsPostBack)
|
|
{
|
|
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region 模板下载
|
|
/// <summary>
|
|
/// 模板下载
|
|
/// </summary>
|
|
/// <param name="sender"></param>
|
|
/// <param name="e"></param>
|
|
protected void btnDownLoad_Click(object sender, EventArgs e)
|
|
{
|
|
PageContext.RegisterStartupScript(Confirm.GetShowReference("确定要下载模板吗?", String.Empty, MessageBoxIcon.Question, PageManager1.GetCustomEventReference(false, "Confirm_OK"), PageManager1.GetCustomEventReference("Confirm_Cancel")));
|
|
}
|
|
|
|
/// <summary>
|
|
/// 下载导入模板
|
|
/// </summary>
|
|
/// <param name="sender"></param>
|
|
/// <param name="e"></param>
|
|
protected void PageManager1_CustomEvent(object sender, CustomEventArgs e)
|
|
{
|
|
if (e.EventArgument == "Confirm_OK")
|
|
{
|
|
string rootPath = Server.MapPath("~/");
|
|
string uploadfilepath = rootPath + Const.MaterialCodeLibTemplateUrl;
|
|
string filePath = Const.MaterialCodeLibTemplateUrl;
|
|
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 导入
|
|
/// <summary>
|
|
/// 导入
|
|
/// </summary>
|
|
/// <param name="sender"></param>
|
|
/// <param name="e"></param>
|
|
protected void btnImport_Click(object sender, EventArgs e)
|
|
{
|
|
string message = string.Empty;
|
|
errorInfos = string.Empty;
|
|
List<HJGL_MaterialCodeLib> codeLib = new List<HJGL_MaterialCodeLib>();
|
|
List<HJGL_MaterialCodeLib> codeLib_update = new List<HJGL_MaterialCodeLib>();
|
|
var materialcodelist =MaterialCodeLibService.GetMaterialCodeLibList();
|
|
if (this.fuAttachUrl.HasFile == false)
|
|
{
|
|
ShowNotify("请选择Excel文件!", MessageBoxIcon.Warning);
|
|
return;
|
|
}
|
|
string IsXls = Path.GetExtension(this.fuAttachUrl.FileName).ToString().Trim().ToLower();
|
|
if (IsXls != ".xls" && IsXls != ".xlsx")
|
|
{
|
|
ShowNotify("只能选择Excel文件!", MessageBoxIcon.Warning);
|
|
return;
|
|
}
|
|
if (codeLib != null)
|
|
{
|
|
codeLib.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);
|
|
//文件上传服务器后的名称
|
|
string fileName = rootPath + initPath + this.hdFileName.Text;
|
|
//读取Excel
|
|
//DataSet ds = NPOIHelper.ExcelToDataSet(fileName, out errorInfos, true);
|
|
////验证Excel读取是否有误
|
|
//if (!string.IsNullOrEmpty(errorInfos))
|
|
//{
|
|
// ShowNotify(errorInfos, MessageBoxIcon.Warning);
|
|
// return;
|
|
//}
|
|
var rows = MiniExcel.Query<MaterialCodeLibDtoIn>(fileName).ToList();
|
|
rows = rows.Where(x => x.MaterialCode != "" && x.MaterialName != "").ToList();
|
|
if (rows.Count==0)
|
|
{
|
|
ShowNotify("没有数据!", MessageBoxIcon.Warning);
|
|
return;
|
|
}
|
|
for (int i = 0; i < rows.Count; i++)
|
|
{
|
|
HJGL_MaterialCodeLib item = new HJGL_MaterialCodeLib();
|
|
#region 数据验证和赋值
|
|
if (rows[i].MaterialCode != null && !string.IsNullOrEmpty(rows[i].MaterialCode))
|
|
{
|
|
item.MaterialCode = rows[i].MaterialCode;
|
|
}
|
|
else
|
|
{
|
|
errorInfos += (i + 2) + "Line, [元件编码] 不能为空</br>";
|
|
}
|
|
item.MaterialDef = rows[i].MaterialDef;
|
|
item.MaterialSpec = rows[i].MaterialSpec;
|
|
// item.MaterialMade = dv[i]["材质"].ToString();
|
|
item.MaterialUnit = rows[i].MaterialUnit;
|
|
item.MaterialName = rows[i].MaterialName;
|
|
//item.PipeGrade = dv[i]["管道等级"].ToString();
|
|
//item.ProjectId = CurrUser.LoginProjectId;
|
|
//= SQLHelper.GetNewID(typeof(Model.Editor_CostReport));
|
|
if (!codeLib.Select(x => x.MaterialCode).Contains(item.MaterialCode))
|
|
{
|
|
|
|
if (materialcodelist.Select(x => x.MaterialCode).Contains(item.MaterialCode))
|
|
{
|
|
codeLib_update.Add(item);
|
|
}
|
|
else
|
|
{
|
|
codeLib.Add(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
// DataRow[] dv = ds.Tables[0].Select("元件编码 <>'' and 类型 <>''");
|
|
//导入数据库
|
|
//if (dv.Length > 0)
|
|
//{
|
|
// for (int i = 0; i < dv.Length; i++)
|
|
// {
|
|
// HJGL_MaterialCodeLib item = new HJGL_MaterialCodeLib();
|
|
|
|
// #region 数据验证和赋值
|
|
// if (dv[i]["元件编码"] != null && !string.IsNullOrEmpty(dv[i]["元件编码"].ToString()))
|
|
// {
|
|
// item.MaterialCode = dv[i]["元件编码"].ToString();
|
|
// }
|
|
// else
|
|
// {
|
|
// errorInfos += (i + 2) + "Line, [元件编码] 不能为空</br>";
|
|
// }
|
|
// item.MaterialDef = dv[i]["材料描述"].ToString();
|
|
// item.MaterialSpec = dv[i]["规格"].ToString();
|
|
// // item.MaterialMade = dv[i]["材质"].ToString();
|
|
// item.MaterialUnit = dv[i]["单位"].ToString();
|
|
// item.MaterialName = dv[i]["类型"].ToString();
|
|
// //item.PipeGrade = dv[i]["管道等级"].ToString();
|
|
// //item.ProjectId = CurrUser.LoginProjectId;
|
|
// //= SQLHelper.GetNewID(typeof(Model.Editor_CostReport));
|
|
// if (!codeLib.Select(x => x.MaterialCode).Contains(item.MaterialCode))
|
|
// {
|
|
|
|
// if (materialcodelist.Select(x => x.MaterialCode).Contains(item.MaterialCode))
|
|
// {
|
|
// codeLib_update.Add(item);
|
|
// }
|
|
// else
|
|
// {
|
|
// codeLib.Add(item);
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// #endregion
|
|
// }
|
|
|
|
//}
|
|
//else
|
|
//{
|
|
// ShowNotify("没有数据!", MessageBoxIcon.Warning);
|
|
// return;
|
|
//}
|
|
|
|
if (!string.IsNullOrEmpty(errorInfos))
|
|
{
|
|
Alert.ShowInTop(errorInfos, MessageBoxIcon.Warning);
|
|
return;
|
|
}
|
|
codeLib = (from x in codeLib
|
|
select new HJGL_MaterialCodeLib
|
|
{
|
|
MaterialCode=x.MaterialCode,
|
|
MaterialDef=x.MaterialDef,
|
|
MaterialSpec=x.MaterialSpec,
|
|
MaterialUnit=x.MaterialUnit,
|
|
MaterialName=x.MaterialName,
|
|
|
|
}).DistinctBy(temp=> new
|
|
{
|
|
temp.MaterialCode,
|
|
temp.MaterialDef,
|
|
temp.MaterialSpec,
|
|
temp.MaterialUnit,
|
|
temp.MaterialName
|
|
}).ToList();
|
|
codeLib_update = (from x in codeLib_update
|
|
select new HJGL_MaterialCodeLib
|
|
{
|
|
MaterialCode = x.MaterialCode,
|
|
MaterialDef = x.MaterialDef,
|
|
MaterialSpec = x.MaterialSpec,
|
|
MaterialUnit = x.MaterialUnit,
|
|
MaterialName = x.MaterialName,
|
|
|
|
}).DistinctBy(temp => new
|
|
{
|
|
temp.MaterialCode,
|
|
temp.MaterialDef,
|
|
temp.MaterialSpec,
|
|
temp.MaterialUnit,
|
|
temp.MaterialName
|
|
}).ToList();
|
|
foreach (var item in codeLib_update)
|
|
{
|
|
var mewCostReport = BLL.MaterialCodeLibService.GetMaterialCodeLib(item.MaterialCode);
|
|
if (mewCostReport == null)
|
|
{
|
|
BLL.MaterialCodeLibService.AddMaterialCodeLib(item);
|
|
}
|
|
else
|
|
{
|
|
BLL.MaterialCodeLibService.UpdateMaterialCodeLib(item);
|
|
}
|
|
}
|
|
MaterialCodeLibService.AddBulkMaterialCodeLib(codeLib);
|
|
ShowNotify("数据导入成功!", MessageBoxIcon.Success);
|
|
PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
|
|
|
|
}
|
|
#endregion
|
|
}
|
|
} |