Ejemplo n.º 1
0
        public ConfigSheet(ExcelPackage excelPackage)
        {
            ExcelWorksheet configSheet = ExcelPackageHelper.GetWorksheet(excelPackage, SHEET_NAME);

            if (configSheet == null)
            {
                AddDefaultConfigSheet(excelPackage);
                return;
            }

            List <object> statuses = ExcelPackageHelper.GetColumnByHeader(configSheet, STATUS_NAME);
            List <object> actives  = ExcelPackageHelper.GetColumnByHeader(configSheet, ACTIVE_NAME);

            if (statuses.Count > 0)
            {
                Statuses = new List <Status>();
                for (int i = 0; i < statuses.Count; i++)
                {
                    bool isActive = true;
                    if (actives.Count > i)
                    {
                        isActive = (actives[i].ToString() == IS_ACTIVE);
                    }
                    Statuses.Add(new Status(statuses[i].ToString(), isActive));
                }
            }
            else
            {
                SetDefaultStatuses();
            }

            Categories = ExcelPackageHelper.GetColumnByHeader(configSheet, CATEGORY_NAME).Cast <string>().ToList();
            if (Categories.Count == 0)
            {
                SetDefaultCategories();
            }

            List <object> ids = ExcelPackageHelper.GetColumnByHeader(configSheet, ID_NAME);

            if (ids.Count > 0)
            {
                MaxId = (int)ids[0];
            }
            else
            {
                MaxId = DEFAULT_ID;
            }

            WriteConfigSheet(configSheet);             //standardize format
        }
Ejemplo n.º 2
0
        public void ExcelPackageHelper_SetColumnByHeader_GetColumnByHeader()
        {
            //arrange
            ExcelPackage   package   = new ExcelPackage();
            string         name      = "Title";
            ExcelWorksheet worksheet = ExcelPackageHelper.AddWorksheet(package, name);
            List <string>  headers   = new List <string>()
            {
                "ColA", "ColB"
            };
            List <object> col1 = new List <object>()
            {
                null, 123, "abc", "", new DateTime(1999, 3, 2)
            };
            List <object> col2 = new List <object>()
            {
                456, "", null, null, null, null, 789
            };

            //act
            ExcelPackageHelper.AppendRow(worksheet, headers);
            ExcelPackageHelper.SetColumnByHeader(worksheet, headers[0], col1);
            ExcelPackageHelper.SetColumnByHeader(worksheet, headers[1], col2);
            List <object> result1 = ExcelPackageHelper.GetColumnByHeader(worksheet, headers[0]);
            List <object> result2 = ExcelPackageHelper.GetColumnByHeader(worksheet, headers[1]);

            //assert
            Assert.AreEqual(headers[0], worksheet.Cells["A1"].Value.ToString());
            Assert.AreEqual(headers[1], worksheet.Cells["B1"].Value.ToString());
            Assert.AreEqual(2, ExcelPackageHelper.CountColumns(worksheet));
            Assert.AreEqual(Math.Max(col1.Count, col2.Count) + 1, ExcelPackageHelper.CountRows(worksheet));
            for (int i = 0; i < col1.Count; i++)
            {
                Assert.AreEqual(col1[i], result1[i]);
            }
            for (int i = 0; i < col2.Count; i++)
            {
                Assert.AreEqual(col2[i], result2[i]);
            }
        }