Thursday, February 4, 2010

HOW TO DOWNLOAD EXCEL SHEET

protected void linkBtnDownLoad_Click(object sender, EventArgs e)
{
DownLoadExcel();
}

private void DownLoadExcel()//function for download excel sheet format.
{
try
{
//b4 download file make sure that file access is not denied ,so set file attribute IsReadonly to false.
string download_filename = Server.MapPath("../Reports") + "\\StudentRegistration.xls";
System.IO.FileInfo fileInfo = new System.IO.FileInfo(download_filename);
fileInfo.IsReadOnly = false;
FileStream fs;
fs = File.Open(Server.MapPath("../Reports") + "\\StudentRegistration.xls", FileMode.Open);
Byte[] bytBytes = new Byte[fs.Length];
fs.Read(bytBytes, 0, Convert.ToInt32(fs.Length));
fs.Close();
Response.AddHeader("Content-disposition", "attachment; filename=StudentRegistration.xls");
Response.ContentType = "application/octet-stream";
Response.BinaryWrite(bytBytes);
Response.End();
}
catch (Exception ex)
{
String errorMessage;
errorMessage = "Error: ";
errorMessage = String.Concat(errorMessage, ex.Message);
errorMessage = String.Concat(errorMessage, " Line: ");
errorMessage = String.Concat(errorMessage, ex.Source);
LCE.Configuration.ErrorLog("ManageUsers/ImportFromExcel.cs", errorMessage);
}
}

No comments:

Post a Comment