using System; using System.Linq; using System.Web.Mvc; using DSWeb.MvcShipping.DAL.MsCodeExcelImpFormatDAL; using DSWeb.MvcShipping.Models.MsCodeExcelImpFormat; using DSWeb.MvcShipping.Helper; using DSWeb.MvcShipping.Comm.Cookie; using System.Collections.Generic; using HcUtility.Comm; using HcUtility.Core; using System.IO; using Microsoft.Office.Interop.Excel; using DSWeb.MvcShipping.DAL.MsOpSeaeDAL; using DSWeb.EntityDA; using System.Data; namespace DSWeb.MvcShipping.Controllers { [JsonRequestBehavior] public class MsCodeExcelImpFormatController : Controller { // // GET: public ActionResult Index() { return View(); } // // GET: / public ActionResult Edit() { return View(); } // // GET: public ContentResult GetDataList(string condition) { var dataList = MsCodeExcelImpFormatDAL.GetDataList(condition, Convert.ToString(Session["USERID"]), Convert.ToString(Session["SHOWNAME"]), Convert.ToString(Session["COMPANYID"])); var json = JsonConvert.Serialize(new { Success = true, Message = "查询成功", totalCount = dataList.Count, data = dataList.ToList() }); return new ContentResult() { Content = json }; } // // GET:/TruckMng/MsWlTyreAcc/GetData/ public ContentResult GetData(string handle, string condition) { MsCodeExcelImpFormat head = null; if (handle == "edit") { var list = MsCodeExcelImpFormatDAL.GetDataList(condition, Convert.ToString(Session["USERID"]), Convert.ToString(Session["SHOWNAME"]), Convert.ToString(Session["COMPANYID"])); if (list.Count > 0) head = list[0]; } if (head == null) { head = new MsCodeExcelImpFormat(); } var json = JsonConvert.Serialize( new { Success = true, Message = "查询成功", data = head }); return new ContentResult() { Content = json }; } public ContentResult GetBodyList(string condition) { var dataList = MsCodeExcelImpFormatDAL.GetBodyList(condition); var json = JsonConvert.Serialize(new { Success = true, Message = "查询成功", totalCount = dataList.Count, data = dataList.ToList() }); return new ContentResult() { Content = json }; } public ContentResult GetFeeList(string condition) { var dataList = MsCodeExcelImpFormatDAL.GetFeeList(condition); var json = JsonConvert.Serialize(new { Success = true, Message = "查询成功", totalCount = dataList.Count, data = dataList.ToList() }); return new ContentResult() { Content = json }; } public ContentResult Save(string opstatus, string data, string body,string bodyfee) { var headData = JsonConvert.Deserialize(data); var bodyList = JsonConvert.Deserialize>(body); var feeList = JsonConvert.Deserialize>(bodyfee); if (opstatus == "add") { headData.DbOperationType = DbOperationType.DbotIns; headData.CREATEUSER = Convert.ToString(Session["USERID"]); headData.CREATETIME = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); headData.CORPID = Convert.ToString(Session["COMPANYID"]); } else if (opstatus == "edit") { headData.DbOperationType = DbOperationType.DbotUpd; headData.ModelUIStatus = "E"; } else { headData.DbOperationType = DbOperationType.DbotDel; } headData.MODIFIEDUSER = Convert.ToString(Session["USERID"]); headData.MODIFIEDTIME = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); var modb = new ModelObjectDB(); modb.Save(headData); var OS_ID = headData.GID; DBResult result = MsCodeExcelImpFormatDAL.SaveDetail(OS_ID, bodyList,feeList); var jsonRespose = new JsonResponse { Success = result.Success, Message = result.Message, Data = MsCodeExcelImpFormatDAL.GetData("GID='" + OS_ID + "'") }; return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) }; } // // GET:/Import/Cargoinfo/Delete public ContentResult Delete(string data) { var headData = JsonConvert.Deserialize(data); var modb = new ModelObjectDB(); DBResult result = modb.Delete(headData); if (result.Success == true) { MsCodeExcelImpFormatDAL.DeleteDetail(headData); } var jsonRespose = new JsonResponse { Success = result.Success, Message = result.Message }; return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) }; } public ContentResult DeleteDetail(string GID) { DBResult result =MsCodeExcelImpFormatDAL.DeleteDetail(GID); var jsonRespose = new JsonResponse { Success = result.Success, Message = result.Message }; return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) }; } public ContentResult DeleteFeeDetail(string GID) { DBResult result = MsCodeExcelImpFormatDAL.DeleteFeeDetail(GID); var jsonRespose = new JsonResponse { Success = result.Success, Message = result.Message }; return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) }; } public ContentResult SaveBodyDetail(string gid, string body) { var bodyList = JsonConvert.Deserialize>(body); DBResult result = MsCodeExcelImpFormatDAL.SaveBodyDetail(gid, bodyList); var jsonRespose = new JsonResponse { Success = result.Success, Message = result.Message }; return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) }; } #region 引入 EXCEL [HttpPost] public ContentResult ImpExcel() { var jsonRespose = new JsonResponse { Success = false, Message = "" }; if (Request.Files.Count != 1) { jsonRespose.Success = false; jsonRespose.Message = "请选择上传的文件"; return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) }; } var file = Request.Files["DsLoadExcel"]; if (file == null) { jsonRespose.Success = false; jsonRespose.Message = "上传文件发生未知错误,请重新上传"; return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) }; } var path = Server.MapPath("../../UploadFiles/ImpBsExcel"); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } var size = file.ContentLength; var name = Path.GetFileName(file.FileName); var bstype = Request.Form["bstype"]; var fmid = Request.Form["fmid"]; var usercode = CookieConfig.GetCookie_UserCode(Request); string filename = path + "\\" + usercode + DateTime.Now.ToString("yyyyMMddHHmmssfff") + name; if (System.IO.File.Exists(filename)) { System.IO.File.Delete(filename); } file.SaveAs(filename); if (!System.IO.File.Exists(filename)) { jsonRespose.Success = false; jsonRespose.Message = "上传的Excel不包含数据01"; return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) }; } try { string ext = Path.GetExtension(file.FileName).ToLower(); if (ext != ".xls" && ext != ".xlsx") { jsonRespose.Success = false; jsonRespose.Message = "上传的文件不是Excel文件"; return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) }; } var strMsg = ""; System.Data.DataTable table = ExcelHelper.ExcelToDatatable(file.InputStream, ext, out strMsg); if (!string.IsNullOrEmpty(strMsg)) { jsonRespose.Success = false; jsonRespose.Message = strMsg; return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) }; } if (table.Rows.Count > 0) { } else { jsonRespose.Success = false; jsonRespose.Message = "excel无数据!"; return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) }; } var result = new DBResult(); //if (customerFieldName == "委托单位") //{ var userid = Session["USERID"].ToString(); var username = Session["SHOWNAME"].ToString(); var companyid = Session["COMPANYID"].ToString(); if (bstype == "海运出口") { result = MsCodeExcelImpFormatDAL.ImpOpSeae(table, fmid, userid, username, companyid); } var json = JsonConvert.Serialize(new { success = result.Success, Message = result.Message }); return new ContentResult() { Content = json }; } catch (Exception e) { jsonRespose.Success = false; jsonRespose.Message = "读取Excel文件出错,请确认文件正确性" + e.Message; return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) }; } } public string getPropertiesValue(T t,string fieldname) { string tStr =""; if (t == null) { return tStr; } System.Reflection.PropertyInfo[] properties = t.GetType().GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public); if (properties.Length <= 0) { return tStr; } foreach (System.Reflection.PropertyInfo item in properties) { string name = item.Name; object value = item.GetValue(t, null); if (item.PropertyType.IsValueType || item.PropertyType.Name.StartsWith("String")) { if (name == fieldname) tStr = value.ToString(); } } return tStr; } public string getEString(int strnum) { var result = ""; if (strnum == 1) result = "A"; if (strnum == 2) result = "B"; if (strnum == 3) result = "C"; if (strnum == 4) result = "D"; if (strnum == 5) result = "E"; if (strnum == 6) result = "F"; if (strnum == 7) result = "G"; if (strnum == 8) result = "H"; if (strnum == 9) result = "I"; if (strnum == 10) result = "J"; if (strnum == 11) result = "K"; if (strnum == 12) result = "L"; if (strnum == 13) result = "M"; if (strnum == 14) result = "N"; if (strnum == 15) result = "O"; if (strnum == 16) result = "P"; if (strnum == 17) result = "Q"; if (strnum == 18) result = "R"; if (strnum == 19) result = "S"; if (strnum == 20) result = "T"; if (strnum == 21) result = "U"; if (strnum == 22) result = "V"; if (strnum == 23) result = "W"; if (strnum == 24) result = "X"; if (strnum == 25) result = "Y"; if (strnum == 26) result = "Z"; return result; } #endregion #region 参照部分 #endregion } }