SGGL_SHJ/SGGL/FineUIPro.Web/HJGL/BaseInfo/MaterialCodeLibIn.aspx.cs

196 lines
7.1 KiB
C#

using BLL;
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>();
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;
}
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))
{
codeLib.Add(item);
}
#endregion
}
}
else
{
ShowNotify("没有数据!", MessageBoxIcon.Warning);
return;
}
if (!string.IsNullOrEmpty(errorInfos))
{
Alert.ShowInTop(errorInfos, MessageBoxIcon.Warning);
return;
}
foreach (var item in codeLib)
{
var mewCostReport = BLL.MaterialCodeLibService.GetMaterialCodeLib(item.MaterialCode);
if (mewCostReport == null)
{
BLL.MaterialCodeLibService.AddMaterialCodeLib(item);
}
else
{
BLL.MaterialCodeLibService.UpdateMaterialCodeLib(item);
}
}
ShowNotify("数据导入成功!", MessageBoxIcon.Success);
PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
}
#endregion
}
}