public void CreateExcelFileTest()
        {
            const string filename = "SimpleBook2.xlsx";
            var target = new GenericWriteExcel();
            var rows = SimpleExcelRowFactory.CreateSimpleRows();

            var actual = target.CreateExcelFile(rows);
            using (var file = File.OpenWrite(filename))
            {
                file.Write(actual.GetBuffer(), 0, (int) actual.Length);
            }

            using (var file = File.OpenRead(filename))
            {
                var excel = new GenericReadExcel {HasHeader = true};
                var actualRows = excel.ExtractRows(file, (header, row) => new SimpleExcelRow
                {
                    Col1 = row.GetValue<int>(1),
                    Col2 = row.GetValue<string>(2),
                    Col3 = row.GetValue<DateTime>(3)
                }, 1);
                var actualRowsList = new List<SimpleExcelRow>();
                actualRowsList.AddRange(actualRows);

                AssertEx.PropertyValuesAreEquals(actualRowsList, rows);
            }
        }
Ejemplo n.º 2
0
        private ActionResult ExportSearch <T>(IEnumerable <T> result, string type) where T : class
        {
            var timestamp = DateTime.Now.ToString(".dd.MM.yyyy.HH.mm.ss");
            var excel     = new GenericWriteExcel();
            var excelFile = excel.CreateExcelFile(result);
            var fileName  = type + timestamp + ".xlsx";

            // contruct the path
            var filePath = Path.Combine(Settings.Default.SearchExportFolder, fileName);

            // save the file
            using (var file = System.IO.File.Create(filePath))
            {
                excelFile.Seek(0, SeekOrigin.Begin);
                excelFile.CopyTo(file);
            }

            // serve the reponse
            return(this.Json(new { File = timestamp, Type = type, Status = "OK" }));
        }