pauloh
pauloh

Reputation: 1002

How load excel file from HttpInputStream in ASP.NET

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

Appreciate any help. Thanks

Upvotes: 1

Views: 1937

Answers (1)

Becuzz
Becuzz

Reputation: 6866

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.

Upvotes: 3

Related Questions