Пример #1
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]);
            }
        }