示例#1
0
        public Wardrobe(String filename)
        {
            shirts    = new List <ClothingItem>();
            pants     = new List <ClothingItem>();
            shoes     = new List <ClothingItem>();
            errorText = "";
            //Create COM Objects. Create a COM object for everything that is referenced
            Excel.Application xlApp       = new Excel.Application();
            Excel.Workbook    xlWorkbook  = xlApp.Workbooks.Open(filename);
            Excel._Worksheet  xlWorksheet = xlWorkbook.Sheets[1];
            Excel.Range       xlRange     = xlWorksheet.UsedRange;

            int rowCount = xlRange.Rows.Count;
            int colCount = xlRange.Columns.Count;

            //iterate over the rows and columns and print to the console as it appears in the file
            //excel is not zero based!!
            String name  = "";
            String type  = "";
            String color = "";
            bool   error = false;

            for (int i = 1; i <= rowCount; i++)
            {
                for (int j = 1; j <= colCount; j++)
                {
                    //new line
                    if (j == 1)
                    {
                        name = xlRange.Cells[i, j].Value2;
                    }
                    else if (j == 2)
                    {
                        type = xlRange.Cells[i, j].Value2;
                    }
                    else if (j == 3)
                    {
                        color = xlRange.Cells[i, j].Value2;
                    }
                    else
                    {
                        //too many columns
                        error = true;
                    }
                    //write the value to the console
                    //if (xlRange.Cells[i, j] != null && xlRange.Cells[i, j].Value2 != null)
                    //Console.Write(xlRange.Cells[i, j].Value2.ToString() + "\t");
                }

                if (error == false)
                {
                    ClothingItem temp = new ClothingItem(getType(type, i), getColor(color, i), name);
                    if (temp.Category == ClothingCategory.Shirt)
                    {
                        Shirts.Add(temp);
                    }
                    else if (temp.Category == ClothingCategory.Pants)
                    {
                        Pants.Add(temp);
                    }
                    else if (temp.Category == ClothingCategory.Shoes)
                    {
                        Shoes.Add(temp);
                    }
                }
                else
                {
                    throw new InvalidOperationException("Excel file is incorrectly constructed.");
                }
            }

            //cleanup
            GC.Collect();
            GC.WaitForPendingFinalizers();

            //rule of thumb for releasing com objects:
            //  never use two dots, all COM objects must be referenced and released individually
            //  ex: [somthing].[something].[something] is bad

            //release com objects to fully kill excel process from running in the background
            Marshal.ReleaseComObject(xlRange);
            Marshal.ReleaseComObject(xlWorksheet);

            //close and release
            xlWorkbook.Close();
            Marshal.ReleaseComObject(xlWorkbook);

            //quit and release
            xlApp.Quit();
            Marshal.ReleaseComObject(xlApp);
        }
示例#2
0
 public bool PassesShirtTests(ClothingItem shirt, byte eventType)
 {
     return(shirt.UseCnt < 1 && ((shirt.EventType & eventType) > 0));
 }