コード例 #1
0
        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();
        }
コード例 #2
0
        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);
        }
コード例 #3
0
 public void Ctor_Stream()
 {
     using (var stream = Helpers.GetResource("Primitives.xlsx"))
         using (var importer = new ExcelImporter(stream))
         {
             Assert.Equal("Primitives", importer.ReadSheet().Name);
         }
 }
コード例 #4
0
 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);
             }
 }
コード例 #5
0
        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);
        }
コード例 #6
0
        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>())
            {
            }
        }
コード例 #7
0
        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>())
            {
            }
        }
コード例 #8
0
        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"));
        }