Open an existing Excel file using C#.

Vishalneeraj-24503
Posted by Vishalneeraj-24503 under Visual Studio category on | Points: 40 | Views : 382
With the help of Response object we can achieve:-
Import System.IO Namespace.
using System;
using System.Text;
using System.IO;

protected void BtnOpen_Click(object sender, EventArgs e)
{
try
{
string XlsPath = Server.MapPath(@"~/abc.xlsx");
FileInfo fileDet = new System.IO.FileInfo(XlsPath);
Response.Clear();
Response.Charset = "UTF-8";
Response.ContentEncoding = Encoding.UTF8;
Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(fileDet.Name));
Response.AddHeader("Content-Length", fileDet.Length.ToString());
Response.ContentType = "application/ms-excel";
Response.WriteFile(fileDet.FullName);
Response.End();
}
catch (Exception ex)
{
throw ex;
}
}

Comments or Responses

Login to post response