How to upload an excel and show your data In C # (MVC)?

0

I researched on the web how to upload an excel file from:

<input type="file"/>

After loading it into a list and passing it to a list (I do not care) I just want to know how I can read it from the file loaded in the input. I can not find a way to do it. The examples that I have seen use it from the route, that is, they only load the chains but not the file

    
asked by Acd 13.06.2017 в 02:24
source

3 answers

1

This example helped me solve.

link

    
answered by 21.06.2017 / 17:35
source
0

There is a library called NPOI for .NET that processes Excel documents.

link

The FILE type input saves the file in a temporary directory. Therefore, you must obtain the path of the file and use the NPOI library to process the Excel file and display the data.

    
answered by 13.06.2017 в 11:11
0

In asp.net mvc the file is sent via POST to an action in the parameter defined as HttpPostedFileBase

Upload and download files using ASP.NET MVC

ASP.NET MVC 5 with EF 6 - Working With Files

observe in the examples how to use

[HttpPost]
public ActionResult Index(HttpPostedFileBase file) {
  //codigo
}

when submitting the form submit the file you select in the

<input type="file" name="file" id="file" />

will be assigned to that parameter, a detail notes that the name of the input matches the parameter of the action.

This way you will have the excel on the server so you can work it out.

If you use the liberia ClosedXml you could upload the excel from a Stream, there is a lot of info

ClosedXml wiki

then you get the stream using

MemoryStream excelStream = new MemoryStream();
file.InputStream.CopyTo(excelStream);

to assign this in the constructor of XLWorkbook

    
answered by 13.06.2017 в 13:13