finalize process

Asked

Viewed 252 times

1

I am a few days with a dilemma here in my code, I am developing a routine that reads the data of a spreadsheet and populates the data in cshtml of the page, I am programming in MVC5 with Razor. the code I created is:

public ActionResult Index(HttpPostedFileBase excelfile)
    {
        if (excelfile == null || excelfile.ContentLength == 0)
        {
            ViewBag.Error = "Selecione um arquivo!<br>";
            return View("Index", "Produto");
        }
        else
        {
            if (excelfile.FileName.EndsWith("xls") || excelfile.FileName.EndsWith("xlsx"))
            {
                string path = Server.MapPath("~/UpLoads/Planilhas/Produto" + excelfile.FileName);

                if (System.IO.File.Exists(path))
                    System.IO.File.Delete(path);
                excelfile.SaveAs(path);

                Excel.Application aplicacao = new Excel.Application();
                Excel.Workbook pastaTrabalho = aplicacao.Workbooks.Open(path);
                Excel.Worksheet worksheet = pastaTrabalho.ActiveSheet;
                Excel.Range range = worksheet.UsedRange;

                List<Produto> listaProduto = new List<Produto>();
                for (int row = 3; row <= range.Rows.Count; row++)
                {
                    Produto p = new Produto();

                    p.cd_Produto = ((Excel.Range)range.Cells[row, 1]).Text;
                    p.nm_Produto = ((Excel.Range)range.Cells[row, 2]).Text;
                    p.ds_Produto = ((Excel.Range)range.Cells[row, 3]).Text;
                    p.nm_TipoProduto = ((Excel.Range)range.Cells[row, 4]).Text;
                    p.nm_UnidadeMedida = ((Excel.Range)range.Cells[row, 5]).Text;
                    p.nm_MarcaProduto = ((Excel.Range)range.Cells[row, 6]).Text;
                    p.dsc_PesoBruto = decimal.Parse(((Excel.Range)range.Cells[row, 7]).Text);
                    p.dsc_PesoLiquido = decimal.Parse(((Excel.Range)range.Cells[row, 8]).Text);
                    p.nr_CodigoBarras = ((Excel.Range)range.Cells[row, 9]).Text;
                    p.qtd_Produto = int.Parse(((Excel.Range)range.Cells[row, 10]).Text);
                    p.qtd_Minima = int.Parse(((Excel.Range)range.Cells[row, 11]).Text);
                    p.vl_Compra = decimal.Parse(((Excel.Range)range.Cells[row, 12]).Text);
                    p.vl_Venda = decimal.Parse(((Excel.Range)range.Cells[row, 13]).Text);
                    p.vl_MinimoVenda = decimal.Parse(((Excel.Range)range.Cells[row, 14]).Text);

                    listaProduto.Add(p);
                }

                ViewBag.ListaProduto = listaProduto;

                return View("Index");
            }
            else
            {
                ViewBag.Error = "Tipo de arquivo incorreto!<br>";
                return View("Index", "Produto");
            }
        }
    }

My problem is being at the time of deleting the file if it exists, for example, if the user refreshes the page, it already gives an error because the process is running. I researched several ways to finalize this process but none worked, I want to finish the process as soon as I finish the routine, someone has already gone through it ?

  • read this: https://answall.com/q/159342/101 But I don’t understand the problem, why do you want to finish the process? I think you want to treat the symptom and not the cause of the problem.

1 answer

1

Jonathan, if you are saving the spreadsheet on disk just so you can read it using Excel.Application, then try saving it under a temporary name.

string extensao = Path.GetExtension(upload.FileName);
if (extensao == ".xls" || extensao == ".xlsx")
{   
    string path = Server.MapPath("~/UpLoads/Planilhas/Produto/" + Guid.NewGuid() + extensao);
    try
    {
        excelfile.SaveAs(path);

        Excel.Application aplicacao = new Excel.Application();
        Excel.Workbook pastaTrabalho = aplicacao.Workbooks.Open(path);
        Excel.Worksheet worksheet = pastaTrabalho.ActiveSheet;
        Excel.Range range = worksheet.UsedRange;

        List<Produto> listaProduto = new List<Produto>();
        for (int row = 3; row <= range.Rows.Count; row++)
        {
            Produto p = new Produto();

            p.cd_Produto = ((Excel.Range)range.Cells[row, 1]).Text;
            p.nm_Produto = ((Excel.Range)range.Cells[row, 2]).Text;
            p.ds_Produto = ((Excel.Range)range.Cells[row, 3]).Text;
            p.nm_TipoProduto = ((Excel.Range)range.Cells[row, 4]).Text;
            p.nm_UnidadeMedida = ((Excel.Range)range.Cells[row, 5]).Text;
            p.nm_MarcaProduto = ((Excel.Range)range.Cells[row, 6]).Text;
            p.dsc_PesoBruto = decimal.Parse(((Excel.Range)range.Cells[row, 7]).Text);
            p.dsc_PesoLiquido = decimal.Parse(((Excel.Range)range.Cells[row, 8]).Text);
            p.nr_CodigoBarras = ((Excel.Range)range.Cells[row, 9]).Text;
            p.qtd_Produto = int.Parse(((Excel.Range)range.Cells[row, 10]).Text);
            p.qtd_Minima = int.Parse(((Excel.Range)range.Cells[row, 11]).Text);
            p.vl_Compra = decimal.Parse(((Excel.Range)range.Cells[row, 12]).Text);
            p.vl_Venda = decimal.Parse(((Excel.Range)range.Cells[row, 13]).Text);
            p.vl_MinimoVenda = decimal.Parse(((Excel.Range)range.Cells[row, 14]).Text);

            listaProduto.Add(p);
        }

        ViewBag.ListaProduto = listaProduto;
        return View("Index");
    }
    finally
    {
        if (System.IO.File.Exists(path))
            System.IO.File.Delete(path);

    }
}
else
{
    ViewBag.Error = "Tipo de arquivo incorreto!<br>";
    return View("Index", "Produto");
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.