using BLL; using Common; using FastReport.Web; using Lib; using Model; using NPOI.HSSF.UserModel; using NPOI.SS.UserModel; using NPOI.XSSF.UserModel; using System; using System.Collections.Generic; using System.Data; using System.IO; using System.Linq; using System.Web; using System.Web.Mvc; using Json; namespace WMS.Areas.Business.Controllers { public class ErpInController : MasterPage { // GET: Business/ErpIn [LoginFilter] public ActionResult Index() { ViewBag.StatuList = LocalHelper.GetDictionaryCodeHtml("CDStatu"); ViewBag.Title = "入库单管理"; return View(); } [LoginFilter] public ActionResult Add() { string OrdNo = Request.QueryString.Get("OrdNo"); Session["SEL_ERPIN_LIST"] = null; if (string.IsNullOrEmpty(OrdNo)) { ViewBag.Admin = new Erp(); ViewBag.OrdTypeList = LocalHelper.GetDictionaryCodeHtml("CDStatu"); } else { IDALErpIn provider = new DALErpIn(); IList erpMats = new List(); ErpIn entity = provider.GetModel(OrdNo, ref erpMats); Session["TEMPDATA_CACHE_INSTORDETAIL"] = erpMats; entity = entity == null ? new ErpIn() : entity; ViewBag.OrdTypeList = LocalHelper.GetOrdTypeList(entity.OrdType); ViewBag.Admin = entity; } return View(); } [LoginFilter] public ActionResult AddProduct() { Session["SEL_ERPIN_LIST"] = null; //清空入库选物料. string MatNo = Request.QueryString.Get("MatNo"); string palno = Request.QueryString.Get("palno"); if (string.IsNullOrEmpty(MatNo)) { ViewBag.Admin = new ErpMat(); } else { List list = Session["TEMPDATA_CACHE_INSTORDETAIL"] as List; List list1 = new List(); if (palno == "") { list1 = list.Where(a => a.MatNo == MatNo && a.Palno == null).ToList(); if (list1.Count <= 0) { list1 = list.Where(a => a.MatNo == MatNo && a.Palno == "").ToList(); } } else { list1 = list.Where(a => a.MatNo == MatNo && a.Palno == palno).ToList(); } ViewBag.Admin = list1[0]; ViewBag.Palnolist = LocalHelper.GetPalnoList(list1[0].addreNo, list1[0].Palno); } return View(); } [LoginFilter] public ActionResult Detail(string OrdNo) { if (!string.IsNullOrEmpty(OrdNo)) { Erp entity = new Erp(); IDALErpIn provider = new DALErpIn(); entity = provider.GetModel(OrdNo); ViewBag.Admin = entity; } return View(); } public ActionResult Position() { return View(); } [LoginFilter] public ActionResult ReportShow() { string ReportNo = Request["ReportNo"]; string OrdNo = Request["OrdNo"]; string[] ordNos = new string[] { OrdNo }; WebReport webReport = new WebReport(); webReport.Width = System.Web.UI.WebControls.Unit.Percentage(100); webReport.Height = 600; webReport.ToolbarIconsStyle = ToolbarIconsStyle.Black; webReport.ToolbarIconsStyle = ToolbarIconsStyle.Black; webReport.PrintInBrowser = false; webReport.PrintInPdf = false; webReport.ShowExports = false; webReport.SinglePage = false; //webReport.ToolbarStyle = ToolbarStyle.Small; webReport.PrintInPdf = true; webReport.ShowPrint = true; webReport.PreviewMode = true; DataSet ds = null; if (ReportNo.IndexOf("入库单") >= 0) { ReportNo = "航天入库单"; DALErpIn purchase = new DALErpIn(); ds = purchase.GetPrintData(OrdNo); } else if (ReportNo.IndexOf("出库单") >= 0) { ReportNo = "出库单"; IDALErpIn purchase = new DALErpIn(); ds = purchase.GetPrintData(OrdNo); } string path = "~/Report/" + ReportNo + ".frx"; path = Server.MapPath(path); webReport.Report.Load(path); if (ds != null && ds.Tables != null && ds.Tables.Count > 0) { webReport.Report.RegisterData(ds); for (int i = 0; i < ds.Tables.Count; i++) { webReport.Report.GetDataSource(ds.Tables[i].TableName).Enabled = true; } } webReport.ID = ReportNo; ViewBag.WebReport = webReport; return View(); } /// /// 导入Excel /// /// /// [HttpPost] public JsonResult ExcelToUpload(HttpPostedFileBase file) { DataTable excelTable = new DataTable(); string msg = string.Empty; if (Request.Files.Count > 0) { try { HttpPostedFileBase mypost = Request.Files[0]; string fileName = Request.Files[0].FileName; //在文件夹Report 用来接收上传的文件 string serverpath = Server.MapPath(string.Format("~/{0}", "Report/") + fileName); mypost.SaveAs(serverpath); // excelTable =GetExcelDataTable(serverpath); NPOIExcel nPOI = new NPOIExcel("", "", ""); excelTable = NPOIExcel.InputExcel(serverpath); //删除服务器生成的文件 System.IO.File.Delete(serverpath); //注意Excel表内容格式,第一行必须为列名与数据库列名匹配 //接下来为各列名对应下来的内容 string CreateUser = this.LoginUserCode; msg = DALErpIn.ImportExcel(excelTable, CreateUser);// 写入基础数据 } catch (Exception ex) { msg = ex.Message; } } else { msg = "请选择文件"; } return Json(msg); } public static DataTable GetExcelDataTable(string filePath) { IWorkbook Workbook; DataTable table = new DataTable(); try { using (FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read)) { //XSSFWorkbook 适用XLSX格式,HSSFWorkbook 适用XLS格式 string fileExt = System.IO.Path.GetExtension(filePath).ToLower(); if (fileExt == ".xls") { Workbook = new HSSFWorkbook(fileStream); } else if (fileExt == ".xlsx") { Workbook = new XSSFWorkbook(fileStream); } else { Workbook = null; } } } catch (Exception ex) { throw ex; } //定位在第一个sheet ISheet sheet = Workbook.GetSheetAt(0); //第一行为标题行 IRow headerRow = sheet.GetRow(0); int cellCount = headerRow.LastCellNum; int rowCount = sheet.LastRowNum; //循环添加标题列 for (int i = headerRow.FirstCellNum; i < cellCount; i++) { DataColumn column = new DataColumn(headerRow.GetCell(i).StringCellValue); table.Columns.Add(column); } //数据 for (int i = (sheet.FirstRowNum + 1); i <= rowCount; i++) { IRow row = sheet.GetRow(i); DataRow dataRow = table.NewRow(); if (row != null) { for (int j = row.FirstCellNum; j < cellCount; j++) { if (row.GetCell(j) != null) { dataRow[j] = GetCellValue(row.GetCell(j)); } } } table.Rows.Add(dataRow); } return table; } private static string GetCellValue(ICell cell) { if (cell == null) { return string.Empty; } switch (cell.CellType) { case CellType.Blank: return string.Empty; case CellType.Boolean: return cell.BooleanCellValue.ToString(); case CellType.Error: return cell.ErrorCellValue.ToString(); case CellType.Numeric: case CellType.Unknown: default: return cell.ToString(); case CellType.String: return cell.StringCellValue; case CellType.Formula: try { HSSFFormulaEvaluator e = new HSSFFormulaEvaluator(cell.Sheet.Workbook); e.EvaluateInCell(cell); return cell.ToString(); } catch { return cell.NumericCellValue.ToString(); } } } } }