Unlock Excel file for reading in my Asp.Net MVC application

0

I'm developing an application in Asp.Net MVC where it will upload an Excel file generated by another system, but this file is coming blocked through reliability center file locking settings >

ThequestionishowdoIgetmyapplicationtoopenthefiletodothisunlock?

Thisisgivingerrorinthelinesbelowwhentryingtoopenfilethatisblocked.

Excel.Applicationapplication=newExcel.Application();Excel.Workbookworkbook=application.Workbooks.Open(filePath);Excel.Worksheetworksheet=workbook.ActiveSheet;Excel.Rangerange=worksheet.UsedRange;
[HttpPost]publicActionResultImportLots(HttpPostedFileBasepostedFile){if(postedFile==null||postedFile.ContentLength==0){ModelState.AddModelError(string.Empty,"Favor selecionar o arquivo de Lotes válido!");
        return View();
    }
    else
    {
        if (postedFile.FileName.EndsWith("xls") || postedFile.FileName.EndsWith("xlsx") || postedFile.FileName.EndsWith("XLS") || postedFile.FileName.EndsWith("XLSX"))
        {
            string filePath = string.Empty;
            string path = Server.MapPath("~/Uploads/");

            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }

            filePath = path + Path.GetFileName(postedFile.FileName);

            if (System.IO.File.Exists(filePath))
            {
                System.IO.File.Delete(filePath);
            }

            postedFile.SaveAs(filePath);

            Excel.Application application = new Excel.Application();
            Excel.Workbook workbook = application.Workbooks.Open(filePath);
            Excel.Worksheet worksheet = workbook.ActiveSheet;
            Excel.Range range = worksheet.UsedRange;

            var dataAbate = DateTime.Parse(((Excel.Range)range.Cells[3, 2]).Text);

            int j = 5;
            int linhasQueContem_P = 0;
            while (((Excel.Range)range.Cells[j, 19]).Text == "P")
            {
                linhasQueContem_P++;
                j++;
            }

            for (j = 5; j < (linhasQueContem_P + 5); j++)
            {
                if(!string.IsNullOrEmpty(((Excel.Range)range.Cells[j, 1]).Text))
                {
                    var lote = int.Parse(((Excel.Range)range.Cells[j, 1]).Text);
                    var pecurista = ((Excel.Range)range.Cells[j, 2]).Text;
                    var propriedade = ((Excel.Range)range.Cells[j, 5]).Text;
                }

                var procedencia = ((Excel.Range)range.Cells[j, 9]).Text;
                var especie = ((Excel.Range)range.Cells[j, 13]).Text;
                var quantidade = ((Excel.Range)range.Cells[j, 15]).Text;
                var sequenciaInicial = ((Excel.Range)range.Cells[j, 17]).Text;
                var sequenciaFinal = ((Excel.Range)range.Cells[j, 18]).Text;
            }

            workbook.Close();
        }
        else
        {
            ModelState.AddModelError(string.Empty, "Arquivo " + Path.GetExtension(postedFile.FileName) + " não é válido!");
            return View();
        }
    }

    return View();
}
    
asked by anonymous 06.04.2018 / 16:06

0 answers