using DocumentFormat.OpenXml; using DocumentFormat.OpenXml.Packaging; using DocumentFormat.OpenXml.Spreadsheet; using Furion.FriendlyException; using Furion.Logging; using Microsoft.VisualBasic; using Myshipping.Application.EDI.VOLTA; using NPOI.HPSF; using NPOI.HSSF.UserModel; using NPOI.OpenXmlFormats.Wordprocessing; using NPOI.SS.Formula.Eval; using NPOI.SS.UserModel; using NPOI.XSSF.UserModel; using StackExchange.Profiling.Internal; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; using Yitter.IdGenerator; namespace Myshipping.Application.EDI.SeaLead { /// /// 海领截单帮助 /// public static class SeaLeadEdiHelper { #region 生成VOLTA申报报文(截单) /// /// 生成VOLTA申报报文(截单) /// /// VOLTA申报详情 /// 返回回执 public static CommonWebApiResult CreateSIEdi(VOLTAEDIBaseModel model,string fileAbsPath) { CommonWebApiResult result = new CommonWebApiResult { succ = false }; //日志 var logger = Log.CreateLogger(nameof(SeaLeadEdiHelper)); try { ValidateInput(model); string bno = model.BookingId; DateTime nowDate = DateTime.Now; var excelFileName = string.Empty; //读取文件 //var file = new FileStream(fileAbsPath, FileMode.Open); var fileName = $"{model.BookingId}_SLS_{DateTime.Now.Ticks.ToString()}.xlsx";//名称 excelFileName = Path.Combine(model.FilePath, fileName); System.IO.File.Copy(fileAbsPath, excelFileName, true); using (SpreadsheetDocument spreadSheetDocument = SpreadsheetDocument.Open(excelFileName, true)) { WorkbookPart wbPart = spreadSheetDocument.WorkbookPart; IEnumerable sheets = spreadSheetDocument.WorkbookPart.Workbook.GetFirstChild().Elements(); var sheetNameArgs = sheets.Select(sh => { return sh.Id.Value; }).ToArray(); int startNo = 1; foreach (var sheet in sheetNameArgs) { var sheetInfo = new ParserSheetInfo(); WorksheetPart worksheetPart = (WorksheetPart)spreadSheetDocument.WorkbookPart.GetPartById(sheet); Worksheet workSheet = worksheetPart.Worksheet; SheetData sheetData = workSheet.GetFirstChild(); var rowList = sheetData.Descendants().ToList(); if (startNo == 1) { var baseColsDict = rowList.ElementAt(rowList.Count - 1).Select((col, idx) => { var cell = (Cell)col; var colInfo = new ParserColsInfo { colsIndx = idx, colsName = (string)GetCellValue(spreadSheetDocument, cell), }; return colInfo; }).ToDictionary(a => a.colsIndx, b => b.colsName); var newRow = new Row { RowIndex = (UInt32)(rowList.Count + 1) }; for (int i = 0; i < baseColsDict.Count; i++) { #region 主信息填值 if (baseColsDict[i].Equals("Booking ID", StringComparison.OrdinalIgnoreCase)) { GenerateCellInfoOpenXml(model.BookingId, i, newRow); } else if (baseColsDict[i].Equals("Shipper", StringComparison.OrdinalIgnoreCase)) { GenerateCellInfoOpenXml(model.ShpperName, i, newRow); } else if (baseColsDict[i].Equals("Consignee", StringComparison.OrdinalIgnoreCase)) { GenerateCellInfoOpenXml(model.ConsigneeName, i, newRow); } else if (baseColsDict[i].Equals("Notify Party", StringComparison.OrdinalIgnoreCase)) { GenerateCellInfoOpenXml(model.NotifyName, i, newRow); } else if (baseColsDict[i].Equals("Additional Notify Party", StringComparison.OrdinalIgnoreCase)) { GenerateCellInfoOpenXml(model.NotifySecondName, i, newRow); } else if (baseColsDict[i].Equals("Shipper Address", StringComparison.OrdinalIgnoreCase)) { GenerateCellInfoOpenXml(model.ShpperAddr, i, newRow); } else if (baseColsDict[i].Equals("Consignee Address", StringComparison.OrdinalIgnoreCase)) { GenerateCellInfoOpenXml(model.ConsigneeAddr, i, newRow); } else if (baseColsDict[i].Equals("Notify Party Address", StringComparison.OrdinalIgnoreCase)) { GenerateCellInfoOpenXml(model.NotifyAddr, i, newRow); } else if (baseColsDict[i].Equals("Additional Notify Party Address", StringComparison.OrdinalIgnoreCase)) { GenerateCellInfoOpenXml(model.NotifySecondAddr, i, newRow); } else if (baseColsDict[i].Equals("Shipper Export Reference", StringComparison.OrdinalIgnoreCase)) { GenerateCellInfoOpenXml(model.ShipperExportReference, i, newRow); } else if (baseColsDict[i].Equals("Shipper Export Reference Type", StringComparison.OrdinalIgnoreCase)) { GenerateCellInfoOpenXml(model.ShipperExportReferenceType, i, newRow); } else if (baseColsDict[i].Equals("Payment Term", StringComparison.OrdinalIgnoreCase)) { GenerateCellInfoOpenXml(model.BLFRT, i, newRow); } else if (baseColsDict[i].Equals("Marks and Numbers", StringComparison.OrdinalIgnoreCase)) { GenerateCellInfoOpenXml(model.Marks, i, newRow); } else if (baseColsDict[i].Equals("Description of Pkgs", StringComparison.OrdinalIgnoreCase)) { GenerateCellInfoOpenXml(model.CargoDescription, i, newRow); } else if (baseColsDict[i].Equals("Document type", StringComparison.OrdinalIgnoreCase)) { GenerateCellInfoOpenXml(model.DocumentTypeCode, i, newRow); } else if (baseColsDict[i].Equals("Port Of Loading", StringComparison.OrdinalIgnoreCase)) { GenerateCellInfoOpenXml(model.LoadPort, i, newRow); } else if (baseColsDict[i].Equals("Port Of Discharge", StringComparison.OrdinalIgnoreCase)) { GenerateCellInfoOpenXml(model.DischargePort, i, newRow); } else if (baseColsDict[i].Equals("Place of Receipt by Precarrier", StringComparison.OrdinalIgnoreCase)) { GenerateCellInfoOpenXml(model.PlaceOfOrigin, i, newRow); } else if (baseColsDict[i].Equals("Place of Final Delivery by On Carrier", StringComparison.OrdinalIgnoreCase)) { GenerateCellInfoOpenXml(model.FinalDestination, i, newRow); } else if (baseColsDict[i].Equals("HS Code", StringComparison.OrdinalIgnoreCase)) { GenerateCellInfoOpenXml(model.HSCode, i, newRow); } else if (baseColsDict[i].Equals("Precarriage", StringComparison.OrdinalIgnoreCase)) { GenerateCellInfoOpenXml(model.Precarriage, i, newRow); } #endregion } sheetData.Append(newRow); } else if(startNo == 2) { var ctnColsDict = rowList.ElementAt(rowList.Count - 1).Select((col, idx) => { var cell = (Cell)col; var colInfo = new ParserColsInfo { colsIndx = idx, colsName = (string)GetCellValue(spreadSheetDocument, cell), }; return colInfo; }).ToDictionary(a => a.colsIndx, b => b.colsName); for (int j = 0; j < model.ContaList.Count; j++) { var newCtnRow = new Row { RowIndex = (UInt32)(rowList.Count + 1) }; GenerateCellInfoOpenXml(model.ContaList[j].SNo.ToString(), 0, newCtnRow); for (int k = 0; k < ctnColsDict.Count; k++) { #region 箱信息填写 if (ctnColsDict[k].Equals("Container Serial Number", StringComparison.OrdinalIgnoreCase)) { GenerateCellInfoOpenXml(model.ContaList[j].ContaNo, k + 1, newCtnRow); } else if (ctnColsDict[k].Equals("Container type", StringComparison.OrdinalIgnoreCase)) { GenerateCellInfoOpenXml(model.ContaList[j].CtnType, k + 1, newCtnRow); } else if (ctnColsDict[k].Equals("Outer package ID", StringComparison.OrdinalIgnoreCase)) { GenerateCellInfoOpenXml(model.ContaList[j].OuterPackageID, k + 1, newCtnRow); } else if (ctnColsDict[k].Equals("Type of Package", StringComparison.OrdinalIgnoreCase)) { GenerateCellInfoOpenXml(model.ContaList[j].EdiPkgs, k + 1, newCtnRow); } else if (ctnColsDict[k].Equals("Number of outer packages", StringComparison.OrdinalIgnoreCase)) { GenerateCellInfoOpenXml(model.ContaList[j].Qty.HasValue ? model.ContaList[j].Qty.Value.ToString("0.##") : "", k + 1, newCtnRow); } else if (ctnColsDict[k].Equals("Weight value", StringComparison.OrdinalIgnoreCase)) { GenerateCellInfoOpenXml(model.ContaList[j].GWt.HasValue ? model.ContaList[j].GWt.Value.ToString("0.##") : "", k + 1, newCtnRow); } else if (ctnColsDict[k].Equals("Gross Weight UoM", StringComparison.OrdinalIgnoreCase)) { GenerateCellInfoOpenXml(model.ContaList[j].WTUnit, k + 1, newCtnRow); } else if (ctnColsDict[k].Equals("Volume value", StringComparison.OrdinalIgnoreCase)) { GenerateCellInfoOpenXml(model.ContaList[j].CBM.HasValue ? model.ContaList[j].CBM.Value.ToString("0.##") : "", k + 1, newCtnRow); } else if (ctnColsDict[k].Equals("Gross Volume UoM", StringComparison.OrdinalIgnoreCase)) { GenerateCellInfoOpenXml(model.ContaList[j].CBMUnit, k + 1, newCtnRow); } else if (ctnColsDict[k].Equals("Length", StringComparison.OrdinalIgnoreCase)) { GenerateCellInfoOpenXml(model.ContaList[j].Length.HasValue ? model.ContaList[j].Length.Value.ToString("0.##") : "", k + 1, newCtnRow); } else if (ctnColsDict[k].Equals("Width", StringComparison.OrdinalIgnoreCase)) { GenerateCellInfoOpenXml(model.ContaList[j].Width.HasValue ? model.ContaList[j].Width.Value.ToString("0.##") : "", k + 1, newCtnRow); } else if (ctnColsDict[k].Equals("Height", StringComparison.OrdinalIgnoreCase)) { GenerateCellInfoOpenXml(model.ContaList[j].Height.HasValue ? model.ContaList[j].Height.Value.ToString("0.##") : "", k + 1, newCtnRow); } else if (ctnColsDict[k].Equals("Unit of Measure", StringComparison.OrdinalIgnoreCase)) { GenerateCellInfoOpenXml(model.ContaList[j].UnitofMeasure, k + 1, newCtnRow); } else if (ctnColsDict[k].Equals("Vehicle Identification Number", StringComparison.OrdinalIgnoreCase)) { GenerateCellInfoOpenXml(model.ContaList[j].VehicleIdNumber, k + 1, newCtnRow); } else if (ctnColsDict[k].Equals("Vehicle Registration PI", StringComparison.OrdinalIgnoreCase)) { GenerateCellInfoOpenXml(model.ContaList[j].VehicleRegistPI, k + 1, newCtnRow); } else if (ctnColsDict[k].Equals("Seal Number 1", StringComparison.OrdinalIgnoreCase)) { GenerateCellInfoOpenXml(model.ContaList[j].SealNo, k + 1, newCtnRow); } else if (ctnColsDict[k].Equals("Seal Number 2", StringComparison.OrdinalIgnoreCase)) { GenerateCellInfoOpenXml(model.ContaList[j].SealNumber2, k + 1, newCtnRow); } else if (ctnColsDict[k].Equals("Seal Number 3", StringComparison.OrdinalIgnoreCase)) { GenerateCellInfoOpenXml(model.ContaList[j].SealNumber3, k + 1, newCtnRow); } else if (ctnColsDict[k].Equals("Seal Number 4", StringComparison.OrdinalIgnoreCase)) { GenerateCellInfoOpenXml(model.ContaList[j].SealNumber4, k + 1, newCtnRow); } else if (ctnColsDict[k].Equals("Seal Number 5", StringComparison.OrdinalIgnoreCase)) { GenerateCellInfoOpenXml(model.ContaList[j].SealNumber5, k + 1, newCtnRow); } #endregion } sheetData.Append(newCtnRow); } } startNo++; } wbPart.Workbook.Save(); //byte[] pReadByte = new byte[file.Length]; ////var bytes = file.(); //var filestream = new FileStream(excelFileName, FileMode.OpenOrCreate, FileAccess.ReadWrite); //file.Write(pReadByte, 0, pReadByte.Length); ////ar fileStream = new FileStream(filepath, FileMode.Create, FileAccess.Write, FileShare.Read); ////fileStream.Write(bytes, 0, bytes.Length); //file.Flush(); //file.Close(); //file.Dispose(); //document.Close(); } /* var excelwork = new XSSFWorkbook(file); //截单主信息 var sheetBase = excelwork.GetSheetAt(0); if (sheetBase == null) throw Oops.Bah("读取模板Sheet Header data失败"); var lstLabelRow = sheetBase.GetRow(sheetBase.LastRowNum - 1); Dictionary baseColsDict = lstLabelRow.Cells.Select((p, idx) => { if (p != null) return new { Name = p.StringCellValue?.Trim(), Idx = idx }; return new { Name = "", Idx = idx }; }).Where(p => !string.IsNullOrWhiteSpace(p.Name)).ToDictionary(a => a.Idx, b => b.Name); var newRow = (XSSFRow)sheetBase.CreateRow(sheetBase.LastRowNum); for (int i = 0; i < lstLabelRow.Cells.Count; i++) { if (i > baseColsDict.Max(a => a.Key)) break; #region 主信息填值 if (baseColsDict[i].Equals("Booking ID", StringComparison.OrdinalIgnoreCase)) { GenerateCellInfo(model.BookingId, i, newRow); } else if (baseColsDict[i].Equals("Shipper", StringComparison.OrdinalIgnoreCase)) { GenerateCellInfo(model.ShpperName, i, newRow); } else if (baseColsDict[i].Equals("Consignee", StringComparison.OrdinalIgnoreCase)) { GenerateCellInfo(model.ConsigneeName, i, newRow); } else if (baseColsDict[i].Equals("Notify Party", StringComparison.OrdinalIgnoreCase)) { GenerateCellInfo(model.NotifyName, i, newRow); } else if (baseColsDict[i].Equals("Additional Notify Party", StringComparison.OrdinalIgnoreCase)) { GenerateCellInfo(model.NotifySecondName, i, newRow); } else if (baseColsDict[i].Equals("Shipper Address", StringComparison.OrdinalIgnoreCase)) { GenerateCellInfo(model.ShpperAddr, i, newRow); } else if (baseColsDict[i].Equals("Consignee Address", StringComparison.OrdinalIgnoreCase)) { GenerateCellInfo(model.ConsigneeAddr, i, newRow); } else if (baseColsDict[i].Equals("Notify Party Address", StringComparison.OrdinalIgnoreCase)) { GenerateCellInfo(model.NotifyAddr, i, newRow); } else if (baseColsDict[i].Equals("Additional Notify Party Address", StringComparison.OrdinalIgnoreCase)) { GenerateCellInfo(model.NotifySecondAddr, i, newRow); } else if (baseColsDict[i].Equals("Shipper Export Reference", StringComparison.OrdinalIgnoreCase)) { GenerateCellInfo(model.ShipperExportReference, i, newRow); } else if (baseColsDict[i].Equals("Shipper Export Reference Type", StringComparison.OrdinalIgnoreCase)) { GenerateCellInfo(model.ShipperExportReferenceType, i, newRow); } else if (baseColsDict[i].Equals("Payment Term", StringComparison.OrdinalIgnoreCase)) { GenerateCellInfo(model.BLFRT, i, newRow); } else if (baseColsDict[i].Equals("Marks and Numbers", StringComparison.OrdinalIgnoreCase)) { GenerateCellInfo(model.Marks, i, newRow); } else if (baseColsDict[i].Equals("Description of Pkgs", StringComparison.OrdinalIgnoreCase)) { GenerateCellInfo(model.CargoDescription, i, newRow); } else if (baseColsDict[i].Equals("Document type", StringComparison.OrdinalIgnoreCase)) { GenerateCellInfo(model.DocumentTypeCode, i, newRow); } else if (baseColsDict[i].Equals("Port Of Loading", StringComparison.OrdinalIgnoreCase)) { GenerateCellInfo(model.LoadPort, i, newRow); } else if (baseColsDict[i].Equals("Port Of Discharge", StringComparison.OrdinalIgnoreCase)) { GenerateCellInfo(model.DischargePort, i, newRow); } else if (baseColsDict[i].Equals("Place of Receipt by Precarrier", StringComparison.OrdinalIgnoreCase)) { GenerateCellInfo(model.PlaceOfOrigin, i, newRow); } else if (baseColsDict[i].Equals("Place of Final Delivery by On Carrier", StringComparison.OrdinalIgnoreCase)) { GenerateCellInfo(model.FinalDestination, i, newRow); } else if (baseColsDict[i].Equals("HS Code", StringComparison.OrdinalIgnoreCase)) { GenerateCellInfo(model.HSCode, i, newRow); } else if (baseColsDict[i].Equals("Precarriage", StringComparison.OrdinalIgnoreCase)) { GenerateCellInfo(model.Precarriage, i, newRow); } #endregion } excelwork.SetActiveSheet(1); //写箱信息 var sheetCtn = excelwork.GetSheetAt(1); if (sheetCtn == null) throw Oops.Bah("读取模板Sheet Container data失败"); int ctnStartNo = sheetCtn.LastRowNum + 1; var lstCtnLabelRow = sheetCtn.GetRow(sheetCtn.LastRowNum); //var noLabelRow = sheetCtn.GetRow(1); Dictionary ctnColsDict = lstCtnLabelRow.Cells.Select((p, idx) => { if (p != null) return new { Name = p.StringCellValue?.Trim(), Idx = idx }; return new { Name = "", Idx = idx }; }).ToDictionary(a => a.Idx, b => b.Name); for (int j = 0; j < model.ContaList.Count; j++) { var newCtnRow = (XSSFRow)sheetCtn.CreateRow(ctnStartNo); GenerateCellInfoWithInt(model.ContaList[j].SNo, 0, newCtnRow); for (int k = 0; k < lstCtnLabelRow.Cells.Count; k++) { if (k > ctnColsDict.Max(a => a.Key)) break; #region 箱信息填写 if (ctnColsDict[k].Equals("Item Number", StringComparison.OrdinalIgnoreCase)) { GenerateCellInfoWithInt(model.ContaList[j].SNo, k + 1, newCtnRow); } else if (ctnColsDict[k].Equals("Container Serial Number", StringComparison.OrdinalIgnoreCase)) { GenerateCellInfo(model.ContaList[j].ContaNo, k + 1, newCtnRow); } else if (ctnColsDict[k].Equals("Container type", StringComparison.OrdinalIgnoreCase)) { GenerateCellInfo(model.ContaList[j].CtnType, k + 1, newCtnRow); } else if (ctnColsDict[k].Equals("Outer package ID", StringComparison.OrdinalIgnoreCase)) { GenerateCellInfo(model.ContaList[j].OuterPackageID, k + 1, newCtnRow); } else if (ctnColsDict[k].Equals("Type of Package", StringComparison.OrdinalIgnoreCase)) { GenerateCellInfo(model.ContaList[j].EdiPkgs, k + 1, newCtnRow); } else if (ctnColsDict[k].Equals("Number of outer packages", StringComparison.OrdinalIgnoreCase)) { GenerateCellInfo(model.ContaList[j].Qty.HasValue ? model.ContaList[j].Qty.Value.ToString("0.##") : "", k + 1, newCtnRow); } else if (ctnColsDict[k].Equals("Weight value", StringComparison.OrdinalIgnoreCase)) { GenerateCellInfo(model.ContaList[j].GWt.HasValue ? model.ContaList[j].GWt.Value.ToString("0.##") : "", k + 1, newCtnRow); } else if (ctnColsDict[k].Equals("Gross Weight UoM", StringComparison.OrdinalIgnoreCase)) { GenerateCellInfo(model.ContaList[j].WTUnit, k + 1, newCtnRow); } else if (ctnColsDict[k].Equals("Volume value", StringComparison.OrdinalIgnoreCase)) { GenerateCellInfo(model.ContaList[j].CBM.HasValue ? model.ContaList[j].CBM.Value.ToString("0.##") : "", k + 1, newCtnRow); } else if (ctnColsDict[k].Equals("Gross Volume UoM", StringComparison.OrdinalIgnoreCase)) { GenerateCellInfo(model.ContaList[j].CBMUnit, k + 1, newCtnRow); } else if (ctnColsDict[k].Equals("Length", StringComparison.OrdinalIgnoreCase)) { GenerateCellInfo(model.ContaList[j].Length.HasValue? model.ContaList[j].Length.Value.ToString("0.##"):"", k + 1, newCtnRow); } else if (ctnColsDict[k].Equals("Width", StringComparison.OrdinalIgnoreCase)) { GenerateCellInfo(model.ContaList[j].Width.HasValue ? model.ContaList[j].Width.Value.ToString("0.##") : "", k + 1, newCtnRow); } else if (ctnColsDict[k].Equals("Height", StringComparison.OrdinalIgnoreCase)) { GenerateCellInfo(model.ContaList[j].Height.HasValue ? model.ContaList[j].Height.Value.ToString("0.##") : "", k + 1, newCtnRow); } else if (ctnColsDict[k].Equals("Unit of Measure", StringComparison.OrdinalIgnoreCase)) { GenerateCellInfo(model.ContaList[j].UnitofMeasure, k + 1, newCtnRow); } else if (ctnColsDict[k].Equals("Vehicle Identification Number", StringComparison.OrdinalIgnoreCase)) { GenerateCellInfo(model.ContaList[j].VehicleIdNumber, k + 1, newCtnRow); } else if (ctnColsDict[k].Equals("Vehicle Registration PI", StringComparison.OrdinalIgnoreCase)) { GenerateCellInfo(model.ContaList[j].VehicleRegistPI, k + 1, newCtnRow); } else if (ctnColsDict[k].Equals("Seal Number 1", StringComparison.OrdinalIgnoreCase)) { GenerateCellInfo(model.ContaList[j].SealNo, k + 1, newCtnRow); } else if (ctnColsDict[k].Equals("Seal Number 2", StringComparison.OrdinalIgnoreCase)) { GenerateCellInfo(model.ContaList[j].SealNumber2, k + 1, newCtnRow); } else if (ctnColsDict[k].Equals("Seal Number 3", StringComparison.OrdinalIgnoreCase)) { GenerateCellInfo(model.ContaList[j].SealNumber3, k + 1, newCtnRow); } else if (ctnColsDict[k].Equals("Seal Number 4", StringComparison.OrdinalIgnoreCase)) { GenerateCellInfo(model.ContaList[j].SealNumber4, k + 1, newCtnRow); } else if (ctnColsDict[k].Equals("Seal Number 5", StringComparison.OrdinalIgnoreCase)) { GenerateCellInfo(model.ContaList[j].SealNumber5, k + 1, newCtnRow); } else { GenerateCellInfo("", k + 1, newCtnRow); } #endregion } ctnStartNo++; } excelwork.SetActiveSheet(0); var filestream = new FileStream(excelFileName, FileMode.OpenOrCreate, FileAccess.ReadWrite); excelwork.Write(filestream); excelwork.Close(); //filestream.Flush(); //filestream.Position = 0; */ // //filestream.Dispose(); result.succ = true; result.extra = excelFileName; result.extra2 = $"截单样本:{model.Vessel} {model.VoyNo} {model.BookingId} SLS"; } catch (Exception ex) { result.succ = false; result.msg = ex.Message; } return result; } #endregion public static string GetCellValue(SpreadsheetDocument document, Cell cell) { SharedStringTablePart stringTablePart = document.WorkbookPart.SharedStringTablePart; string value = String.Empty; if (cell.CellValue != null) value = cell.CellValue.InnerXml; if (cell.DataType != null && (cell.DataType.Value == CellValues.SharedString || cell.DataType.Value == CellValues.String || cell.DataType.Value == CellValues.Number)) { return stringTablePart.SharedStringTable.ChildElements[Int32.Parse(value)].InnerText; } else //浮点数和日期对应的cell.DataType都为NULL { // DateTime.FromOADate((double.Parse(value)); 如果确定是日期就可以直接用过该方法转换为日期对象,可是无法确定DataType==NULL的时候这个CELL 数据到底是浮点型还是日期.(日期被自动转换为浮点 return value; } } private static void GenerateCellInfo(string val, int cellIndex, XSSFRow row, NPOI.SS.UserModel.CellType cellType = NPOI.SS.UserModel.CellType.String) { if (!string.IsNullOrWhiteSpace(val)) { var currCell = (XSSFCell)row.CreateCell(cellIndex, cellType); if (cellType == NPOI.SS.UserModel.CellType.Numeric) { currCell.SetCellValue(double.Parse(val)); } else { currCell.SetCellValue(val); } } else { var currCell = (XSSFCell)row.CreateCell(cellIndex, cellType); currCell.SetCellValue(new XSSFRichTextString("")); } } private static void GenerateCellInfoWithInt(int val, int cellIndex, XSSFRow row) { var currCell = (XSSFCell)row.CreateCell(cellIndex, NPOI.SS.UserModel.CellType.String); currCell.SetCellValue(val.ToString()); } private static void GenerateCellInfoOpenXml(string val, int cellIndex, Row row) { if (!string.IsNullOrWhiteSpace(val)) { var cell = new Cell { CellValue = new DocumentFormat.OpenXml.Spreadsheet.CellValue(val), DataType = new EnumValue(CellValues.String), }; row.AppendChild(cell); } else { var cell = new Cell { CellValue = new DocumentFormat.OpenXml.Spreadsheet.CellValue(""), DataType = new EnumValue(CellValues.String) }; row.AppendChild(cell); } } #region 校验VOLTA请求参数s /// /// 校验VOLTA请求参数 /// /// VOLTA请求参数 public static void ValidateInput(VOLTAEDIBaseModel model) { StringBuilder msgBuilder = new StringBuilder(); if (string.IsNullOrEmpty(model.BookingId)) msgBuilder.AppendLine("提单号不能为空"); if (string.IsNullOrEmpty(model.ShpperName)) msgBuilder.AppendLine("发货人名称不能为空"); if (string.IsNullOrEmpty(model.ShpperAddr)) msgBuilder.AppendLine("发货人地址不能为空"); if (!string.IsNullOrEmpty(model.ShpperAddr)) { if (model.ShpperAddr.Length > 450) msgBuilder.AppendLine("发货人地址不能大于450个字符"); } if (string.IsNullOrEmpty(model.ConsigneeName)) msgBuilder.AppendLine("收货人名称不能为空"); if (string.IsNullOrEmpty(model.ConsigneeAddr)) msgBuilder.AppendLine("收货人地址不能为空"); if (!string.IsNullOrEmpty(model.ConsigneeAddr)) { if (model.ConsigneeAddr.Length > 450) msgBuilder.AppendLine("收货人地址不能大于450个字符"); } if (string.IsNullOrEmpty(model.NotifyName)) msgBuilder.AppendLine("通知人名称不能为空"); if (string.IsNullOrEmpty(model.NotifyAddr)) msgBuilder.AppendLine("通知人地址不能为空"); if (!string.IsNullOrEmpty(model.NotifyAddr)) { if (model.NotifyAddr.Length > 450) msgBuilder.AppendLine("通知人地址不能大于450个字符"); } if (!string.IsNullOrEmpty(model.NotifySecondName) && string.IsNullOrEmpty(model.NotifySecondAddr)) msgBuilder.AppendLine("通知人1地址不能为空"); if (string.IsNullOrEmpty(model.NotifySecondName) && !string.IsNullOrEmpty(model.NotifySecondAddr)) msgBuilder.AppendLine("通知人1名称不能为空"); if (!string.IsNullOrEmpty(model.NotifySecondAddr)) { if (model.NotifySecondAddr.Length > 450) msgBuilder.AppendLine("通知人1地址不能大于450个字符"); } if (string.IsNullOrEmpty(model.PlaceOfOrigin)) msgBuilder.AppendLine("发货地不能为空"); if (string.IsNullOrEmpty(model.LoadPort)) msgBuilder.AppendLine("起运港不能为空"); if (string.IsNullOrEmpty(model.DischargePort)) msgBuilder.AppendLine("卸货港不能为空"); if (string.IsNullOrEmpty(model.FinalDestination)) msgBuilder.AppendLine("最终目的港不能为空"); if (string.IsNullOrEmpty(model.PlaceOfDelivery)) msgBuilder.AppendLine("最终交货地不能为空"); if (string.IsNullOrEmpty(model.CargoDescription)) msgBuilder.AppendLine("品名不能为空"); if (string.IsNullOrEmpty(model.DocumentTypeCode)) msgBuilder.AppendLine("文档类型不能为空"); if (string.IsNullOrEmpty(model.BLFRT)) msgBuilder.AppendLine("付款方式不能为空"); if (string.IsNullOrEmpty(model.HSCode)) { msgBuilder.AppendLine("HSCode不能为空"); } else { if (!Regex.IsMatch(model.HSCode, "[0-9]{4}\\.[0-9]{2}\\.[0-9]{2}\\.[0-9]{2}") || model.HSCode.Length != 13) { msgBuilder.AppendLine("HSCode格式错误,请参考格式 0000.00.00.00"); } } if (!string.IsNullOrEmpty(model.CargoDescription)) { if (model.CargoDescription.Length > 3000) msgBuilder.AppendLine("品名不能大于3000个字符"); } if (string.IsNullOrEmpty(model.Marks)) msgBuilder.AppendLine("唛头不能为空"); if (!string.IsNullOrEmpty(model.Marks)) { if (model.Marks.Length > 1000) msgBuilder.AppendLine("唛头不能大于1000个字符"); } if (model.ContaList == null || model.ContaList.Count == 0) msgBuilder.AppendLine("箱明细不能为空"); model.ContaList.ForEach(ctn => { StringBuilder ctnMsgBuilder = new StringBuilder(); if (string.IsNullOrWhiteSpace(ctn.ContaNo)) { ctnMsgBuilder.Append("箱号不能为空;"); } else { if (ctn.ContaNo.Length > 11) { ctnMsgBuilder.Append("箱号长度不能大于11;"); } } if (string.IsNullOrWhiteSpace(ctn.CtnType)) { ctnMsgBuilder.Append("箱型不能为空;"); } if (string.IsNullOrWhiteSpace(ctn.SealNo)) { ctnMsgBuilder.Append("铅封号不能为空;"); } if (string.IsNullOrWhiteSpace(ctn.WTUnit)) { ctnMsgBuilder.Append("重量单位不能为空;"); } if (string.IsNullOrWhiteSpace(ctn.CBMUnit)) { ctnMsgBuilder.Append("尺寸单位不能为空;"); } if (!ctn.GWt.HasValue) { ctnMsgBuilder.Append("毛重不能为空;"); } if (!ctn.CBM.HasValue) { ctnMsgBuilder.Append("尺寸不能为空;"); } if (!ctn.Qty.HasValue) { ctnMsgBuilder.Append("件数不能为空;"); } if (string.IsNullOrWhiteSpace(ctn.EdiPkgs)) { ctnMsgBuilder.Append("包装不能为空;"); } if (ctnMsgBuilder.Length > 0) { msgBuilder.AppendLine($"箱【{ctn.SNo}】{ctnMsgBuilder.ToString()}"); } }); if (msgBuilder.Length > 0) throw Oops.Bah(msgBuilder.ToString()); } #endregion } }