Exemplo n.º 1
0
        private IEnumerable <T> Process <T>(DWControleSocialContext dbContext, Microsoft.AspNetCore.Http.FormFile file, AddRangeObserver observer)
            where T : IFT_Staging <T>
        {
            var idSeedHistory = default(long);
            var rows          = Enumerable.Empty <T>();
            var hashs         = new Dictionary <string, string>();

            using (var fileStream = file.OpenReadStream())
            {
                using (var md5 = new MD5CryptoServiceProvider())
                {
                    var hash = BitConverter.ToString(md5.ComputeHash(fileStream))
                               .Replace("-", string.Empty)
                               .ToUpperInvariant();

                    var seedHistory = dbContext.SeedHistory.FirstOrDefault(o => o.Hash == hash);
                    if (seedHistory == default)
                    {
                        seedHistory = new __SeedHistory {
                            Arquivo = file.FileName, Hash = hash
                        };
                        dbContext.Add(seedHistory);
                        dbContext.SaveChanges();
                    }
                    //else
                    //{
                    //    throw new InvalidOperationException("Este arquivo já foi carregado");
                    //}
                    hashs.Add(file.FileName, hash);
                    idSeedHistory = seedHistory.Id;
                }
                fileStream.Seek(0, SeekOrigin.Begin);
                rows = ParseCSV <T>(idSeedHistory, fileStream, observer);
            }
            return(rows);
        }
Exemplo n.º 2
0
        // https://localhost:44397/Blog/OnPostImport
        // https://github.com/miladsoft/npoi/blob/master/npoi_Example/Controllers/HomeController.cs
        public ActionResult OnPostImport()
        {
            // using NPOI.HSSF.UserModel;
            // using NPOI.SS.UserModel;
            // using NPOI.XSSF.UserModel;


            // Microsoft.AspNetCore.Http.IFormFile file = Request.Form.Files[0];
            string fullPath = @"C:\Users\Administrator\Downloads\demo2.xlsx";

            System.IO.Stream baseStream = System.IO.File.OpenRead(@"C:\Users\Administrator\Downloads\demo.xlsx");
            Microsoft.AspNetCore.Http.IFormFile file = new Microsoft.AspNetCore.Http.FormFile(baseStream, 0, baseStream.Length, "thePostedFile", "demo.xlsx");

            // string folderName = "Upload";
            // string webRootPath = "_hostingEnvironment.WebRootPath";
            // string newPath = System.IO.Path.Combine(webRootPath, folderName);
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            // if (!System.IO.Directory.Exists(newPath))
            // {
            // System.IO.Directory.CreateDirectory(newPath);
            // }
            if (file.Length > 0)
            {
                string sFileExtension = System.IO.Path.GetExtension(file.FileName).ToLower();

                NPOI.SS.UserModel.IWorkbook workbook;
                NPOI.SS.UserModel.ISheet    sheet;
                // string fullPath = System.IO.Path.Combine(newPath, file.FileName);
                using (var stream = new System.IO.FileStream(fullPath, System.IO.FileMode.Create))
                {
                    file.CopyTo(stream);
                    stream.Position = 0;

                    if (sFileExtension == ".xls")
                    {
                        // This will read the Excel 97-2000 formats
                        // NPOI.HSSF.UserModel.HSSFWorkbook hssfwb = new NPOI.HSSF.UserModel.HSSFWorkbook(stream);
                        workbook = new NPOI.HSSF.UserModel.HSSFWorkbook(stream);
                    }
                    else
                    {
                        // This will read 2007 Excel format
                        // NPOI.XSSF.UserModel.XSSFWorkbook hssfwb = new NPOI.XSSF.UserModel.XSSFWorkbook(stream);
                        workbook = new NPOI.XSSF.UserModel.XSSFWorkbook(stream);
                    }

                    sheet = workbook.GetSheetAt(0);                     //get first sheet from workbook

                    NPOI.SS.UserModel.IRow headerRow = sheet.GetRow(0); //Get Header Row
                    int cellCount = headerRow.LastCellNum;
                    sb.Append("<table class='table'><tr>");
                    for (int j = 0; j < cellCount; j++)
                    {
                        NPOI.SS.UserModel.ICell cell = headerRow.GetCell(j);
                        if (cell == null || string.IsNullOrWhiteSpace(cell.ToString()))
                        {
                            continue;
                        }
                        sb.Append("<th>" + cell.ToString() + "</th>");
                    }
                    sb.Append("</tr>");
                    sb.AppendLine("<tr>");
                    for (int i = (sheet.FirstRowNum + 1); i <= sheet.LastRowNum; i++) //Read Excel File
                    {
                        NPOI.SS.UserModel.IRow row = sheet.GetRow(i);
                        if (row == null)
                        {
                            continue;
                        }
                        if (row.Cells.All(d => d.CellType == NPOI.SS.UserModel.CellType.Blank))
                        {
                            continue;
                        }
                        for (int j = row.FirstCellNum; j < cellCount; j++)
                        {
                            if (row.GetCell(j) != null)
                            {
                                sb.Append("<td>" + row.GetCell(j).ToString() + "</td>");
                            }
                        }
                        sb.AppendLine("</tr>");
                    }
                    sb.Append("</table>");
                }
            }

            return(this.Content(sb.ToString()));
        }