using Common;
|
using BLL;
|
using Model;
|
using Lib;
|
using System.Web.Mvc;
|
using wms;
|
using System.Data;
|
using System.Web;
|
using System;
|
using NPOI.SS.UserModel;
|
using System.IO;
|
using NPOI.HSSF.UserModel;
|
using NPOI.XSSF.UserModel;
|
|
namespace WMS.Areas.BasicInfo.Controllers
|
{
|
public class MatController : MasterPage
|
{
|
[LoginFilter]
|
public ActionResult MatList()
|
{
|
ViewBag.Brand = LocalHelper.GetBrandItemsHtml();
|
ViewBag.MatType = LocalHelper.GetDictionaryHtml("MatType");
|
ViewBag.IsDel = LocalHelper.GetMatIsDelHtml();
|
ViewBag.Title = "物料管理";
|
return View();
|
}
|
|
[LoginFilter]
|
public ActionResult AddMat()
|
{
|
string Guid = Request.QueryString.Get("MatNo");
|
if (Guid.IsEmpty())
|
{
|
ViewBag.UnitList = LocalHelper.GetUnitList("");
|
ViewBag.Brand = LocalHelper.GetBrandItemsHtml();
|
ViewBag.MatType = LocalHelper.GetDictionaryHtml("MatType");
|
ViewBag.Admin = new Material();
|
|
}
|
else
|
{
|
IDALMatNo provider = new DALMatNo();
|
Material entity = provider.GetModel(Guid);
|
entity = entity == null ? new Material() : entity;
|
ViewBag.UnitList = LocalHelper.GetUnitList(entity.UnitNum);
|
ViewBag.Brand = LocalHelper.GetBrandItemsHtml(entity.BrandId);
|
ViewBag.MatType = LocalHelper.GetDictionaryHtml("MatType",entity.MatTypeId);
|
ViewBag.Admin = entity;
|
|
}
|
return View();
|
}
|
public ActionResult Dialog()
|
{
|
return View();
|
}
|
/// <summary>
|
/// 导入Excel
|
/// </summary>
|
/// <param name="file"></param>
|
/// <returns></returns>
|
[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);
|
NPOIExcel nPOI = new NPOIExcel("", "", "");
|
excelTable = NPOIExcel.InputExcel(serverpath);
|
//excelTable = GetExcelDataTable(serverpath);
|
//删除服务器生成的文件
|
System.IO.File.Delete(serverpath);
|
//注意Excel表内容格式,第一行必须为列名与数据库列名匹配
|
//接下来为各列名对应下来的内容
|
string CreateUser = this.LoginUserCode;
|
|
msg = DALMatNo.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();
|
}
|
}
|
}
|
}
|
}
|