コード例 #1
0
        public void CreateExcelFile_BlankCtor_Test()
        {
            // Arrange
            // path and sheet info for the excel file
            string       path          = Path.GetTempPath() + Guid.NewGuid() + ".xlsx";
            const string worksheetName = "test";

            IExcelData excelData = new ExcelData(path);

            // Act
            excelData.CreateExcelWorkSheet(path, worksheetName);

            // Assert
            bool exists = File.Exists(path);

            Assert.IsTrue(exists);
            Assert.IsTrue(excelData.Package != null);
            Assert.IsTrue(excelData.Worksheet != null);
            Assert.AreEqual(worksheetName, excelData.Worksheet.Name);

            // Now delete the excel file, as to not create hundreds of them on disk
            excelData.DeleteExcelFile();
            bool noLongerExists = File.Exists(path);

            Assert.IsFalse(noLongerExists);
        }
コード例 #2
0
        public void WriteHeaders_Test()
        {
            // Arrange
            // path and sheet info for the excel file
            string       path          = Path.GetTempPath() + Guid.NewGuid() + ".xlsx";
            const string worksheetName = "test";
            IExcelData   excelData     = new ExcelData(path);
            IExcelStyler excelStyler   = new ExcelStyler(excelData);

            excelData.CreateExcelWorkSheet(path, worksheetName);
            bool exists = File.Exists(path);

            Assert.IsTrue(exists);
            Assert.IsTrue(excelData.Package != null);
            Assert.IsTrue(excelData.Worksheet != null);
            Assert.AreEqual(worksheetName, excelData.Worksheet.Name);

            string[] headers = new string[]
            {
                "Marvel", "Captain", "Hulk", "Thor", "IronMan", "X-Men",
                "Spiderman", "Black Widow"
            };
            BaseExcelWriter baseExcelWriter = new ExcelWriter(excelData, excelStyler);

            // Act
            baseExcelWriter.WriteHeaders(headers);

            // Assert
            for (int i = 0; i < headers.Length; i++)
            {
                int col = i + 1;
                Assert.AreEqual(excelData.Worksheet.Cells[1, col].Text, headers[i]);
            }

            excelData.DeleteExcelFile();
            Assert.IsFalse(File.Exists(path));
        }
コード例 #3
0
        public void WriteShellTempsToExcelFile()
        {
            // Arrange
            // path and sheet info for the excel file
            string       path          = Path.GetTempPath() + Guid.NewGuid() + ".xlsx";
            const string worksheetName = "test";
            IExcelData   excelData     = new ExcelData(path);
            IExcelStyler excelStyler   = new ExcelStyler(excelData);

            excelData.CreateExcelWorkSheet(path, worksheetName);
            bool exists = File.Exists(path);

            Assert.IsTrue(exists);
            Assert.IsTrue(excelData.Package != null);
            Assert.IsTrue(excelData.Worksheet != null);
            Assert.AreEqual(worksheetName, excelData.Worksheet.Name);

            ExcelWriter excelWriter = new ExcelWriter(excelData, excelStyler);

            Random random = new Random();

            ShellTemperatureRecord[] temps = new ShellTemperatureRecord[1000];
            for (int i = 0; i < temps.Length; i++)
            {
                float?     lat    = null;
                float?     lon    = null;
                DeviceInfo device = new DeviceInfo
                {
                    DeviceAddress = "CraigsAddress",
                    DeviceName    = "Spiderman",
                    Id            = Guid.NewGuid()
                };

                if (i % 4 == 0)
                {
                    lat = random.Next(0, 1000);
                    lon = random.Next(0, 4000);
                }
                ShellTemperatureRecord temp = new ShellTemperatureRecord
                {
                    Id               = Guid.NewGuid(),
                    Latitude         = lat,
                    Longitude        = lon,
                    Temperature      = random.Next(20, 80),
                    RecordedDateTime = DateTime.Now,
                    Device           = device,
                    Comment          = i.ToString()
                };

                temps[i] = temp;
            }

            // add headers
            string[] headers = temps[0].GetType().GetProperties().Select(x => x.Name).ToArray();
            excelWriter.WriteHeaders(headers);
            excelWriter.WriteToExcelFile(temps);

            Assert.AreEqual(temps.Length, excelData.Worksheet.Dimension.End.Row - 1);

            excelData.DeleteExcelFile();
            Assert.IsFalse(File.Exists(path));
        }