Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8523671
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T07:28:33+00:00 2026-06-11T07:28:33+00:00

Need to be able to read an Excel file uploaded using FileUploadControl in ASP.NET.

  • 0

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.

  1. 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.

  2. 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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T07:28:35+00:00Added an answer on June 11, 2026 at 7:28 am

    You can use the InputStream property of the HttpPostedFile to read the file into memory.

    Here’s an example which shows how to create a DataTable from the IO.Stream of a HttpPostedFile using EPPlus:

    protected void UploadButton_Click(Object sender, EventArgs e)
    {
        if (FileUpload1.HasFile && Path.GetExtension(FileUpload1.FileName) == ".xlsx")
        {
            using (var excel = new ExcelPackage(FileUpload1.PostedFile.InputStream))
            {
                var tbl = new DataTable();
                var ws = excel.Workbook.Worksheets.First();
                var hasHeader = true;  // adjust accordingly
                // add DataColumns to DataTable
                foreach (var firstRowCell in ws.Cells[1, 1, 1, ws.Dimension.End.Column])
                    tbl.Columns.Add(hasHeader ? firstRowCell.Text
                        : String.Format("Column {0}", firstRowCell.Start.Column));
    
                // add DataRows to DataTable
                int startRow = hasHeader ? 2 : 1;
                for (int rowNum = startRow; rowNum <= ws.Dimension.End.Row; rowNum++)
                {
                    var wsRow = ws.Cells[rowNum, 1, rowNum, ws.Dimension.End.Column];
                    DataRow row = tbl.NewRow();
                    foreach (var cell in wsRow)
                        row[cell.Start.Column - 1] = cell.Text;
                    tbl.Rows.Add(row);
                }
                var msg = String.Format("DataTable successfully created from excel-file. Colum-count:{0} Row-count:{1}",
                                        tbl.Columns.Count, tbl.Rows.Count);
                UploadStatusLabel.Text = msg;
            }
        }
        else 
        {
            UploadStatusLabel.Text = "You did not specify a file to upload.";
        }
    }
    

    Here’s the VB.NET version:

    Sub UploadButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        If (FileUpload1.HasFile AndAlso IO.Path.GetExtension(FileUpload1.FileName) = ".xlsx") Then
            Using excel = New ExcelPackage(FileUpload1.PostedFile.InputStream)
                Dim tbl = New DataTable()
                Dim ws = excel.Workbook.Worksheets.First()
                Dim hasHeader = True ' change it if required '
                ' create DataColumns '
                For Each firstRowCell In ws.Cells(1, 1, 1, ws.Dimension.End.Column)
                    tbl.Columns.Add(If(hasHeader,
                                       firstRowCell.Text,
                                       String.Format("Column {0}", firstRowCell.Start.Column)))
                Next
                ' add rows to DataTable '
                Dim startRow = If(hasHeader, 2, 1)
                For rowNum = startRow To ws.Dimension.End.Row
                    Dim wsRow = ws.Cells(rowNum, 1, rowNum, ws.Dimension.End.Column)
                    Dim row = tbl.NewRow()
                    For Each cell In wsRow
                        row(cell.Start.Column - 1) = cell.Text
                    Next
                    tbl.Rows.Add(row)
                Next
                Dim msg = String.Format("DataTable successfully created from excel-file Colum-count:{0} Row-count:{1}",
                                        tbl.Columns.Count, tbl.Rows.Count)
                UploadStatusLabel.Text = msg
            End Using
        Else
            UploadStatusLabel.Text = "You did not specify an excel-file to upload."
        End If
    End Sub
    

    For the sake of completeness, here’s the aspx:

    <div>
       <h4>Select a file to upload:</h4>
    
       <asp:FileUpload id="FileUpload1"                 
           runat="server">
       </asp:FileUpload>
    
       <br /><br />
    
       <asp:Button id="UploadButton" 
           Text="Upload file"
           OnClick="UploadButton_Click"
           runat="server">
       </asp:Button>    
    
       <hr />
    
       <asp:Label id="UploadStatusLabel"
           runat="server">
       </asp:Label>        
    </div>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've a requirement that I need to read an excel sheet programmatically using asp.net/C#
I need to be able to read the text of many different file types
I need to be able to read a file format that mixes binary and
I'm using SimpleXML for parsing XMl documents. I need to be able to read/update
I am using a web application to read the excel file and uploading it
I need to be able to read a file, break it into packets of
folks, I am able to read the excel sheet using apache POI in grails
I have a file, which I am able read with a windows application using
I need to be able to read a list of variables that follow certain
I need to be able to read the value of my attribute from within

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.