Home » excel » c# – How load excel file from HttpInputStream in ASP.NET

c# – How load excel file from HttpInputStream in ASP.NET

Posted by: admin May 14, 2020 Leave a comment

Questions:

How do I load an Excel file in an ASP page from System.Web.HttpInputStream? File uploaded by Telerik: RadUpload.

Appreciate any help.
Thanks

How to&Answers:

I had to do this recently from a file that a user would upload. I got it to work by saving the file temporarily then doing the following

string fileTempPath = Path.Combine(Server.MapPath("~/temp"), fileUploader.FileName);
                    fileUploader.SaveAs(fileTempPath);
                    fileTempPathHiddenField.Value = fileTempPath;

                    System.Data.DataTable excelDataTable = LoadData();

Later in the code:

 protected System.Data.DataTable LoadData()
        {
            string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" +
                        "Data Source=" + fileTempPathHiddenField.Value + ";" +
                        "Extended Properties=Excel 8.0;";
            OleDbDataAdapter dataAdapter = new OleDbDataAdapter(string.Format("Select * from [{0}$]", sheetNameTextBox.Text), connectionString);
            DataSet excellDataSet = new DataSet();

            dataAdapter.Fill(excellDataSet, "ExcelInfo");
            return excellDataSet.Tables["ExcelInfo"];
        }

Then once you are done with the temp file, go ahead and delete it if you want to.