Open an Excel file from an ASP.NET form

0

Hello friends, I'm getting an excel like HttpRequestPostedFileBase from a form of Asp.NET , but I need to open it, I know I can store the file and send it to me

  

Workbook sheet = excel.Workbooks.Open ("path / file.xls");

The path of the file to open it, but in this case I want to open it and confirm its contents before saving it. So I want to open it, until now I have this, but it asks me the name with the route, does anyone know if I can open it in this same way or some other library?

if (Request.Files.Count > 0) {
  var file = Request.Files[0];
  string value = Request.Form["lstlistaCuentas"].ToString();
  string[] keys = Request.Form.AllKeys;
  string cuenta = form["lstlistaCuentas"].ToString();
  string banco = form["lstBancos"].ToString();

  string extension = System.IO.Path.GetExtension(file.FileName);
  string fname = System.IO.Path.GetFileName(file.FileName);

  if (extension == ".xls" || extension == ".xlsx") {
    switch (banco) {
      //banamex
      case ("1"):
        string fn = file.FileName;
        if (!fn.Contains("BX")) {
          return View("Error");
        } else {
          Application excel = new Application();

          Workbook sheet = excel.Workbooks.Open(" quiero mandar unicamente el HttpRequestPostedFileBase ");
          Worksheet x = excel.ActiveSheet as Worksheet;
          _Worksheet xlWorksheet = sheet.Sheets[1];
          Excel.Range xlRange = xlWorksheet.UsedRange;
        }


        break;
    }
  }
}
    
asked by E.Rawrdríguez.Ophanim 16.08.2018 в 17:58
source

1 answer

1

I understand that what you want to do is open an Excel file directly from the variable HttpRequestPostedFileBase to validate the document and then save it to disk.

According to this comment in the English community, with the EPPlus you can do it in the following way:

public void ImportExcelXls(HttpPostedFileBase fileBase)
{
    using (var package = new ExcelPackage(fileBase.InputStream))
    {
        // get the first worksheet in the workbook
        ExcelWorksheet worksheet = package.Workbook.Worksheets[1];
        int col = 1;

        for (int row = 1; worksheet.Cells[row, col].Value != null; row++)
        {
            // do something with worksheet.Cells[row, col].Value                    
        }
    } // the using 
}
    
answered by 16.08.2018 / 19:00
source