using BLL; using MiniExcelLibs; using Model; using System; using System.Data; using System.IO; using System.Text; using System.Web; namespace FineUIPro.Web.CLGL { public partial class OutPlanMasterHistoryImport : PageBase { private readonly string initPath = Const.ExcelUrl; /// /// 单位工程主键 /// public string UnitWorkId { get { return (string)ViewState["UnitWorkId"]; } set { ViewState["UnitWorkId"] = value; } } protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { UnitWorkId = Request.Params["UnitWorkId"]; hdFileName.Text = string.Empty; } } protected void btnImport_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(UnitWorkId)) { ShowNotify("请选择单位工程!", MessageBoxIcon.Warning); return; } if (fuAttachUrl.HasFile == false) { ShowNotify("请选择Excel文件!", MessageBoxIcon.Warning); return; } string isXls = Path.GetExtension(fuAttachUrl.FileName).Trim().ToLower(); if (isXls != ".xlsx") { ShowNotify("只能选择xlsx格式Excel文件!", MessageBoxIcon.Warning); return; } string rootPath = Server.MapPath("~/"); string initFullPath = rootPath + initPath; if (!Directory.Exists(initFullPath)) { Directory.CreateDirectory(initFullPath); } hdFileName.Text = Funs.GetNewFileName() + isXls; string filePath = initFullPath + hdFileName.Text; fuAttachUrl.PostedFile.SaveAs(filePath); ResponeData responeData = TwInOutplanmasterService.ImportOutHistoryData( fuAttachUrl.FileName, filePath, this.CurrUser.LoginProjectId, this.CurrUser.PersonId, UnitWorkId); if (responeData.code == 1) { DeleteTempFile(filePath); ShowNotify(responeData.message, MessageBoxIcon.Success); PageContext.RegisterStartupScript(ActiveWindow.GetHideRefreshReference()); } else { Alert alert = new Alert { Message = responeData.message, MessageBoxIcon = MessageBoxIcon.Error }; alert.Show(); } } protected void btnDownLoad_Click(object sender, EventArgs e) { string rootPath = Server.MapPath("~/"); string tempDirectory = rootPath + @"File\Excel\Temp\"; if (!Directory.Exists(tempDirectory)) { Directory.CreateDirectory(tempDirectory); } string tempPath = tempDirectory + "历史出库数据导入模板" + string.Format("{0:yyyyMMddHHmmss}", DateTime.Now) + ".xlsx"; DataTable template = new DataTable(); template.Columns.Add("仓库"); template.Columns.Add("材料编码"); template.Columns.Add("炉号"); template.Columns.Add("批号"); template.Columns.Add("数量"); template.Columns.Add("出库时间"); template.Columns.Add("领用单位"); MiniExcel.SaveAs(tempPath, template); FileInfo info = new FileInfo(tempPath); long fileSize = info.Length; Response.ClearContent(); Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode("历史出库数据导入模板.xlsx", Encoding.UTF8)); Response.ContentType = "excel/plain"; Response.ContentEncoding = Encoding.UTF8; Response.AddHeader("Content-Length", fileSize.ToString().Trim()); Response.TransmitFile(tempPath, 0, fileSize); Response.Flush(); Response.Close(); DeleteTempFile(tempPath); } private void DeleteTempFile(string filePath) { if (!string.IsNullOrEmpty(filePath) && File.Exists(filePath)) { File.Delete(filePath); } } } }