1

How to upload excel file using ASP.net and C#?

2

3 Answers 3

5

Would not be very easy it is ?

<!-- front end -->
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="btnUpload" runat="server" Text="Upload Excel File" 
            onclick="btnUpload_Click" />

//backend
protected void btnUpload_Click(object sender, EventArgs e)
{
    if (FileUpload1.HasFile)
    {
        if (System.IO.Path.GetExtension(FileUpload1.FileName) == ".xls" || System.IO.Path.GetExtension(FileUpload1.FileName) == ".xlsx")
        {
            FileUpload1.SaveAs(Server.MapPath("~/upload/temp/Forecast.xls"));
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

2

In your aspx page you can use the following...make sure you have enctype="multipart/form-data" in your form tag it will allow the posting of files.

<form enctype="multipart/form-data">
    <input type="file" id="excel-file" name="excel-file" />
    <input type="submit" />
</form>

Getting the file from the form post...

protected void Page_Load(object sender, EventArgs e)
{
    if(this.IsPostback)
    {
        var yourFile = this.Request.Files["excel-file"];
    }
}

Comments

1

I use This It works perfectly, without anything needed to be changed in the code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.