示例#1
0
        /// <summary>
        /// Grabs the selected boxes from the List Box.
        /// </summary>
        /// <returns>List of FBABox from the listbox.</returns>
        private List <FBABox> GetSelectedBoxes()
        {
            List <FBABox> selectedBoxes = new List <FBABox>();

            //get boxes from selected items in the listbox
            foreach (var item in boxListBox.SelectedItems)
            {
                if (item is FBABox) //check that only FBABox items are being grabbed
                {
                    FBABox newBox = (FBABox)item;
                    selectedBoxes.Add(newBox);
                }
            }

            return(selectedBoxes);
        }
        /// <summary>
        /// Reads an excel workbook and grabs the content of each Worksheet. Each worksheet
        /// represents a box. Each box is added to the shipment being processed.
        /// </summary>
        /// <param name="xlFile"></param>
        public void ReadExcelBook(string xlFile)
        {
            List <FBABox> boxes = new List <FBABox>();

            //get file info for excel epplus package wrapper class
            FileInfo exlBook = new FileInfo(xlFile);

            //get the excel file name for naming the PO
            var    splitPath = xlFile.Split('\\');
            var    fSplit    = splitPath[splitPath.Length - 1];
            var    name      = fSplit.Split('.');
            string poName    = name[0];

            //set the shipmentID
            Shipment.ShipmentID = poName;

            using (ExcelPackage xlPackage = new ExcelPackage(exlBook))
            {
                ExcelWorkbook book = xlPackage.Workbook;
                foreach (ExcelWorksheet sheet in book.Worksheets)
                {
                    //Console.WriteLine("Currently viewing sheet {0}", sheet.Name); //testing

                    //get the box/sheet number
                    var sname = sheet.Name.Split(new string[] { "Sheet" }, StringSplitOptions.None);
                    int boxNumber;
                    int.TryParse(sname[1], out boxNumber);

                    //get a boxID number with appropriate leading zeros. BoxNumber ID must be 3 digits long.
                    int countLen = boxNumber.ToString("D").Length;
                    if (countLen > 1) //use 1 leading zeros
                    {
                        countLen += 1;
                    }
                    else //use 2 leading zeros
                    {
                        countLen += 2;
                    }

                    //write a box ID with format of SHIPMENTID+U+001
                    var boxId = poName + "U" + boxNumber.ToString("D" + countLen.ToString());
                    int col   = 1;

                    //create a new box to be added to the shipment
                    FBABox newBox = new FBABox(boxId);
                    newBox.PO        = poName;
                    newBox.BoxNumber = boxNumber;

                    //iterate through the first 100 rows
                    for (int i = 1; i < 100; i++)
                    {
                        if (sheet.Cells[i, col].Value == null)
                        {
                            //as soon as a null value appears, break out of the for loop
                            break;
                        }
                        else
                        {   //else(if cell value is not null), add the item ID to the box
                            //add new item to box,
                            newBox.AddItem(new Item(sheet.Cells[i, col].Value.ToString()));
                        }
                    }//end of for loop

                    // add the new box to the list of boxes
                    boxes.Add(newBox);
                } // end of foreach loop, iterating throught each sheet in the workbook.
            }     //end of excelPackage USE block

            //set the boxes from excel file to the Shipment object
            Shipment.Boxes = boxes;
        }