Need to be able to read an Excel file uploaded using FileUploadControl in ASP.NET. The solution will be hosted on a server. I do not want to store the Excel file on the server. I would like to directly convert the excel content into a dataset or a datatable and utilize.
Below are the two solutions I already found but would not work for me.
LINQTOEXCEL - This method works when you have an excel file on your local machine and you are running your code on the local machine. In my case, the user is trying to upload an excel file from his local machine using a webpage hosted on a server.
ExcelDataReader - I am currently using this one, but this is a third party tool. I cannot move this to our customer. Also if a row/column intersection is carrying a formula, then that row/column intersection's data is not being read into the dataset.
Most of the suggestions i found on google and StackOverflow work when both the excel and the .NET solution are on the same machine. But in mine, I need it to work when the solution is hosted on a server, and users are trying to upload excel using the hosted webpage on their local machine. If you have any other suggestions, could you please let me know?
This is how to do this in MVC using ClosedXML.Excel. I do know this answer is too late. I just wanted to put this answer for all those who land on this page after googling problem. In Visual Studio click on tools menu and expand NuGet Package Manager and then run Package manager console.Type the following command:
The Model:
The Controller:
This link has many examples showing different ways of handling diverse stuff of excel.
https://github.com/ClosedXML/ClosedXML/tree/9ac4d868a313f308b82e94617b9cc2d28baeb1c3/ClosedXML
The View
You can use the
InputStream
property of theHttpPostedFile
to read the file into memory.Here's an example which shows how to create a
DataTable
from theIO.Stream
of aHttpPostedFile
usingEPPlus
:Here's the VB.NET version:
For the sake of completeness, here's the aspx:
Maybe you can take a look at Koogra this is an Open Source excel reader (readonly no writer) I think you will get a stream back from the client. Then you can do all your things like you're doing now Read from memorystream and write to Database.
I hope this helps.