예제 #1
0
        public async Task Issue3_行数读取错误导致row为null错误()
        {
            string curDir  = Environment.CurrentDirectory;
            string fileUrl = Path.Combine(curDir, "Resources/bugs", "issue3.xlsx");

            var rows = await _excelImportService.ValidateAsync <PerformanceTest>(new ImportOption()
            {
                FileUrl = fileUrl
            });
        }
        public ExcelImportServiceTest()
        {
            _excelImportService = _excelImportService.Resolve();

            string curDir  = Environment.CurrentDirectory;
            string fileUrl = Path.Combine(curDir, "Resources", "CarImport.xlsx");

            _rows = _excelImportService.ValidateAsync <ExcelCarTemplateDTO>(new ImportOption()
            {
                FileUrl = fileUrl
            }).Result;
        }
예제 #3
0
        /// <summary>
        /// Excel导入
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="filePath">Excel绝对路径</param>
        /// <returns></returns>
        public async Task <DataTable> ExcelImport <T>(string filePath) where T : class, new()
        {
            var _rows = await _excelImportService.ValidateAsync <T>(new ImportOption()
            {
                //Excel文件绝对地址
                FileUrl = filePath,
                //数据起始行索引,默认1第二行
                DataRowStartIndex = 1,
                //表头起始行索引,默认0第一行
                HeaderRowIndex = 0,
                //映射字典,可以将模板类与Excel列重新映射, 默认null
                MappingDictionary = null,
                //页面索引,默认0第一个页签
                SheetIndex = 0,
                //校验模式,默认StopOnFirstFailure校验错误后此行停止继续校验,Continue:校验错误后继续校验
                ValidateMode = ValidateModeEnum.Continue
            });

            //数据校验
            var errorDatas = _rows.Where(x => !x.IsValid);

            if (errorDatas.Count() > 0)
            {
                NLogHelper.ErrorLog("Excel导入验证失败。");
                return(null);
            }

            //导入转成DataTable
            return(await _excelImportService.ToTableAsync <T>
                   (
                       //文件绝对地址
                       filePath,
                       //页签索引,默认0
                       0,
                       //表头行索引,默认0
                       0,
                       //数据行索引,默认1
                       1,
                       //读取多少条数据,默认-1全部
                       -1));
        }