public static void Main(string[] args) { Console.WriteLine("Iniciando programa..."); string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); // obtiene la ruta del escritorio del usuario en sesion Console.Write("Indique el nombre de archivo (Formato .xlsx) ubicado en su escritorio: "); string fileName = Console.ReadLine(); if (fileName.Remove(0, fileName.Length - 4) == ".xlsx") // si es formato ".xlsx" entra { using (var stream = File.OpenRead(path + "/" + fileName)) using (var importer = new ExcelImporter(stream)) { ExcelSheet sheet = importer.ReadSheet(); Event[] events = sheet.ReadRows <Event>().ToArray(); Console.WriteLine(events[0].Name); // Pub Quiz Console.WriteLine(events[1].Name); // Live Music Console.WriteLine(events[2].Name); // Live Football } } else { Console.WriteLine("Solo archivos de formato .xlsx"); } Console.ReadKey(); }
public static IEnumerable <TEntity> Load <TEntity, TConfiguration>(byte[] content) where TConfiguration : ExcelClassMap <TEntity>, new() { IEnumerable <TEntity> entities; try { using (var ms = new MemoryStream(content)) using (var importer = new ExcelImporter(ms)) { var index = GetHeadingIndex(content); if (index < 0) { throw new InvalidDataException("The file does not contain data-sheet data."); } importer.Configuration.RegisterClassMap <TConfiguration>(); var sheet = importer.ReadSheet(); sheet.HeadingIndex = index; entities = sheet.ReadRows <TEntity>().ToList(); } } catch (Exception ex) { var message = $"Cannot load collection of {typeof(TEntity).Name} type from the give file stream."; throw new ImportFormatException(message, ex); } return(entities); }
public void Ctor_Stream() { using (var stream = Helpers.GetResource("Primitives.xlsx")) using (var importer = new ExcelImporter(stream)) { Assert.Equal("Primitives", importer.ReadSheet().Name); } }
public void Ctor_IExcelDataReader() { using (var stream = Helpers.GetResource("Primitives.xlsx")) using (var reader = ExcelReaderFactory.CreateReader(stream)) using (var importer = new ExcelImporter(reader)) { Assert.Equal("Primitives", importer.ReadSheet().Name); } }
private List <MuncipalityTaxDto> ReadTaxesFromLocalFile(string fileLocalPath) { System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance); using var stream = System.IO.File.OpenRead(fileLocalPath); using var importer = new ExcelImporter(stream); ExcelSheet sheet = importer.ReadSheet(); var taxes = sheet.ReadRows <MuncipalityTaxDto>().ToArray().ToList(); return(taxes); }
public void OptionalNoSuchValueMap() { using var original = Helpers.GetResource("VeryLargeSheet.xlsx"); var importer = new ExcelImporter(original); importer.Configuration.RegisterClassMap <OptionalDataWithMissingValueClassMap>(); ExcelSheet sheet = importer.ReadSheet(); foreach (object value in sheet.ReadRows <DataClassWithMissingValue>()) { } }
public void DefaultMap() { using var original = Helpers.GetResource("VeryLargeSheet.xlsx"); var importer = new ExcelImporter(original); importer.Configuration.RegisterClassMap <DataClassMap>(); ExcelSheet sheet = importer.ReadSheet(); foreach (object value in sheet.ReadRows <DataClass>()) { } }
public async Task <IActionResult> Upload(File file) { if (file.FormFile.Length > 0) { using var importer = new ExcelImporter(file.FormFile.OpenReadStream()); ExcelSheet sheet = importer.ReadSheet(); var books = sheet.ReadRows <Book>().ToList(); _context.AddRange(books); await _context.SaveChangesAsync(); } return(RedirectToAction("Index")); }