How to upload excel file using ASP.net and C#?
-
5Upload to where? Be spesific.Soner Gönül– Soner Gönül2011-04-19 10:49:42 +00:00Commented Apr 19, 2011 at 10:49
-
6What have you tried? A quick search reveals lots of information google.co.uk/search?q=upload+excel+file+asp.netFishcake– Fishcake2011-04-19 10:50:55 +00:00Commented Apr 19, 2011 at 10:50
Add a comment
|
3 Answers
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"));
}
}
}
Comments
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"];
}
}