using System; using System.Data; using System.Linq; using System.Web.Mvc; using System.Threading; using DSWeb.MvcShipping.DAL.VGMDAL; using DSWeb.Areas.MvcShipping.Models.VGM; using DSWeb.MvcShipping.Helper; using DSWeb.MvcShipping.Comm.Cookie; using System.Collections.Generic; using HcUtility.Comm; using HcUtility.Core; using DSWeb.MvcShipping.Models.UserQuerySetting; using DSWeb.Areas.CommMng.DAL; using DSWeb.Areas.SysMng.DAL.SysTask; using DSWeb.TruckMng.Helper.Repository; using Microsoft.Practices.EnterpriseLibrary.Data; using DSWeb.Areas.CommMng.Models; using DSWeb.MvcShipping.Models.MsOpSeae; using DSWeb.MvcShipping.DAL.MsOpSeaeEdiPortDAL; using DSWeb.MvcShipping.DAL.MsCodeFtpSet; using DSWeb.Areas.SysMng.Models.SysTask; using DSWeb.MvcShipping.Models.FtpSet; using NPOI.SS.UserModel; using System.IO; using NPOI.HSSF.UserModel; using DSWeb.MvcShipping.DAL.MsOpSeaeEdiVGM; namespace DSWeb.MvcShipping.Controllers { [JsonRequestBehavior] public class VGMController : Controller { // // GET: public ActionResult Index() { return View(); } // // GET: / public ActionResult Edit() { return View(); } #region VGM台账及明细 列表 查询 public ContentResult GetDataList(int start, int limit, string condition, string sort = "") { var canlogin = BasicDataRefDAL.CheckLogin(Session); if (!canlogin.Success) { return (ContentResult)canlogin.Data; } var _count = 0; var dataList = VGMDAL.GetDataList(start, limit, condition, Session["USERID"].ToString(), out _count, sort); var list_2 = ModelObjectBill.getDicList(ModelObjectConvert.ToModelObjectList(dataList)); var json = JsonConvert.Serialize(new { Success = true, Message = "查询成功", totalCount = _count, data = list_2.ToList() }); return new ContentResult() { Content = json }; } public ContentResult GetData(string handle, string condition) { var canlogin = BasicDataRefDAL.CheckLogin(Session); if (!canlogin.Success) { return (ContentResult)canlogin.Data; } var result = new VGM_Head_mb(); if (handle == "add") { result.getDef(Session); result.SetValue("canedit", "true"); } else if (handle == "copy") { result = VGMDAL.GetData(condition, Session); result.getDef(Session); result.SetValue("canedit", "true"); } else { result = VGMDAL.GetData(condition, Session); result.SetValue("canedit", VGMDAL.GetCanChange(result.GetValue("GID"), Session["USERID"].ToString()).ToString()); } var json = JsonConvert.Serialize(new { Success = true, Message = "查询成功", //totalCount = 1, data = result.ExtendDic //,fieldlist = fieldlist.ToList() }); return new ContentResult() { Content = json }; } public ContentResult GetCtnList(string condition) { var dataList = VGMDAL.GetBodyList(condition); var list = new List>(); if (dataList.Count() > 0) { foreach (var data in dataList) { list.Add(data.ExtendDic); } } var json = JsonConvert.Serialize(new { Success = true, Message = "查询成功", totalCount = list.Count, data = list.ToList() }); return new ContentResult() { Content = json }; } public ContentResult GetCanChange(string GID) { DBResult _r = new DBResult(); _r.Success = VGMDAL.GetCanChange(GID, Session["USERID"].ToString()); var json = JsonConvert.Serialize(new { Success = _r.Success, Message = "" }); return new ContentResult() { Content = json }; } #endregion #region 保存 删除 public ContentResult Save(string opstatus, string data, string Body)// { var modb = new ModelObjectRepository(); Database db = DatabaseFactory.CreateDatabase(); var _date = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); var head = new VGM_Head_mb(); head.SetValue(db, data); head.GetTableField(db); if (opstatus == "edit") { var msg = ""; var canchange1 = SysTaskDAL.TaskGetWord(head.GetValue("FORWARDSTATUS"), "canchange"); if (!canchange1) msg = "转发状态不允许该条数据被修改;"; var canchange3 = VGMDAL.GetCanChange(head.GetValue("GID"), Session["USERID"].ToString()); if (!canchange3) msg += "没有对该数据的编辑权限;"; if ((canchange1 && canchange3) == false)//canbedel2&& { var jsonRespose1 = new JsonResponse { Success = false, Message = msg + "不能进行修改!" }; return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose1) }; } } var basebody = new VGM_CTN_mb(); basebody.GetTableField(db); var BodyList = new List(); var DelBodyList = new List(); if (Body != "") { var savedl = JsonConvert.Deserialize>>(Body); foreach (var sdl in savedl) { var body = new VGM_CTN_mb(); basebody.Copy(body); body.SetValue(sdl); BodyList.Add(body); } } if (opstatus == "add") { head.DbOperationType = DbOperationType.DbotIns; head.ModelUIStatus = "I"; head.SetValue("GID", Guid.NewGuid().ToString()); } else if (opstatus == "edit") { head.DbOperationType = DbOperationType.DbotUpd; head.ModelUIStatus = "E"; } else if (opstatus == "copy") { head.DbOperationType = DbOperationType.DbotIns; head.ModelUIStatus = "I"; head.SetValue("GID", Guid.NewGuid().ToString()); foreach (var b in BodyList) { b.SetValue("GID", Guid.NewGuid().ToString()); b.SetValue("VGMGID", ""); } head.getDef(Session); } else { head.DbOperationType = DbOperationType.DbotDel; } DBResult result = modb.Save(head, ModelObjectConvert.ToModelObjectList(BodyList), ModelObjectConvert.ToModelObjectList(DelBodyList) ); var bsnovalue = head.GetBillNoValue(); var bsnoname = head.GetBillNoFieldName(); var jsonRespose = new JsonResponse { Success = result.Success, Message = result.Message, Data = VGMDAL.GetData(head.GetBillNoFieldName() + " = '" + head.GetBillNoValue() + "'", Session).ExtendDic//fieldlist }; return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) }; } public ContentResult Delete(string data) { Database db = DatabaseFactory.CreateDatabase(); var head = new VGM_Head_mb(); head.SetValue(db, data); var msg = ""; var canbedel1 = SysTaskDAL.TaskGetWord(head.GetValue("FORWARDSTATUS"), "candelete"); if (!canbedel1) msg = "转发状态不允许该条数据被删除;"; //var canbedel = true; if (canbedel1 == false)//canbedel2&& { var jsonRespose = new JsonResponse { Success = false, Message = msg + "不能进行删除!" }; return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) }; } else { var modb = new ModelObjectDBBill(); DBResult result = modb.Delete(head, "delete from VGM_CTN where VGMGID='" + head.GetValue("GID") + "'"); //MsSysBillNoSetDAL.DeleteBsNo(head.BillNo); var jsonRespose = new JsonResponse { Success = result.Success, Message = result.Message }; return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) }; } } public ContentResult DeleteDetail(string body) { var head = new VGM_CTN_mb(); var datadic = JsonConvert.Deserialize>(body); head.SetValue(datadic); DBResult result = VGMDAL.DeleteDetail(head); var jsonRespose = new JsonResponse { Success = result.Success, Message = result.Message }; return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) }; } #endregion #region 状态变更 public ContentResult PushState_FORWARD(string data, string Execution) { Database db = DatabaseFactory.CreateDatabase(); var head = new VGM_Head_mb(); head.SetValue(db, data); return doPushState(head, "FORWARDSTATUS", Execution); } public ContentResult PushState_RECEIPT(string data, string Execution) { Database db = DatabaseFactory.CreateDatabase(); var head = new VGM_Head_mb(); head.SetValue(db, data); return doPushState(head, "RECEIPTSTATUS", Execution); } private ContentResult doPushState(VGM_Head_mb head, string statusfield, string Execution) { Database db = DatabaseFactory.CreateDatabase(); var modb = new ModelObjectRepository(); JsonResponse result = new JsonResponse(); var STATE = head.GetValue(statusfield); var newState = SysTaskDAL.DoExecution(STATE, Execution); if (newState.STATEKEY == "0000") { result.Success = false; result.Message = head.GetValue(statusfield + "REF") + "状态下的业务不能进行此操作"; } else { var _Execution = SysTaskDAL.GetTheExecution(Execution); head.SetValue(statusfield, newState.STATEKEY); head.DbOperationType = DbOperationType.DbotUpd; head.ModelUIStatus = "E"; var _str1 = $"[{ _Execution.PROP1}][时间:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}][操作人:{Session["SHOWNAME"].ToString()}]"; var newtips = new VGM_TIPS_mb(); newtips.GetTableField(db); newtips.SetValue("EXECUTION", Execution); newtips.SetValue("CREATOR", Session["SHOWNAME"].ToString()); newtips.SetValue("CREATETIME", System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); newtips.SetValue("TIPSTEXT", _str1); newtips.SetValue("VGMGID", ""); //根据新状态 设定业务状态值 SetStatusTime(head, newState); var tipslist = new List(); tipslist.Add(newtips); var deltipslist = new List(); var _r = modb.Save(head, ModelObjectConvert.ToModelObjectList(tipslist), ModelObjectConvert.ToModelObjectList(deltipslist) ); result.Success = _r.Success; result.Message = _r.Message; if (result.Success && statusfield == "RECEIPTSTATUS" && !string.IsNullOrEmpty(head.GetValue("MFNO"))) { //1101 船公司已接收 //1102 船公司拒绝 if (newState.STATEKEY == "1101") { SetVGMStatus(head.GetValue("MFNO"), "船公司已接收", head.GetValue("MBLNO")); } if (newState.STATEKEY == "1102") { SetVGMStatus(head.GetValue("MFNO"), "船公司拒绝", head.GetValue("MBLNO")); } if (newState.STATEKEY == "1100") { DelVGMStatus(head.GetValue("MFNO")); } } } return new ContentResult() { Content = JsonConvert.Serialize(result) }; } private void SetStatusTime(VGM_Head_mb head, TaskStateMC newState) { //根据新状态 设定业务状态值 if (SysTaskDAL.TaskGetWord(newState.STATEKEY, "clearsend")) { head.SetForwardStatusTime(""); } if (SysTaskDAL.TaskGetWord(newState.STATEKEY, "savesendtime")) { head.SetForwardStatusTime(Session["SHOWNAME"].ToString()); } if (SysTaskDAL.TaskGetWord(newState.STATEKEY, "clearreceipt")) { head.SetValue("RECEIPTTIME", ""); } if (SysTaskDAL.TaskGetWord(newState.STATEKEY, "savereceipttime")) { head.SetValue("RECEIPTTIME", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); } } private void SetVGMStatus(string MFNO, string statestr, string mblno) { //var cmdstr = $" INSERT INTO [dbo].[op_seae_vgm_status] ([gid],[mfno],[statustext],[statustime],[createtime],[cntrno],[mblno])VALUES(NEWID(),'{MFNO}','{statestr}',getdate(),getdate(),isnull((select top 1 CNTRNO from op_seae_vgm_ctn where MFNO='{MFNO}' and isnull(CNTRNO,'')<>''),''),'{mblno}') "; //插入触发器阻止了批量的 insert into select过程的执行 //var cmdstr = $" INSERT INTO [dbo].[op_seae_vgm_status] ([gid],[mfno],[statustext],[statustime],[createtime],[cntrno],[mblno])VALUES(NEWID(),'{MFNO}','{statestr}',getdate(),getdate(),isnull((select top 1 CNTRNO from op_seae_vgm_ctn where MFNO='{MFNO}' and isnull(CNTRNO,'')<>''),''),'{mblno}') "; var vgmctnlist= MsOpSeaeEdiVGMDAL.GetBodyList("MFNO='"+ MFNO + "'"); var cmdstr = ""; foreach (var item in vgmctnlist) { cmdstr = $" INSERT INTO [dbo].[op_seae_vgm_status] ([gid],[mfno],[statustext],[statustime],[createtime],[cntrno],[mblno])VALUES(NEWID(),'{MFNO}','{statestr}',getdate(),getdate(),'{item.CNTRNO}','{mblno}') "; BasicDataRefDAL.ExecSql(cmdstr); } //BasicDataRefDAL.ExecSql(cmdstr); } private void DelVGMStatus(string MFNO) { var cmdstr = $" delete from [dbo].[op_seae_vgm_status] where MFNO='{MFNO}' "; BasicDataRefDAL.ExecSql(cmdstr); } #endregion #region VGM工作台账列表 根据查询条件获取打印的sql文本 public ContentResult GetHeadIndexPrint(string printname, string condition) { var jsonRespose = new JsonResponse { Success = false, Message = "" }; var result = new PrintSql(); result.sql1 = VGMDAL.getHeadListStr(condition); //result.sql2 = MsQuotationDAL.getBodyListStr(" and BSNO in(" + BSNOList + ")"); jsonRespose.Success = true; jsonRespose.Message = "查询成功"; jsonRespose.Data = result; return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) }; } #endregion /// /// 生成的VGM文件的放置位置 /// public string VGMFILEPATH { get { return "../../VGMFile/" + Session["USERID"].ToString(); } } private void PreparePath(){ if (Directory.Exists(Server.MapPath(VGMFILEPATH)) == false) { Directory.CreateDirectory(Server.MapPath(VGMFILEPATH)); } foreach (string f in Directory.GetFileSystemEntries(Server.MapPath(VGMFILEPATH))) { if (System.IO.File.Exists(f)) { //如果有子文件删除文件 System.IO.File.Delete(f); } } } /// /// 亿通的Excel版VGM的位置 /// const string YTVGMExcel = "../../Areas/MvcShipping/Views/VGM/YTVGM_EXCEL.xls"; public static string CARRIERVGM(string CARRIER) { if (CARRIER == "EMC") return "../../Areas/MvcShipping/Views/VGM/EMC.xls"; if (CARRIER == "COSCO") return "../../Areas/MvcShipping/Views/VGM/COSCO.xls"; if (CARRIER == "APL" || CARRIER == "CNC" || CARRIER == "ANL" || CARRIER == "CMA") return "../../Areas/MvcShipping/Views/VGM/APL_CNC_ANL_CMA.xls"; if (CARRIER == "YML") return "../../Areas/MvcShipping/Views/VGM/YML.xls"; if (CARRIER == "MSC") return "../../Areas/MvcShipping/Views/VGM/MSC.xls"; return ""; } public static Int32 CARRIERVGM_FirstRow(string CARRIER) { if (CARRIER == "EMC") return 4; if (CARRIER == "COSCO") return 1; if (CARRIER == "APL" || CARRIER == "CNC" || CARRIER == "ANL" || CARRIER == "CMA") return 1; if (CARRIER == "YML") return 1; if (CARRIER == "MSC") return 1; return -1; } public static bool CheckCarrier(string CARRIER) { if (CARRIERVGM_FirstRow(CARRIER)!=-1) return true; return false; } public class CARRIERVGMHelper { public string CARRIER { get; set; } public string EXCELPATH { get; set; } public IWorkbook book { get; set; } public ISheet SheetOne { get { return book.GetSheetAt(0); } } public CARRIERVGMHelper(string CARRIER, IWorkbook book) { EXCELPATH = CARRIERVGM(CARRIER); this.book = book; this.CARRIER = CARRIER; } public Int32 NEWROW( VGM_Head_mb head, VGM_CTN_mb ctn, Int32 CurrRow = -1) { var start = -1; var Row = CurrRow; var 企业签名 = "QDDJY"; if (CurrRow == -1) { Row = CARRIERVGM_FirstRow(CARRIER); } var row = SheetOne.CreateRow(Row); //创建1行 row.HeightInPoints = 20;//设置单元格的高度 row = (HSSFRow)SheetOne.GetRow(Row); //获取Sheet1工作表的首行 var SheetCell = new HSSFCell[20]; //对每一行创建10个单元格 for (int i = 0; i < 20; i++) { SheetCell[i] = (HSSFCell)row.CreateCell(i); //为第一行创建10个单元格 } IDataFormat dataformat = book.CreateDataFormat(); ICellStyle 小数0 = book.CreateCellStyle(); 小数0.DataFormat = dataformat.GetFormat("0"); ICellStyle 小数3 = book.CreateCellStyle(); 小数3.DataFormat = dataformat.GetFormat("0.000"); //小数3.DataFormat = HSSFDataFormat.GetBuiltinFormat("0.000"); if (CARRIER == "EMC") { SheetCell[start + 1].SetCellValue("EGLV"); SheetCell[start + 2].SetCellValue(head.Mblno); SheetCell[start + 3].SetCellValue(ctn.CntrNo); SheetCell[start + 4].SetCellValue(ctn.TOTALWEIGHT_double); //SheetCell[start + 4].CellStyle = 小数3; SheetCell[start + 5].SetCellValue("KG"); SheetCell[start + 6].SetCellValue(企业签名); SheetCell[start + 7].SetCellValue(企业签名); SheetCell[start + 8].SetCellValue(ctn.WEIGHTYPE_double); SheetCell[start + 9].SetCellValue(企业签名); } if (CARRIER == "COSCO") { SheetCell[start + 1].SetCellValue(head.Mblno.Substring(4, head.Mblno.Length - 4)); //SheetCell[start + 2].SetCellValue(head.Mblno); SheetCell[start + 2].SetCellValue(""); SheetCell[start + 3].SetCellValue(ctn.CntrNo); SheetCell[start + 4].SetCellValue(ctn.TOTALWEIGHT_double); //SheetCell[start + 4].CellStyle = 小数3; SheetCell[start + 5].SetCellValue("KGS"); SheetCell[start + 6].SetCellValue(企业签名); SheetCell[start + 7].SetCellValue(企业签名); SheetCell[start + 8].SetCellValue(ctn.WEIGHTYPE_double); SheetCell[start + 9].SetCellValue(企业签名); //SheetCell[start + 10].SetCellValue(ctn.VGMTIME_COSCO); SheetCell[start + 10].SetCellValue(""); } if (CARRIER == "APL" || CARRIER == "CNC" || CARRIER == "ANL" || CARRIER == "CMA") { SheetCell[start + 1].SetCellValue(head.Mblno); SheetCell[start + 2].SetCellValue(ctn.CntrNo); SheetCell[start + 3].SetCellValue("N"); SheetCell[start + 4].SetCellValue(""); SheetCell[start + 5].SetCellValue(ctn.TOTALWEIGHT_double); //SheetCell[start + 5].CellStyle = 小数3; SheetCell[start + 6].SetCellValue("KGM"); SheetCell[start + 7].SetCellValue(企业签名); SheetCell[start + 8].SetCellValue(ctn.WEIGHTYPE); } //阳明 VGMEXCEL if (CARRIER == "YML") { SheetCell[start + 1].SetCellValue(ctn.CntrNo); SheetCell[start + 2].SetCellValue(head.Mblno); SheetCell[start + 3].SetCellValue(企业签名); SheetCell[start + 4].SetCellValue(企业签名); SheetCell[start + 5].SetCellValue(企业签名); SheetCell[start + 6].SetCellValue(ctn.TOTALWEIGHT_double); SheetCell[start + 7].SetCellValue("KGS"); SheetCell[start + 8].SetCellValue(ctn.WEIGHTYPE_double); SheetCell[start + 9].SetCellValue(ctn.VGMTIME_YML); } if (CARRIER == "MSC") { //SheetCell[start + 1].SetCellValue(head.Mblno); SheetCell[start + 1].SetCellValue(ctn.CntrNo); SheetCell[start + 2].SetCellValue(ctn.TOTALWEIGHT); SheetCell[start + 3].SetCellValue("KGS"); } Row++; return Row; } } /// /// 发出VGM 亿通 /// /// /// /// public ContentResult SENDVGM_YT(string data,string portcode) { var result = new DBResult(); var path = Server.MapPath(VGMFILEPATH); var ftpset = MsCodeFtpSetDAL.GetData("EDINAME='YTVGM'", Convert.ToString(Session["COMPANYID"]));// AND CARRIERID='" + carrier + "' if (ftpset.SERVERIP != "" && ftpset.SERVERIP != null) { var ytvgmhead = getYTVGMSText(data, portcode, ftpset); result.setMessage(true, "测试发送成功"); BasicDataRefDAL.SaveLog_str(ytvgmhead.FILENAME, Session["USERID"].ToString(),"VGM工作台账_发出VGM","YTVGM"); } else { result.SetErrorInfo( "FTP参数没有设置,请先设置FTP参数!"); } var json = JsonConvert.Serialize(new { Success = result.Success, Message = result.Message, totalCount = 0, Data = "" }); return new ContentResult() { Content = json }; } /// /// 获取VGM文本 /// /// /// /// public ContentResult GETVGM_YT(string data, string portcode) { var result = new DBResult(); var path = Server.MapPath(VGMFILEPATH); var ftpset = MsCodeFtpSetDAL.GetData("EDINAME='YTVGM'", Convert.ToString(Session["COMPANYID"]));// AND CARRIERID='" + carrier + "' if (ftpset.SERVERIP != "" && ftpset.SERVERIP != null) { var filename = getYTVGMSText(data, portcode, ftpset).smallFileName; result.setMessage(true, "导出成功"); BasicDataRefDAL.SaveLog_str(filename, Session["USERID"].ToString(), "VGM工作台账_生成VGM", "YTVGM"); result.Data = VGMFILEPATH + "/" + filename; } else { result.SetErrorInfo("FTP参数没有设置。亿通报文内容需要填写FTP信息,请先设置FTP参数!"); } JsonResponse JsonResponse = new JsonResponse(result); return JsonResponse.getContentReult(); } public ContentResult GETVGM_YT_List(string GIDList, string portcode) { var result = new DBResult(); var path = Server.MapPath(VGMFILEPATH); var ftpset = MsCodeFtpSetDAL.GetData("EDINAME='YTVGM'", Convert.ToString(Session["COMPANYID"])); if (ftpset.SERVERIP != "" && ftpset.SERVERIP != null) { var filename = getYTVGMSText_List(GIDList, portcode, ftpset).smallFileName; result.setMessage(true, "导出成功"); BasicDataRefDAL.SaveLog_str(filename, Session["USERID"].ToString(), "VGM工作台账_生成VGM", "YTVGM"); result.Data = VGMFILEPATH + "/" + filename; } else { result.SetErrorInfo("FTP参数没有设置。亿通报文内容需要填写FTP信息,请先设置FTP参数!"); } var json = JsonConvert.Serialize(new { Success = result.Success, Message = result.Message, Data = result.Data }); return new ContentResult() { Content = json }; } private VGM_Head_mb_YT getYTVGMSText(string data, string portcode, CodeFtpSet ftpset) { Database db = DatabaseFactory.CreateDatabase(); var ytvgmhead = new VGM_Head_mb_YT(); ytvgmhead.SetValue(db, data); var templist = new List(); templist.Add(ytvgmhead); return getYTVGMSText(templist, portcode, ftpset); } private VGM_Head_mb_YT getYTVGMSText_List(string GIDList, string portcode, CodeFtpSet ftpset) { var outcount = 0; var vgmlist = VGMDAL.GetDataList(0, 999, $"GID in({GIDList})", Session["USERID"].ToString(), out outcount); var datalist = new List(); foreach (var vgm in vgmlist) { var data = new VGM_Head_mb_YT(vgm); datalist.Add(data); } return getYTVGMSText(datalist, portcode, ftpset); } private VGM_Head_mb_YT getYTVGMSText(List dataList, string portcode, CodeFtpSet ftpset) { Database db = DatabaseFactory.CreateDatabase(); //var ytvgmhead = new VGM_Head_mb_YT(); //ytvgmhead.SetValue(db, data); var path = Server.MapPath(VGMFILEPATH); foreach (var ytvgmhead in dataList) { var _ctn = new VGM_CTN_mb(); var ctnList = VGMDAL.GetBodyList(" VGMGID='" + ytvgmhead.GetValue("GID") + "' "); ytvgmhead.setYT(ctnList, ftpset, path, portcode); } var filename = MsOpSeaeEdiPortDAL.CreateYTVGM(dataList); return dataList[0]; } public ContentResult GETVGM_YT_Excel(string GIDList, string portcode) { JsonResponse JsonResponse = new JsonResponse(); PreparePath(); try { var result = new DBResult(); var path = Server.MapPath(VGMFILEPATH); var filename = getYTVGMExcel(GIDList); result.setMessage(true, "导出成功"); BasicDataRefDAL.SaveLog_str(filename, Session["USERID"].ToString(), "VGM工作台账_生成VGM", "YTVGM_Excel"); result.Data = VGMFILEPATH + "/" + filename; JsonResponse = new JsonResponse(result); } catch (Exception e) { JsonResponse.Success = false; JsonResponse.Message = e.Message; } return JsonResponse.getContentReult(); } //protected Application objExcelApp;//定义Excel Application对象 //private Workbooks objExcelWorkBooks;//定义Workbook工作簿集合对象 //protected Workbook objExcelWorkbook;//定义Excel workbook工作簿对象 //private Worksheet objExcelWorkSheet;//定义Workbook工作表对象 private string getYTVGMExcel(string GIDList) { var outcount = 0; var vgmlist = VGMDAL.GetDataList(0, 999, $"GID in({GIDList})", Session["USERID"].ToString(), out outcount); var datalist = new List(); foreach (var vgm in vgmlist) { var data = new VGM_Head_mb_YT(vgm); data.setCarrierCode(); var _ctn = new VGM_CTN_mb(); var ctnList = VGMDAL.GetBodyList(" VGMGID='" + vgm.GetValue("GID") + "' "); data.setCtn(ctnList); datalist.Add(data); } var BaseExcelFilename = Server.MapPath(YTVGMExcel); IWorkbook book = null; //置空 FileStream fs = new FileStream(BaseExcelFilename, FileMode.Open, FileAccess.Read);//打开表 book = new HSSFWorkbook(fs);//读取表 fs.Close(); var rowcount = 1; ISheet SheetOne = book.GetSheetAt(0); //获取名称为第0个工作表 //IRow row = sheet.GetRow(0); //读取当前行数据 foreach (var YTItem in datalist) { foreach (var ctn in YTItem.ctnList) { var row = SheetOne.CreateRow(rowcount); //创建10行 row.HeightInPoints = 20;//设置单元格的高度 row = (HSSFRow)SheetOne.GetRow(rowcount); //获取Sheet1工作表的首行 var SheetCell = new HSSFCell[20]; //对每一行创建10个单元格 for (int i = 0; i < 20; i++) { SheetCell[i] = (HSSFCell)row.CreateCell(i); //为第一行创建10个单元格 } int start = -1; SheetCell[start + 1].SetCellValue(YTItem.Mblno); SheetCell[start + 2] .SetCellValue( YTItem.Mblno); SheetCell[start + 3] .SetCellValue( ctn.CntrNo); SheetCell[start + 4] .SetCellValue( ctn.CTNEDICODE); SheetCell[start + 5] .SetCellValue( ctn.SealNo); SheetCell[start + 6] .SetCellValue( ctn.TOTALWEIGHT); SheetCell[start + 7] .SetCellValue( ctn.WEIGHTYPE); SheetCell[start + 8] .SetCellValue( ctn.VGMTIME); SheetCell[start + 10] .SetCellValue( YTItem.Vessel); SheetCell[start + 11] .SetCellValue( YTItem.Voyno); SheetCell[start + 13] .SetCellValue( "QDDJY"); SheetCell[start + 18] .SetCellValue( "QINGDAO DAJIANYUN LOGISTICS CO.,LTD"); SheetCell[start + 19] .SetCellValue( "1ST ROAD KEYUANWEI,LAOSHAN DISTRICT,QINGDAO"); rowcount++; } } //保存 var filepath = Server.MapPath(VGMFILEPATH) + @"\" + datalist[0].smallFileName_excel; FileStream savefile = new FileStream(filepath, FileMode.Create);//创建一个文件 book.Write(savefile); savefile.Close(); return datalist[0].smallFileName_excel; } public ContentResult GETVGM_Excel(string GIDList,string CARRIER) { JsonResponse JsonResponse = new JsonResponse(); PreparePath(); if (!CheckCarrier(CARRIER)) { JsonResponse.Success = false; JsonResponse.Message = "船公司" + CARRIER + "暂时不支持"; return JsonResponse.getContentReult(); } try { var result = new DBResult(); var path = Server.MapPath(VGMFILEPATH); var VGMList = getVGMList(GIDList); if (VGMList.Count == 0) { JsonResponse.Success = false; JsonResponse.Message = "没有可以发出的业务"; return JsonResponse.getContentReult(); } var filename = getVGMExcel(VGMList, CARRIER); result.setMessage(true, "导出成功"); BasicDataRefDAL.SaveLog_str(filename, Session["USERID"].ToString(), "VGM工作台账_生成VGM", "VGM_"+CARRIER+"_Excel"); result.Data = VGMFILEPATH + "/" + filename; JsonResponse = new JsonResponse(result); } catch (Exception e) { JsonResponse.Success = false; JsonResponse.Message = e.Message; } return JsonResponse.getContentReult(); } private List getVGMList(string GIDList) { var outcount = 0; var locallist = VGMDAL.GetDataList(0, 999, $"GID in({GIDList})", Session["USERID"].ToString(), out outcount); var vgmlist = new List(); foreach (var vgm in locallist) { vgm.setCarrierCode(); var _ctn = new VGM_CTN_mb(); var ctnList = VGMDAL.GetBodyList(" VGMGID='" + vgm.GetValue("GID") + "' "); if (ctnList.Count > 0) { vgm.setCtn(ctnList); vgmlist.Add(vgm); } } return vgmlist; } private string getVGMExcel(List vgmlist, string CARRIER) { //根据船公司缩写 寻找一个船公司excel基础表格 var BaseExcelFilename = Server.MapPath(CARRIERVGM(CARRIER)); IWorkbook book = null; //置空 FileStream fs = new FileStream(BaseExcelFilename, FileMode.Open, FileAccess.Read);//打开表 book = new HSSFWorkbook(fs);//读取表 fs.Close(); var rowcount = -1; CARRIERVGMHelper ch = new CARRIERVGMHelper(CARRIER, book); foreach (var VGMItem in vgmlist) { if (VGMItem.ctnList.Count > 0) { foreach (var ctn in VGMItem.ctnList) { rowcount = ch.NEWROW(VGMItem, ctn, rowcount); } } } //保存 var filepath = Server.MapPath(VGMFILEPATH) + @"\" + vgmlist[0].smallFileName_excel; FileStream savefile = new FileStream(filepath, FileMode.Create);//创建一个文件 ch.book.Write(savefile); savefile.Close(); return vgmlist[0].smallFileName_excel; } } }