Ejemplo n.º 1
0
        private void ImportExcelMainFunction(IExcelImporter impoerter, ExcelStructreTable tbl, bool Confirmed)
        {
            if (impoerter == null)
            {
                throw new Exception("impoerter ارسالی نال است ");
            }

            if (tbl == null)
            {
                throw new Exception("tbl ارسالی نال است ");
            }
            if (Request.Files.Count == 0)
            {
                throw new Exception("هیچ فایلی ارسال نشده است ");
            }

            tbl.Confirmed = Confirmed;

            var file = Request.Files[0];

            if (file == null)
            {
                throw new Exception("فایل ارسال شده نال است ");
            }

            Stream stream = file.InputStream;

            impoerter.ImportExcel(stream, tbl);

            impoerter.ValidateModels();
        }
Ejemplo n.º 2
0
        public void TestImport()
        {
            var sheet1 = new BookSheet <ImportStudent>
            {
                UniqueValidationPrompt = "零花钱不可重复",
                HeaderRowIndex         = 0,
                SheetIndex             = 0,
                ValidateHandler        = (list) =>
                {
                    foreach (ImportStudent model in list)
                    {
                        if (model.IsValidated && model.Name == "name0")
                        {
                            model.SetError(nameof(model.Name), "名字不可为0");
                        }
                        if (model.IsValidated && model.Money < 0.5)
                        {
                            model.SetError(nameof(model.Money), "零花钱不可小于0.5");
                        }
                    }
                }
            };
            var sheet2 = new BookSheet <ImportGrade>
            {
                HeaderRowIndex = 0,
                SheetIndex     = 1
            };
            var sheet3 = new BookSheet <ImportSchool>
            {
                HeaderRowIndex  = 1,
                SheetIndex      = 2,
                ValidateHandler = (list) =>
                {
                    foreach (var model in list)
                    {
                        if (model.Price > 0.5)
                        {
                            model.SetError(nameof(model.Price), "学费不可大于0.5");
                        }
                    }
                }
            };
            var bookmodel = new ImportBook().SetSheets(sheet1, sheet2, sheet3);

            using var inputStream = new FileStream(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Excels//Export.xlsx"), FileMode.OpenOrCreate, FileAccess.Read);
            using var outStream   = new FileStream("D://Export_Error.xlsx", FileMode.Create, FileAccess.Write);
            var importResult = _importer.ImportExcel(inputStream, ExtEnum.Xlsx, bookmodel, outStream);

            var success         = importResult.ImportSuccess;
            var summaryErrorMsg = importResult.GetSummaryErrorMessage();
            var notDisplayMsg   = importResult.GetNotDisplayErrorMessage();
            var sheet1Data      = importResult.Sheets[0].GetData <ImportStudent>();

            Output.WriteLine($"success:{success}");
            Output.WriteLine("summaryErrorMsg------------");
            Output.WriteLine(summaryErrorMsg);
            Output.WriteLine("notDisplayMsg------------");
            Output.WriteLine(notDisplayMsg);
        }
Ejemplo n.º 3
0
        public void TestImport()
        {
            var sheet = new BookSheet <ImportBalanceOfPayment>
            {
                UniqueValidationPrompt = "年月不可重复",
                HeaderRowIndex         = 0,
                SheetIndex             = 0
            };
            var bookmodel = new ImportBook().SetSheets(sheet);

            var inputFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Excels//Simple.xlsx");

            using var inputStream = new FileStream(inputFilePath, FileMode.OpenOrCreate, FileAccess.Read);
            using var outStream   = new FileStream("D://Simple_Error.xlsx", FileMode.Create, FileAccess.Write);

            var importResult = _importer.ImportExcel(inputStream, inputFilePath.GetExt(), bookmodel, outStream);

            var success         = importResult.ImportSuccess;
            var summaryErrorMsg = importResult.GetSummaryErrorMessage();
            var importData      = importResult.Sheets[0].GetData <ImportBalanceOfPayment>();

            Output.WriteLine($"success:{success}");
            Output.WriteLine(summaryErrorMsg);
        }